''Use the Force, Luke''... or Modern C++ Tools
''Use the Force, Luke''... or Modern C++ Tools
In this blog post, I'd like to show you how I could quickly improve my old project with Modern C++. Thanks to using the newest compilers and free code analys...
Join the DZone community and get the full member experience.Join For Free
In this blog post, I'd like to show you how I could quickly improve an old project with Modern C++. Thanks to using the newest compilers and free code analysis checkers, you can cover and modernize a lot of code.
If you have a terrible code like:
You can quickly come up with the issues here and how to fix them. Even the basic compiler will also tell you about a narrowing conversion error from
But how about some larger code samples? And your commercial projects at work?
In Visual Studio 2019 16.4 (but also in VS 2015 and later), there's code analysis functionality you can enable and get some meaningful insights from.
For a start, see this code:
In Visual Studio 2019, we can go to projects options and select rules that fit your needs. You can enable all of them, or focus on some smaller "profile" of the code.
When I enabled the code analysis, I got the following issues:
And later, here are the warnings for the code in the
That's quite nice! Visual Studio reported all of the essential issues.
What's more, in many cases, when a rule comes from a C++ core guideline, you can see there's a rule number mentioned at the end of the comment. You can open core guidelines and just look up the rule.
Here's the link to all of the guidelines: C++ Core Guidelines @Github.
Another thing is that the lines that are found to be suspicious are now shown with a green squiggle line, and when you move the mouse over those lines, you can get messages as tooltips:
One note: if you cannot use the latest Visual Studio, you can also have a look at Clang Power Tools that allows you to check your code with clang-tidy integration. Have a look at this website: https://clangpowertools.com/.
Ok, but I wrote that lousy code intentionally, ... can we use it on something "real"?
Checking a Larger Project
At the beginning of December 2019, I dug out one of my old projects. It's an application that visualizes sorting algorithms. I wrote it in 2005/2006 and used old C++, Win32Api, and OpenGL.
Here's the app preview:
Above, you can see a cool animation of a quick sort algorithm. The algorithm works on an array of input values and performs a single step around 30 times per second. The input data is then taken and drawn as a diagram with some reflection underneath. The green element is the currently accessed value, and the light-blue section represents the part of the array that the algorithm is working on.
While the app looks nice, It has some awful ideas in the code... so please don't blame me much :)
Looking at code that I wrote so long ago is a positive experience. Since I like the app, I decided to convert it to a VS 2019 project and then started refactoring it. For example, initially, the application didn't have the quick sort algorithm, so I implemented it and that way, I also "recalled" how to work in that system.
The application uses C++03... or something like that :), so having such a "toy" is an excellent thing, as you can experiment and modernize the code in many ways. It's not massive, but it's also not that super small (it's around 5k LOC).
Ok, back to the code analysis... can something go wrong in a project written that long time ago? Maybe I was super smart at that time, and I wrote amazing code that's still valid in 2020?
Yeah... right :)
I enabled all of the code analysis warnings... and I got 956 of them!
Let's have a look at some more substantial warnings.
The compiler can see that you don't change the variable and suggests using
For example, for code like:
It can even suggest using
constexpr for functions like:
Variables That Are Not Initialized
That's a common error in my code, unfortunately! It's easy to initialize all variables when you create a class, but then, when you add new member variables, I forgot to initialize them:
But I forgot about
Reduce the Use of Pointers
Back in 2005, I didn't know much about smart pointers, so I used
delete all the time.
Now in Modern C++ we should really avoid such code, and Visual Studio can easily find places to be updated:
And the message:
The compiler can detect issues with null pointer checks and for example reported:
So I should decide if the pointer can be null in this situation or not.
That's an easy thing, but in all the places where I used
NULL, I can now replace that with
nullptr from C++11.
There are even clang-tidy features to do that automatically.
While my code used exceptions to some extent, I didn't write consistent code in that regards. With C++11 we got
noexcept, and now, for every function, we should decide what to do.
In dozens of places the compiler reported:
For code like:
Not to mention, getters should be
Contrary to setting
noexcept for every function, sometimes we'd have to remove this specifier or consider updating the function.
For example, I got:
Modernizing Code With
Back in 2005, there was no support for
override, so when I had an interface that defined three pure virtual functions:
I had no way to express that in a derived class, so I just used:
With C++11 we can, of course, change it and mark as:
Rule of Zero
For whatever reason I defined a lot of empty destructors for my classes and the compiler reports now:
That's a classic Rule of Zero, and I should reconsider my classes — should they only expose constructors or maybe I need some extra resource handling inside?
Returning to an ancient project is fun, especially if you liked the project's idea. It's amazing how tools changed over time. With the help of a modern compiler and code analysis, I could detect a lot of issues and fix them. It's like having a colleague who does a basic code overview. Additionally, through such checks and refactoring, you can learn a lot about Modern C++.
Going back to the title of this text: You can rely on your "force", knowledge, and experience and see through the code and improve it. But you can also try modern C++ tools (Visual Studio is just one of them, but there are others on other platforms) and modernize code even easier.
The project repo: github.com/fenbf/ViAlg-Update.Back To You
- Do you use code analysis tools?
- Do you sometimes refactor old code? What tools do you use?
The core guideline checkers are installed by default in Visual Studio 2017 and Visual Studio 2019, and are available as a NuGet package for Visual Studio 2015.
Published at DZone with permission of Bartłomiej Filipek , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.