3
Useful/’Good’ Programming Practice There are some habits/rules of programming that will help you in writing readable, easy to understand, portable, and well maintainable code. Following these rules makes your code look good & helps finding mistakes/errors earlier. A few of such rules/habits are: 1. Indent each block of a code with at least 2 characters (4 char is quite common). This holds for functions and loops as well. This helps anyone, including yourself, in reading and understanding the flow of the code. Use the following vi command to set auto-indentation feature: :set ai 2. The following is one way of indenting blocks of code (there are other ways; this is just one among the better ones to practice). Make sure you use one way and then stick to it. When opening new blocks of code such as while, if, or for, leave the opening curly brace on the same line as the beginning of the block (while, if, etc.). The closing brace should be put in its own line (the only exception is else) aligning with the while, if, for. Always use opening and closing braces, even if it’s just a one-line block. This reduces future error when blocks are expanded beyond one line. A few good examples of proper indentation and brace positions are: for(lp = list; lp; lp = lp->next) { newlist = alpm_list_add(newlist, strdup(lp->data)); } while(it) { ptr = it->next; if(fn) { fn(it->data); } else { return 1; } free(it); it = ptr; }

Good Coding Practices

Embed Size (px)

DESCRIPTION

good coding practice

Citation preview

Useful/Good Programming PracticeThere are some habits/rules of programming that will help you in writing readable, easy to understand, portable, and well maintainable code. Following these rules makes your code look good & helps finding mistakes/errors earlier. A few of such rules/habits are:1. Indent each block of a code with at least 2 characters (4 char is quite common). This holds for functions and loops as well. This helps anyone, including yourself, in reading and understanding the flow of the code. Use the following vi command to set auto-indentation feature::set ai

2. The following is one way of indenting blocks of code (there are other ways; this is just one among the better ones to practice). Make sure you use one way and then stick to it.When opening new blocks of code such as while, if, or for, leave the opening curly brace on the same line as the beginning of the block (while, if, etc.). The closing brace should be put in its own line (the only exception is else) aligning with the while, if, for. Always use opening and closing braces, even if its just a one-line block. This reduces future error when blocks are expanded beyond one line. A few good examples of proper indentation and brace positions are:for(lp = list; lp; lp = lp->next) { newlist = alpm_list_add(newlist, strdup(lp->data));}while(it) { ptr = it->next; if(fn) { fn(it->data); } else { return 1; } free(it); it = ptr;}

3. When declaring a new function, put the opening and closing braces on their own line. Also, when declaring a pointer, do not put a space between the asterisk and the variable name. alpm_list_t *alpm_list_add(alpm_list_t *list, void *data){ alpm_list_t *ptr, *lp; ptr = list; if(ptr == NULL) { ... } ...}

[continued]4. Beware of compiler warnings generated. They are most likely pointing to some problem with the code. A good program should compile with no warnings. To display all warnings during compilation, use Wall option. Ex.cc -Wall yourprogam.c To convert all warnings into errors use Werror. Errors result into non-compilation of code. Ex.cc -Werror yourprogam.c

5. Note that gcc compiler supports c99 which is enabled by default (which has many a useful features), so your code may compile correctly even if it is not a valid ANSI C/ISO C/ C89/90. But the same code may not compile in many other compilers. Some of the c99 features that are not allowed in standard C (also called ANSI C, c89, c90) include C++ style comments, mixed declaration and code, and variable-length arrays. While these features are no doubt advanced & useful, avoid them while writing C programs in the lab. Write ANSI C conforming code in the lab. This will make you write code that will compile and run anywhere C is supported.To force the gcc compiler to follow Standard C, and convert any warning to errors, compile your program with these options:cc ansi pedantic Werror yourprog.corcc ansi pedantic-errors yourprog.c

6. The main() function should be one of the following (dont use void main()):

int main(){return 0;}

OR

int main(void){return 0;}

OR, if command-line args are used:

int main(int arc, char *argv[]){return 0;}