THIS DOCUMENT
IS OUT OF
DATE
https://github.com/lefticus/cppbestpractices/blob/master/00-Table_of_Contents.md Instead!
SeeC++ Coding Standards Part 0: Automated Code Analysis
Automated analysis is the main advantage to working with a modern statically typed compiled language like C++. Code analysis tools can inform us when we have implemented an operator overload with a non-canonical form, when we should have made a method const, or when the scope of a variable can be reduced.
In short, these tools catch the most commonly agreed best practice mistakes we are making and help educate us to write better code. We will be fully utilizing these tools.
Compilers
All reasonable warning levels should be enabled. Some warning levels, such as GCC's -Weffc++
warning mode can be too noisy and will not be recommended for normal compilation.
GCC / Clang
A good combination of settings is -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic
-Wall -Wextra
: reasonable and standard-Wshadow
: warn the user if a variable declaration shadows another with the same name in the same scope-Wnon-virtual-dtor
: warn the user if a class with virtual functions has a non-virtual destructor. This can lead to hard to track down memory errors-pedantic
: warn about non-portable code, C++ that uses language extensions.
MSVC
MSVC has fewer warning options, so all warnings should be enabled: /W4
. /Wall
could be considered, but does not seem to be recommended even by microsoft.
Static Analyzers
Static analyzers look for errors that compilers do not look for, such as potential performance and memory issues.
Cppcheck
Cppcheck is free and opensource. It strives for 0 false positives and does a good job at it. Therefor all warning should be enabled: -enable=all
Clang's Static Analyzer
Clang's analyzer's default options are good for the respective platform. It can be used directly from cmake.
MSVC's Static Analyzer
Can be enabled with the /analyze
command line option. For now we will stick with the default options.
Code Coverage Analysis
A coverage analysis tool shall be run when tests are executed to make sure the entire application is being tested. Unfortunately, coverage analysis requires that compiler optimizations be disabled. This can result in significantly longer test execution times.
The most likely candidate for a coverage visualization is the lcov project. A secondary option is coveralls, which is free for open source projects.
Ignoring Warnings
If it is determined by team consensus that the compiler or analyzer is warning on something that is either incorrect or unavoidable, the team will disable the specific error to as localized part of the code as possible.
Unit Tests
There should be a test enabled for every feature or bug fix that is committed. See also "Code Coverage Analysis."
This comment has been minimized.
Very nice and detailed, thank you.