What guidelines do you follow to improve the general quality of your code? Many people have rules about how to write C++ code that (supposedly) make it harder to make mistakes. I've seen people insist that every if
statement is followed by a brace block ({...}
).
I'm interested in what guidelines other people follow, and the reasons behind them. I'm also interested in guidelines that you think are rubbish, but are commonly held. Can anyone suggest a few?
To get the ball rolling, I'll mention a few to start with:
- Always use braces after every
if
/else
statement (mentioned above). The rationale behind this is that it's not always easy to tell if a single statement is actually one statement, or a pre-processor macro that expands tomore than one statement, so this code would break:
// top of file: #define statement doSomething(); doSomethingElse // in implementation: if (somecondition) doSomething();
but if you use braces then it will work as expected.
- Use preprocessor macros for conditional compilation ONLY. preprocessor macros can cause all sorts of hell, since they don't collow C++ scoping rules. I've run aground many times due to preprocessor macros with common names in header files. If you're not carefull you can cause all sorts of havock!
now over to you: