Wednesday 10 April 2013

Good programming practices

Here are some important points which we need to follow during programming as good programming practices:-

1)We need to initialize each variable with zero or null while declaring it.By this we reduce the chances of occurring an error due to garbage value initialized to the variables which are not intilaized.The pointrer which is not initialized while declaration may be dangerous.

2)We need to create any project in many modules means we need to create function for each specific task.It increase the reusablity of the code.

3)Always check file pointer is null or not and exit if null in the file handling programs because it may cause more problems because we don't come to know what the problem is.


  if (ColorImageFile == NULL  ||BlackandwhiteImageFile == NULL)
  {
printf("can't open the file");
exit(1);
  }

4)In file handling program use fread and fwrite in the place of fputc and fgetc because it is more efficient .

5)Use try for that part of program where is chance of an exception and handle it after try block in catch or finally.Use __try with __finally and __leave and use try for catch and throw.

6)Use modifiers with datatypes according to need .While if we have only positive values in an array then we need to declared it as unsigned int.It also increase the range twice.

7)Always prefer dynamic memory allocation because it saves memory.

8)Always give a meaning full name to the variables and functions.It plays a very important role to understand the program by anyone else or the programmer himself.

9)Adding comments is also a very important thing which helps anybody to understand a block of code in seconds.

10)Always use the format specifier corresponding to the variable declared because minor change in format specifier does major change in output .For example if we store a negative value in a signed int and print it in %u then it gives different output.So we need to be careful in this.
 

No comments:

Post a Comment