Coding Standards


Introduction

To be good, according to the vulgar standard of goodness, is obviously quite easy. It merely requires a certain amount of sordid terror, a certain lack of imaginative thought, and a certain low passion for middle-class respectability.      -- Oscar Wilde (1854 - 1900)

The purpose of this document is to define a standard coding style to be used by Compiler Group at CSE department, IIT-Bombay for Garbage Collection project (and other future projects) implementation. The effort started during first week of June, 2003, it will take time to standardize and so the page will keep on changing.

Why Standard is Important?

Though at initial stages it seems like a burden to follow standard, its advantages become visible once the software grows to few thousand lines spanning few hundred files. Some of the advantages are: The problem with having standard is that it takes time to get acquainted with it. And if care is not taken during this transition period, then the resulting code will be a mix of standard and programmer's natural style. This can be avoided by having regular code review sessions.

Top

Naming Conventions

My name is Bond. James Bond.      -- Ian Fleming

It is very important to give meaningful names to all your constructs. A name like getAverageHeight() or get_avg_height() gives us much more information then calculate().
NOTE: Though the naming convention talks about global variables naming, it is for completeness sake only. A global in program should be avoided as much as possible. An unnecessary global remains unnecessary, even if it is given a very good name. Same things hold true for Macros and #define-s too.
First some general rules for naming: Here are specific rules for giving names to various constructs:

Constants, Enums and #define-s

#define constants should be avoided in favor of const. If unavoidable, then same naming scheme as for Constants and Enums should be used.

Classes

Variables

Functions

functions should be named similar to variables. We can always distinguish between the because of the parenthesis associated with functions.

Macros

Like global variables, macros should be avoided in favor of inline functions. But some times they become unavoidable (for e.g assert). Special care should be taken while defining Macros which themselves declare variables. Top

File Naming and Organization

It is not so important to know everything as to know the exact value of everything, to appreciate what we learn, and to arrange what we know.      -- Hannah More

Files should be organized into directories in a module-wise fashion instead of having a monolithic structure where all source code files and all header files are in a single directory. This should be part of design process.

File suffix should be used to distinguish file type:

Though it is possible to have alternative suffixes for some of the file types (for example .C, .cc etc for C++), there is no reason for preferring one over other, as long as everyone uses the same suffix.

Compilation should be done using make through rules defined in Makefile.

Header files are for declarations. They should NOT contain any definitions of variables,functions, and methods.

A source file should NEVER be #include-d in other file. Source files are meant to be compiled separately. make utility depends on it for saving recompilation time.

Top


Formatting and Indentation

When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong.      -- R. Buckminster Fuller (1895 - 1983)

This list is in no way complete, things will be added as and when seemed necessary. If something is not described here then there is one final rule for it: MAKE IT READABLE.

Top

Comments and Documentation

Comments are free but facts are sacred.      -- Charles Prestwich Scott

(Note: Here I am not talking about the documentation as in design-document or user-guide, but documents which explain working of some part of the code and passed among generation of developers to understand the source code.)

Comments and Documentation are aids to understand the code. They help us in following the program flow, and skip parts for which we are not interested in details.

If one is not careful, then they become rewriting of what is already written in the code. So, they have same problems as duplication of source code: consistency, maintenance etc. These should be minimized by making the code self explanatory.

Of course, there are cases when comments/documentation is needed. No one is expected to know each and every details of all modules of a complex system. In such cases comments/documentation should be used to provide an overview of the system. Also, they can be used to record other useful informations - e.g. bug-id, kludge, reference etc., and to keep track of sequence of events which lead to a particular design decision.

Some situations where comments are required are explained below.



Top

Classes

The loftier the building, the deeper must the foundation be laid.      -- Thomas Kempis



Top

Functions

Success is more a function of consistent common sense than it is of genius.      -- An Wang

The points mentioned here also apply class methods, macros also unless explicitly mentioned otherwise.

Top

Using STL

Libraries are not made; they grow.      -- Augustine Birrell

This section summarizes points to be taken care of while using Standard template Library (STL).

Top

Pointers vs References

You will find it a very good practice always to verify your references sir.      -- Martin Routh

If coding in C++, encourage use of references instead of pointers. Infact a pointer should typically be passed to a function only in cases where you need to execute something on the pointer being null condition.

Top

Minimizing Bugs while Coding

If debugging is the art of removing bugs, then programming must be the art of inserting them.      -- Unknown

Being a little careful while coding makes it easy to minimize bugs in the code. Though there are no fixed rules to achieve it, some tips are given below.

Top

Minimizing Bugs by Testing

What we have to do is to be forever curiously testing new opinions and courting new impressions.      -- Walter Pater

Testing is an integral part of software development. Tests help us not only in making sure that what we have written is correct, but also in finding out if someone breaks the code later.

Tests are written either by the programmer himself, or by someone in QA (Quality Assurance) team - one who knows only about the working of the software, but does not have any knowledge about the implementation. Second type of testing is called black-box testing.

Both types of testing have their own advantages and disadvantages. A person who has knowledge about the program can read the code, and construct a test-case about the part which is not handled properly. But, at the same time he gets a little biased about the description written in the code. QA person writes random test hoping to catch missing/incorrectly implemented feature. He has nothing but functional specifications and his own experience to guide him. But this result in testcase which are not biased by impleentation. For e.g., programmer tend to write testcase about valid inputs only, because they are more interested in the working of their algorithm. While QA people try to write testcases about error conditions to see if the errors are handled in a satisfactory way.

Here we are more interested in the testing done by programmer himself, as there in no seperate QA team. Some important points to keep in mind are:



Top

Miscellaneous

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.      -- Rich Cook

Some of the processes which help in developing good software are:

Top

References

The man who doesn't read good books has no advantage over the man who can't read them.      -- Mark Twain (1835 - 1910)

  1. The Elements of Programming Style, Brian W. Kernighan and P. J. Plauger.
  2. The Practice of Programming, Brian W. Kernighan and Rob Pike.
  3. Effective C++, Scott Meyers.
  4. More Effective C++, Scott Meyers.
  5. Code Complete, Steve McConell.
  6. Writing Solid Code, Steve Maguire.
  7. C++ Coding Standard


Top

Valid HTML 4.01!

Last updated by $Author: karkare $ on $Date: 2005/05/29 17:48:00 $