Welcome To Rahul Sharma's Website

Coding Style

    Sometimes you have to code things that you had already done in past. Or you might be interested in editing your old code, probably for adding new functionality or fixing bugs. When you open the code you start looking at lines, may be trying to recall, but suddenly you shout "What the hell is this?" Using good programming style you can make your program more readable, maintainable and extendable. There are many other things besides comments and indentation that you can do to make your code mode readable and manageable.

  • If you have a lot of functions in the program, then group similar functions to make separate files.

  • Use white spaces to break a function into paragraphs. A paragraph is a group of statements that do similar job. Like declaration of variables, getting input from user or all free (free means freeing memory) statements.

  • Put each statement on a line by itself. Means maximum one statement should allow per line.

  • Avoid very long statements. Use multiple shorter statements instead.

  • Keep programs files to no longer than about 2,000 to 3,000 lines. Most editors tend to get a bit slow when the file size gets to be more than about 3,000 lines.

  • Include a heading comment at the beginning of each file that explains the file.

  • Comment your code as you write it.

  • Use simple, descriptive variable names.

  • Use all uppercase for constants.

  • Don't use variable names that differ by only one or two characters. Make every name obviously different from every other name.

  • Follow every variable declaration with a comment that defines it.

  • Put spaces before and after each
    arithmetic operator, just like you put spaces between words when you write, e.g. a=b+c (wrong), a = b + c (more readable)

  • Indent one level for each new level of logic. The best indentation size is four spaces.

  • Use macros for the elements on which your code is heavily depends on. Like MAXITEMS for your program.