c++ - Guidelines to improve your code -


what guidelines follow improve general quality of code? many people have rules how write c++ code (supposedly) make harder make mistakes. i've seen people insist every if statement followed brace block ({...}).

i'm interested in guidelines other people follow, , reasons behind them. i'm interested in guidelines think rubbish, commonly held. can suggest few?

to ball rolling, i'll mention few start with:

  • always use braces after every if / else statement (mentioned above). rationale behind it's not easy tell if single statement 1 statement, or preprocessor macro expands more 1 statement, code break:
     // top of file:     #define statement dosomething(); dosomethingelse      // in implementation:     if (somecondition)         dosomething(); 

but if use braces work expected.

  • use preprocessor macros conditional compilation only. preprocessor macros can cause sorts of hell, since don't allow c++ scoping rules. i've run aground many times due preprocessor macros common names in header files. if you're not careful can cause sorts of havoc!

now on you.

a few of personal favorites:

strive write code const correct. enlist compiler weed out easy fix painful bugs. code tell story of had in mind @ time wrote -- valuable newcomers or maintainers once you're gone.

get out of memory management business. learn use smart pointers: std::auto_ptr, std::tr1::shared_ptr (or boost::shared_ptr) , boost::scoped_ptr. learn differences between them , when use 1 vs. another.

you're going using standard template library. read josuttis book. don't stop after first few chapters on containers thinking know stl. push through stuff: algorithms , function objects.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -