I have been coding for the past 7 years in a MNC and had experienced a lot of programming rules. Here I am sharing some of the tips with which you can start writing better programs in C/C++.
Learning New APIs
You can improve the programming skills day by day by learning about new APIs.
For example, in c++ if you know about
count API in <
algorithm>, you no need to write extra piece of code for getting the number of elements that have same value in the given array.
By knowing about this type of APIs, you can write effective code and also reduce your LOC(Lines of Code). C++ is so vast and you can read about an API each day and use it in day-to-day programming.
Using checkpatch Tool
If you are a C programmer and need to check your code, you can run
checkpatch tool.
checkpatch.pl is a script in the kernel tree that facilitates better C code, and can be used to check many coding style rules.
You can run the checkpatch tool as below:
checkpatch.pl --terse --file --no-tree <File Name>
Writing Comments for Doxygen
While writing code you need to write proper comments, so that you can generate documentation later using doxygen.
You can check
doxygen documentation for knowing how to write comments in your source code. You need to put extra * after comments for doxygen to read.
For example for a function declaration, you can write comment as
/**
Description of function
@pre Init function should be called.
@param [in] win Window object
@return void
*/
void add_text(struct window *win);
Preventing Memory Leaks - Valgrind
While writing code, you need to have clear understanding about memory allocation and deallocation with which you can prevent memory leaks.
The basic rule is that for every
new there should be
delete in C++ whereas for every
malloc/calloc there should be
free in C.
When you finish writing your code, you can further know the memory leaks with valgrind. You can run valgrind by using below command
valgrind --tool=memcheck --leak-check=full <outputfile>
You will get the detail logs including the function names and line numbers.
Using cppcheck tool
Cppcheck is a static analysis tool for your C/C++ code. The features of cppcheck tool are as follows:
- Automatic variable checking
- Bounds checking for array overruns
- Classes checking (e.g. unused functions, variable initialization and memory duplication)
- Usage of deprecated or superseded functions according to Open Group[13]
- Exception safety checking, for example usage of memory allocation and destructor checks
- Memory leaks, e.g. due to lost scope without deallocation
- Resource leaks, e.g. due to forgetting to close a file handler
- Invalid usage of Standard Template Library functions and idioms
- Miscellaneous stylistic and performance errors
Using Design Patterns
Design patterns define a solution to the common software design issues. There are many design patterns like creational patterns, structural patterns, behavioral patterns.
By using these patterns, you can further improve your code structure and reduce design issues
Exception Handling
You need to take care of exceptions while writing your code.
Exceptions provide a way to react to exceptional circumstances like runtime errors(divide by zero) in programs by transferring control to special functions called handlers.
This is popular in C++ rather than in C as C++ provides special keywords try, catch, throw for handling exceptions.
I will add more programming tips soon. Check out the space.
Related Posts