Coding Standards
Coding Standards
Table of Contents
1. Naming Conventions........................................................................................................................2
2. Indentation and Spacing..................................................................................................................2
3. Good Programming practices..........................................................................................................4
4. Comments.......................................................................................................................................5
Page 1 of 5
1. Naming Conventions
1. Camel Casing: First characters of all words, except the first word are Upper Case and other
characters are lower case. Use Camel casing for methods and variables names. For example,
int nAge;
Good:
string address
int salary
Not Good:
string addr
int sal
5. Do not use single character variable names like i, n, s etc. Use names like index, temp. One
exception in this case would be variables used for iterations in loops:
Page 2 of 5
}
1. Use TAB for indentation. Do not use SPACES. Define the Tab size as 4.
2. Curly braces ( {} ) should be in the same level as the code outside the braces.
Good:
int main()
{
double one, two;
traceMe(one, two);
traceMe(two, one);
return 0;
}
Not Good:
int main()
{
double one, two;
cout << "Enter two numbers: ";
cin >> one >> two;
cout << endl;
traceMe(one, two);
traceMe(two, one);
return 0;
}
Page 3 of 5
4. The curly braces should be on a separate line and not in the same line as if, for etc.
Good:
Not Good:
5. Use a single space before and after each operator and brackets.
Good:
Not Good:
if (hours>40.0)
{
…
}
1. Avoid writing very long methods. A method should typically have 1~25 lines of code. If a method
has more than 25 lines of code, you must consider re factoring into separate methods.
2. Method name should tell what it does. Do not use misleading names. If the method name is
obvious, there is no need of documentation explaining what the method does.
Good:
Page 4 of 5
{
// Save the phone number.
}
Not Good:
4. Comments
Good and meaningful comments make code more maintainable. However,
1. Do not write comments for every line of code and every variable declared.
2. Use // or /// for comments. Avoid using /* … */
3. Write comments wherever required. But good readable code will require very less comments. If
all variables and method names are meaningful, that would make the code very readable and
will not need many comments.
4. Do not write comments if the code is easily understandable without comment. The drawback of
having lot of comments is, if you change the code and forget to change the comment, it will lead
to more confusion.
5. Fewer lines of comments will make the code more elegant. But if the code is not clean/readable
and there are less comments, that is worse.
6. If you have to use some complex or weird logic for any reason, document it very well with
sufficient comments.
7. The bottom line is, write clean, readable code such a way that it doesn't need any comments to
understand.
Page 5 of 5