Few Things To remember while designing and writing the code

November 25, 2008 at 6:10 PM | Posted in Design | Leave a comment
  • Limits nesting in the the function as far as possible.  3 to 4 nesting is fine.
  • Always write reentrant and thread safe code. For more on reentrance, check the link About reentrance
  • Header file of a  module should contain only those things ( function prototype, variables) which  other module should know. For internal things, either write separate internal header file or put in c file itself.
  • Module design should be layered. Each layer should perform a specific job, and it should have information only it is needed, means it should not know any information of other layer.
  • Avoid system call and low level function in the external function/interface.  malloc, strcpy are fine.
  • Similar function should accept similar parameter, like getDatat or setData etc.
  • Always pass pointer to structure to the function, never pass the structure directly, because
    1. If structure is passed directly, whole structure should be copy to the function, so overhead.
    2. Can not reflect the changes in the structure to the caller.(Pass  by value & Pass by reference  concept)
  • Initialize the character variable with memset before using it.
  • Try to reduce the dependency to one point rather than flowing through whole code. For example, if accessing something from other module, try to make it at one point. so if any changes happen in that module you need to change only at one point, not throughout the code.
  • If u create a structure, important thing should come first, temporary fields should come last.
  • Use consistent name for parameter or structure element. For example- if you have two counters, then bothe name should be like noOfRead and not noOfWrite, or readNo and writeNo, but not like one is noOfRead and another is WriteNo.
  • If same file can be simultaneously read and write, then always use locking mechanism. Exclusive locking for write and shared lock for read.
  • Use specific name for #define and global variable inside a module to avoid compile time conflict. For example, if use use #define READ, then there is a chance that other module use the same name and it will give conflict at compile time. So use something like <module_name>READ
  • Select a proper data type for variable to avoid overflow or waste of memory. Do our counter fields need to be 64 bits?  If we use a 32 bit number,
    // and if the event occour once every second, that means 136 years.
    // 4294967296/60/60/24/365 = 136.19251953323186199898
  • Put the externally callable functions together at the beginning, then have the local functions.
  • Any calculation in code, should be verified with different condition.

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.

Follow

Get every new post delivered to your Inbox.