-
Notifications
You must be signed in to change notification settings - Fork 1
Guidelines
Landkeks edited this page Feb 12, 2012
·
7 revisions
- Kernel-style (exceptions?)
- Initialize all members in the initialization lists of your constructors.
- Make constructors with exactly one argument explicit whenever an implicit conversion does not make sense (i.e. almost always).
- Define a copy constructor and overload the assignment operator for every object. Make them private if you don't want to provide an actual implementation or if copying does not make sense.
- Use forward declaration whenever possible to increase compiling speed. Move includes to the implementation file wherever possible.
- Declare as many variables and functions as possible as const. Since STL containers do not support const members (members have to be assignable), consider making them const themselves (which will make them return const objects) or working around that with a pointer.
- Document whenever a class whose constructors/setters take a pointer will assume it owns the passed object (and delete it at some point) and document whether returned pointers should be destroyed by the client.
- Unless you have a good reason to use pointers (i.e. unless you want to transfer ownership, explicitly allow null values or explicitly allow the pointer to be re-assigned), use references (preferrably const).
- Encapsulate resource allocation in Utility classes (RAII). This ensures that resources are freed by the destructor under all circumstances (exceptions etc.).
- Throw exceptions by value and catch them by reference (preferrably const).
- Use the new nullptr keyword (C++11) instead of 0 or NULL.
- Use strongly typed enums (C++11).
- Use C++11's language features to avoid unnecessary deep copies (rvalue references, move constructors).
- Use the new range-based for loop (C++11).
- Don't include unnecessary header files, especially not in your own header files!
- Don't import whole namespaces, import single symbols instead.
- Don't declare anything as "virtual" unless absolutely necessary (performance!)
- Don't allocate small objects on the heap, use local objects instead.
- Don't use C-style casts and avoid reinterpret_cast.
- Don't create inheritance hierarchies only for code reuse (LSP!).
- All code should compile without any warnings using g++ with -Wall -pedantic -Weffc++ (except from third-party libraries).