Monday, February 18, 2008

Set of coding guidelines

Over the years coding teaches you to set certain guidelines about various aspects of coding. Some of this is about style, some of them is about naming etc...

In this post I will try to list my own guidelines, and I will update them as I add new guidelines or change my idea.

Use PascalCase for class names. e.g. String, Environment, etc...

For data fields of classes use camelCase, e.g. counter, length, numOfItems

For private and protected data fields also use an underscore "_" at the end of the name , e.g. counter_, length_ , numOfItems_

In general, to get a value of a data field, use get_FieldName. e.g. get_Length(), get_NumOfItems()

There could be exceptions: e.g. length(), size()

Static member functions should be PascalCase e.g. Convert::ToInt(...)

Always call static member functions through classes, not through objects. e.g.
Convert::ToInt(...)

Always use Exception suffix in exception class names. e.g. FileFormatException, NetworkException

Use PascalCase to name namespaces. e.g. System

For acronyms that is 2 letters, use upper case, e.g. IO, instead of io, but when the acronym is more than 2 letters use PascalCase. e.g. Html

For parameters use camelCase, e.g. void f ( int sizeOfArray) ;

Put the public interface of a class first, and later protected and private interface. e.g.
class X {
public:
//public interface
protected:
//protected interface
private:
//private interface
}

If necessary, you can use a different header file for public consumption vs. private use, though this might be more difficult to maintain, especially if the class is not very stable yet.

No comments: