1. Before sitting down for coding, you must have a formal or a paper-napkin design of the solution to be coded. Never start coding without any design unless the code is trivial.
2. Good code documentation is as important as good knowledge of a programming language. Write brief logic for each major block of your code as comments in source code file itself. It’s good to mention the creation and modification dates of your program along-with why modification was required.
3. Maintaining versions of your program is another important task. Some present-day programming tools already have built-in version management. Whenever you make any change to your program, they save its copy as.bak file.
My approach is to maintain 3 versions of a program. Say, I have a file program.c which is used by other project team members also. I copy this file as program.c.old as backup and make another copy as program.c.wk where I do modifications. When modifications are successfully compiled, replace the program.c with.wrk file.
You can also append a date or some explanation phrase to your program versions like program260505.c or programReadFnWrking.c.
Help us to write more!
If you like reading our articles, consider supporting us on Patreon! This helps us to do even more for you!
Nonetheless, you can also comment which is free…
4. If your project contains multiple source files then maintain a README file stating the purpose of each source files, data files, intermediate and log files (if any). You may also mention the compilation and execution steps.
5. Ever wondered why your IF statement is not working as it should do. Maybe you are using single equal i.e. “=” instead of “==” in the condition check. A good approach is to write a condition in reverse order. So, your condition should read something like this:
if ( 10==i)…. So, if you put a single equal sign by mistake then it will be detected at compilation time only as an error.
6. While using loops and conditional statements, always first put closing braces corresponding opening braces and then write the inner statements i.e.
1) for(int i=0;i<10;i++)
The numbers at the starting of each line indicate the sequence of writing loop codes.
7. Avoid using magic numbers. For example, instead of writing
circleArea = 3.14 * pow(radius,2);
use following code:
#define PI 3.14
circleArea = PI * pow(radius,2);
8. Use meaningful variable and function names. For e.g. instead of using ‘r’ use ‘radius’ to represent the radius of a circle. Similarly, the function name ‘calculateArea’ is better than any cryptic short name. In a hurry, we may use short variable names but the time saved leads to double wastage of time later when you guess for what that short variable name stands for.
9. Using print statements for later debugging is a good habit. But, removing them when the final code is ready is, sometimes, a risky task. So, make a function that displays debugging information passed to it. When your final version is ready, simply comment on the internals of this function. So, this requires changes only at one place.
10. Once you are done with coding, start optimizing your code. Some of the variables you declared earlier may not be of use at this stage. Similarly, statements that are not loop dependent can be moved out of the loop block. Sound knowledge of compiler can also help in optimizing the code further.
11. With good knowledge of your operating system and hardware, you can improve the performance of your program in terms of resource requirements, etc.
12. Always indent your code for clarity and easy readability.
13. You will also like the idea of organizing project files in into various folders like SOURCE, HEADERS, MAKE, EXES, etc.
14. Study the code written by others. This will bring to you new programming techniques and what approach they have followed for the task for which you have also coded.
15. Last but not least important, take a backup of your source-code files so that your effort won’t go waste if hard-disk crashes or a similar mishappening occurs.
Please comment and let us know about your thoughts on the topic!
Featured Image courtesy: https://www.geeksforgeeks.org/