Always Initialize Variables
Since C++ doesnt give variables a default value, you should initialize it to avoid problems. That is, instead of just saying:
int a; double b;
You should instead:
int a = 0; double b = 0.0
Always Use Smart Pointers
Smart pointers were made to make your life easier, so why not use it? You can use boost or the one from TR1. Also smart pointers are now included in C++11.
Dont use “using namespace” in a global scope
Frankly speaking I dont really use “using namespace” even on a local scope. Namespace was made to avoid name conflicts so why take them out of their namespace and transfer them into the global space? If you really are feeling lazy to type lengthy namespaces like the one from Boost you should just use an alias:
namespace foo3 = foo1::foo2::foo3;
Use “const” Modifier
“const” will convey to the user that you dont have an intention to alter any data member or function arguments. This can also prevent some of your teammates to create codes that can affect data inside it in the future. Think of it like a contract that any parameters with const will not be changed.
Use assert() For Debugging
assert() is a handy macro for debugging. It is also easy to remove once your building for release, just define “NDEBUG”. Some do prefer unit testing but if youre maintaining a project that was made earlier by another person that doesnt have a unit test ready . Then adding asserts for the new added codes is not as difficult as making unit tests for it.20
Leave a Reply