Title: Elephant Retirement Camp in the vicinity of Chiang Mai, Source: own resources, Authors: Agnieszka and Michał Komorowscy
In the post Nuget in C++ rulez I wrote that I returned to programming in C++. It is like a new world for me but it's better and better. I'm even reminding myself things that I learned many years ago so it's not bad with me ;) Recently, I've discovered a C++ alternative for .NET StyleCop. StyleCop is a tool that analyses C# code in order to check if it is consistent with given rules and good practices. What is obvious there is a similar thing for C++ I'm talking about a tool called CppLint that was created by Google. It's written in Python and is fairly easy in use. However, please note that CodeLint requires the old Python 2.7. I tried and it won't work with Python 3.5.
When I run CppLint on my code it turned out that my habits from C# don't fit to C++ world according to Google. Here is an example of Hello Word written in C++ but in C# style.
#include <iostream> namespace sample { class HelloWorld { public: void Fun() { std::cout << "Hello World Everyone!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; } }; } int main() { sample::HelloWorld hw = sample::HelloWorld(); hw.Fun(); return 0; }If we verify this code, we will get the following errors:
a.cpp:0: No copyright message found. You should have a line: "Copyright [year] <Copyright Owner>" [legal/copyright] [5] a.cpp:3: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] a.cpp:4: { should almost always be at the end of the previous line [whitespace/braces] [4] a.cpp:5: Do not indent within a namespace [runtime/indentation_namespace] [4] a.cpp:6: { should almost always be at the end of the previous line [whitespace/braces] [4] a.cpp:7: public: should be indented +1 space inside class HelloWorld [whitespace/indent] [3] a.cpp:9: { should almost always be at the end of the previous line [whitespace/braces] [4] a.cpp:10: Lines should be <= 80 characters long [whitespace/line_length] [2] a.cpp:13: Namespace should be terminated with "// namespace sample" [readability/namespace] [5] a.cpp:16: { should almost always be at the end of the previous line [whitespace/braces] [4] a.cpp:19: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] a.cpp:21: Could not find a newline character at the end of the file. [whitespace/ending_newline] [5]At the beginning of each line we have the line number where an error was detected. The number in square brackets at the end of each line informs you how confident CppLint is about each error i.e. 1 - it may be a false positive, 5 - extremely confident. In order to fix all these problems I did the following things:
- Added Copyright 2016 Michał Komorowski.
- Removed whitespaces at the end of lines.
- Added a new line at the end of file.
- Added a comment // namespace sample
- Move curly braces. This one I don't like the most.
- Break a too long line. It's also a little bit strange to me. 80 doesn't seem to be a lot. However, shorter lines makes working with multiple windows easier (see also this answer).
a.cpp:6: Do not indent within a namespace [runtime/indentation_namespace] [4] a.cpp:7: public: should be indented +1 space inside class HelloWorld [whitespace/indent] [3]I also fixed them and the final version of Hello Worlds compliant with Google rules looks as follows: Here is the correct version:
// Copyright 2016 Michal Komorowski #include <iostream> namespace sample { class HelloWorld { public: void Fun() { std::cout << "Hello World Everyone!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; } }; } // namespace sample int main() { sample::HelloWorld hw = sample::HelloWorld(); hw.Fun(); return 0; }It's worth adding that CppLint has many configuration options for example you can disable some rules if you don't agree with them or change the maximum allowed length of a line (default is 80). Options can be also read from the configuration file CPPLINT.cfg.