GOOD PROGRAMMING PRACTICES


GOOD PROGRAMMING PRACTICES - FUNDAMENTALS

  • Every function should be preceded by a comment describing the purpose of the function.
  • Add a comment to the line containing the right brace, } that close every function, including the main(). This helps in determining that the function is ended.
  • Indent the entire body of each function. This indentation emphasizes the functional structure of a program and helps make program easier to read.
  • Set a convention for the size of indent you prefer and then uniformly apply that convention.
  • Choosing meaningful variable name helps make a program self-documenting i.e fewer comments are needed.
  • Multiple-word variable names can help make a program more readable.Separate the words with underscore eg : comp_psyche or good_programming_practices.
  • Place a space after each comma to make a program more readable.
  • Place spaces on either side of binary operator. This makes the operator stand out and makes the program more readable.
  • Although it is allowed that we can have multiple statement in a single line but to make program more readable there should be only one statement per line in a program.

GOOD PROGRAMMING PRACTICES - CONTROL STRUCTURES

  • Indent both body statement of an if...else statement.
  • If there are several level of indentation, each level should be indented with same additional amount of space.
  • When performing division by an expression whose value could be zero, explicitly test this case and handle it appropriately in your program ( such as printing an error message ) rather than allowing the fatal error to occur.
  • In a sentinel-controlled loop, the prompts requesting data entry should explicitly remind the user what the sentinel value is.
  • Too many levels of nesting can make a program difficult to understand. As a rule, try to avoid using more than three levels of nesting.
  • Limit the size of the control statement headers to a single line if possible.
  • Although the case clauses and the default case clause in a switch statement can occur in any order but its advisable to place the default clause at last.
  • In a switch statement when the default clause is last then the break statement is not required.

GOOD PROGRAMMING PRACTICES - FUNCTION

  • Familiarize yourself with rich collection of functions in C standard library.
  • Although It is not incorrect to do so but do not use the same names for a function's arguments and the corresponding parameters in function definition. This helps avoid ambiguity.
  • Choosing meaningful function names and meaningful parameter names makes program more readable and helps avoid excessive use of comments.
  • Parameter names are sometimes included in function prototype for documentation purposes. The compiler ignore these names.
  • Use only uppercase letters in the names of enumeration constants to make these constants stand out in a program and to indicate that enumeration constants are not variable.  

GOOD PROGRAMMING PRACTICES - ARRAY

  • Defining the size of each array as a symbolic constant make program more readable.
  • Use only uppercase letter for symbolic constant names. This makes these constants stand out in a program and reminds you that symbolic constants are not variable.
  • In multi-word symbolic constant names, separate the words with underscores for readability.
  • Strive for program clarity. Sometimes it may be worthwhile to trade off the most efficient use of memory or processor time in favor of writing clearer programs.
  • When passing an array to a function, also pass the size of the array. This helps make function reusable in many programs.

GOOD PROGRAMMING PRACTICES - STRING

  • When storing a string of characters in a character array be sure that the array is large enough to hold the largest string that will be stored.
  • Familiarize yourself with the various predefined string function. This helps in achieving task easily.
  • When using functions from the string-handling library, include the <string.h> header.

GOOD PROGRAMMING PRACTICES - POINTERS


  • Its preferable to include the letters Ptr in pointer variable names to make it clear that these variables are pointer and thus need to be handled appropriately.
  • Initialize pointer to prevent unexpected result.
  • If a variable does not change in the body of a function to which it is passed, the variable should be declared const to ensure that it is not accidentally modified.
  • Pass large objects such as structures using pointers to constant data to obtain the performance benefits of pass-by-reference.

GOOD PROGRAMMING PRACTICES - STRUCTURE AND UNION

  • Always provide a structure tag name when creating structure type. The structure type name is convenient for declaring new variables of the structure type later in the program.
  • Do not use spaces around  '->' and '.' operators. Omitting spaces helps emphasize that the expressions the operators are contained in are essentially single variable names.
  • Passing structure by reference is more efficient than passing structure by value.
  • Use typedef to help make a program more protable.
  • Capitalize the first letter of the typedef names to emphasize that they are synonyms for other types names.
  • Using typedef can help make a program more readable and maintainable.

Share this

Related Posts

Previous
Next Post »