Coding Guideline

The way to code a C program


General Coding Guideline

Some of the points to be considered while writting a code. Below is a list of such points taken from SLAC as well as from other sources. Some of the guidelines i added based on my experience.
  • Provide plenty of comments
  • Avoid clever tricks
  • Code for clarity rather than efficiency
  • Write linear code (code that starts at the beginning, goes through the middle and ends at the end)
  • Code defensively, check return codes and don't assume success
  • Use meaningful names for variables ( not i, ii, iii and j)
  • Indent nested blocks
  • Avoid implicit data-type conversions, use explicit conversions
  • Avoid complicated ``if'' constructs, use nested blocks
  • Avoid global variables
  • There should be consistent code indentation throughout all source and header files. This will make the code more readable. If your IDE supports for inserting space for tab then enable it.
  • Never make assumption while writting code. Different compilers may follow different rules to treat a piece of code
  • Never ignore warnings given by compiler even though it seems simple.
  • Include files should not contain executable code statements
  • File names must reflect the functionality implemented in that file (e.g. checkcrc.c should be intended to implement the CRC check routine)
  • When multiple programmers are working on different modules of a project then mske sure that everyone follows the same coding style rather than sticking with own

Useful Links