Coding guidelines
This is a list of guidelines that you should check your code
against before submitting each assignment.
- Use meaningful variable names, but try to keep them short. Use a comment to describe the purpose of the variable if there is any question.
- Never use a variable for two different purposes.
- Avoid duplicate code. Either condense it somehow or
write a function.
- Keep variables in as local scope as possible. Avoid global variables. Declare variables at initialization.
- Make variables const when possible.
- Use comment blocks rather than commenting every line.
- Avoid comments that state the obvious.
- Make comments meaningful.
- Always enclose the body of control structures (e.g. if, for, etc) in
braces even if there is only one line in the body.
- Prefer for loops over while loops. Always use a for loop when
an iteration variable is used and it can be kept in local scope.
- Prefer passing non-primitive data types as const reference
parameters.
- Prefer having only one return statement per function.
- Avoid using break in loops.
- Use val->a() rather than (*val).a() when using iterators and pointers.
- Use val[i] rather than *(val+i) when using arrays.
- Keep class data members private.
- Never return a non-const reference to a class data member.