{"id":1842,"date":"2021-03-11T18:45:22","date_gmt":"2021-03-11T22:45:22","guid":{"rendered":"https:\/\/blog.wholetomato.com\/?p=1842"},"modified":"2021-03-12T08:52:18","modified_gmt":"2021-03-12T12:52:18","slug":"how-to-modernize-c-code-with-visual-assist-in-five-easy-steps","status":"publish","type":"post","link":"https:\/\/stage.wholetomato.com\/blog\/how-to-modernize-c-code-with-visual-assist-in-five-easy-steps\/","title":{"rendered":"How to Modernize C++ Code with Visual Assist in Five Easy Steps"},"content":{"rendered":"\n<p>You probably know that over time our projects seem to get old and legacy. Code written now might look suspicious five years later. With the standardization of C++11 in 2011, developers coined the term Modern C++. In this article (and the next one) we&#8217;ll take a look at some techniques you might apply to get nicer code and be closer to the best feature that recent versions of C++ offer.&nbsp;<\/p>\n\n\n\n<p>Let&#8217;s start!&nbsp;<\/p>\n\n\n\n<p><strong>1. Rename and Aim for Meaningful Names<\/strong><strong> <\/strong>You might be surprised by the first point on our list.&nbsp;<\/p>\n\n\n\n<p>Is rename added to C++11 as a feature?&nbsp;<\/p>\n\n\n\n<p>No, it\u00e2\u20ac\u2122s definitely not just an element of C++. In fact, having good names is not a feature of any programming language, as it depends on your style. The compiler can take any names, including single letters and even almost any Unicode character. Thanks to better IDE support and powerful text editors, we can now avoid shortcuts and rely on full and meaningful names.&nbsp;<\/p>\n\n\n\n<p>And what&#8217;s most important is that we can leverage extra refactoring tools, like those from Visual Assist, to fix code with the legacy style.&nbsp;<\/p>\n\n\n\n<p>Let&#8217;s have a look at the following class:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">class PBrush { \npublic: \n\tPBrush(); \n\t~PBrush(); \n\n\tbool Gen(HDC r) { } \n\tbool IsAval() const { return aval; } \n\nprivate: \n\tbool aval; \n\t\n\tHGLOBAL m_hGlobal; \n\tLPBITMAPINFO lpBitmap; \n\tLPBYTE lpBits; \n\tHDC ReferenceDC; \n\t\n\tRGBQUAD m_pal[256]; \n}; <\/pre>\n\n\n\n<p>Read the above code and think about potential naming issues. Is the code clear and descriptive? What would you change?&nbsp;<\/p>\n\n\n\n<p>Or take a look at the following function, which uses this <strong>PBrush<\/strong> objects:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">std::vector&lt;PBrush*&gt; FilterAvalBrushes(const std::vector&lt;PBrush*&gt;&amp; brs) { std::vector&lt;PBrush*&gt; o;\n\tULONG totalBCount = 0; \n\tsize_t totalBitmaps = 0; \n\t\n\tfor (size_t i = 0; i &lt; brs.size(); ++i) { \n\t\tif (brs[i]-&gt;IsAval()) { \n\t\t\to.push_back(brs[i]); \n\t\t\t++totalBitmaps; \n\t\t\ttotalBCount += brs[i]-&gt;GetByteCount(); \n\t\t} \n\t} \n\t\n\t\/\/ make sure they have some bits ready: \n\tfor (size_t i = 0; i &lt; o.size(); ++i) { \n\t\tif (!o[i]-&gt;Bits()) \n\t\t\tLog(\"ERROR, empty bitmap!\"); \n\t} \n\t\n\tLog(\"Byte count %d, total bitmaps %d\", totalBCount, totalBitmaps); return o; \n}<\/pre>\n\n\n\n<p>How could we improve the code?&nbsp;<\/p>\n\n\n\n<p>You might be tempted to say that it&#8217;s better to leave old code and not touch it since other systems might depend on it. However, adding or changing to better names and keeping it useful to other systems is very simple. In most cases, you can find and replace old names or use Rename from Visual Assist.&nbsp;<\/p>\n\n\n\n<p>Visual Assist has a powerful feature for renaming objects and making sure the code compiles after this transformation. It was also one of the earliest VA features and enabled using this refactoring capability years before Visual Studio added it.&nbsp;<\/p>\n\n\n\n<p>You can invoke the rename tool in many situations.&nbsp;<\/p>\n\n\n\n<p>The simplest way is to hit a key shortcut when your cursor is inside a particular name ( <strong>Shift + Alt + R<\/strong> by default). For example, let&#8217;s say that I&#8217;d like to rename <strong>bool aval<\/strong>, so I move my cursor there and invoke the dialog, and then I can see the following window:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/rename_dialog.png?ssl=1\"><img decoding=\"async\" width=\"893\" height=\"396\" data-attachment-id=\"1852\" data-permalink=\"https:\/\/stage.wholetomato.com\/blog\/rename_dialog\/\" data-orig-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/rename_dialog.png?fit=893%2C396&amp;ssl=1\" data-orig-size=\"893,396\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"rename_dialog\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/rename_dialog.png?fit=300%2C133&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/rename_dialog.png?fit=893%2C396&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/rename_dialog.png?resize=893%2C396&#038;ssl=1\" alt=\"\" class=\"wp-image-1852\" srcset=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/rename_dialog.png?w=893&amp;ssl=1 893w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/rename_dialog.png?resize=300%2C133&amp;ssl=1 300w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/rename_dialog.png?resize=768%2C341&amp;ssl=1 768w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/rename_dialog.png?resize=360%2C160&amp;ssl=1 360w\" sizes=\"(max-width: 893px) 100vw, 893px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>Another option is to write a new name, and then VA will show a handy context menu where you can invoke the rename or see the preview window before making the changes.&nbsp;<\/p>\n\n\n\n<p>What other things can you do with rename?&nbsp;<\/p>\n\n\n\n<p>Improve consistency of names. For example, your style guide might suggest adding <strong>m_<\/strong> before each nonstatic data member for classes. So you might quickly improve it and add this prefix for classes that don&#8217;t share this style. In our example, some names start with <strong>m_<\/strong> while others don&#8217;t.&nbsp;<\/p>\n\n\n\n<p>It&#8217;s not only variables and functions but also things like enumerations and namespaces.&nbsp;<\/p>\n\n\n\n<p>But most importantly, what name would be better for <strong>aval<\/strong>? Do you prefer <strong>m_isAvailable<\/strong> ? or Available ? I&#8217;ll leave it as an exercise.&nbsp;<\/p>\n\n\n\n<p><strong>2. Extract Smaller Functions<\/strong><strong>&nbsp;<\/strong><\/p>\n\n\n\n<p>Here&#8217;s another example to fix:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">\/\/ number of data components \nswitch ( format ) \n{ \ncase GL_RGB: \ncase GL_BGR_EXT: components = 3; break; \ncase GL_RGBA: \ncase GL_BGRA_EXT: components = 4; break; \ncase GL_LUMINANCE: components = 1; break; \ndefault: components = 1; \n} \n\nGLuint t; \nglGenTextures(1, &amp;t); \n\n\/\/ load data \nHBITMAP hBmp = (HBITMAP)LoadImage(NULL, fname, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION); \nif (hBmp == NULL) \n\treturn 0; \n\t\n\/\/ rest of code... long code<\/pre>\n\n\n\n<p>The above code is just a part of some longer function that loads, opens a file, loads bytes, and creates an OpenGL texture object with the loaded data. One thing that we might immediately do is to make this function smaller. More compact functions are easier to read and allow for more code reuse.&nbsp; We can see that it has several smaller subtasks, so it would be better to extract them into separate parts. For example, we can implement <strong>FormatToComponents<\/strong>:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method.png?ssl=1\"><img decoding=\"async\" width=\"785\" height=\"368\" data-attachment-id=\"1853\" data-permalink=\"https:\/\/stage.wholetomato.com\/blog\/extract_method\/\" data-orig-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method.png?fit=785%2C368&amp;ssl=1\" data-orig-size=\"785,368\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"extract_method\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method.png?fit=300%2C141&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method.png?fit=785%2C368&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method.png?resize=785%2C368&#038;ssl=1\" alt=\"\" class=\"wp-image-1853\" srcset=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method.png?w=785&amp;ssl=1 785w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method.png?resize=300%2C141&amp;ssl=1 300w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method.png?resize=768%2C360&amp;ssl=1 768w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method.png?resize=360%2C169&amp;ssl=1 360w\" sizes=\"(max-width: 785px) 100vw, 785px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>And when you select this context menu, you&#8217;ll get the following dialog:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method_ui.png?ssl=1\"><img decoding=\"async\" width=\"506\" height=\"275\" data-attachment-id=\"1854\" data-permalink=\"https:\/\/stage.wholetomato.com\/blog\/extract_method_ui\/\" data-orig-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method_ui.png?fit=506%2C275&amp;ssl=1\" data-orig-size=\"506,275\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"extract_method_ui\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method_ui.png?fit=300%2C163&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method_ui.png?fit=506%2C275&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method_ui.png?resize=506%2C275&#038;ssl=1\" alt=\"\" class=\"wp-image-1854\" srcset=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method_ui.png?w=506&amp;ssl=1 506w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method_ui.png?resize=300%2C163&amp;ssl=1 300w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/extract_method_ui.png?resize=360%2C196&amp;ssl=1 360w\" sizes=\"(max-width: 506px) 100vw, 506px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>After some adjustments (for example, we don&#8217;t need to take components as input argument), we can prepare the following code:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">int FormatToComponents(GLenum format) { \n\tswitch (format) { \n\tcase GL_RGB: \n\tcase GL_BGR_EXT: return 3; \n\tcase GL_RGBA: \n\tcase GL_BGRA_EXT: return 4; \n\tcase GL_LUMINANCE: return 1; \n\t} \n\treturn 1; \n} <\/pre>\n\n\n\n<p>And then call it from our initial function:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">GLuint LoadTextureFromBmp(const char *fname, GLenum format, GLuint filter) { const int components \n\t= FormatToComponents(format);  <\/pre>\n\n\n\n<p>This not only gave us shorter, cleaner code, but also we can now mark our variable as const, which even improves the quality. Additionally, this function might be reused in some other places of the system.&nbsp;<\/p>\n\n\n\n<p>Here are the general rules for the extraction by VA:&nbsp;<\/p>\n\n\n\n<p>Symbols available globally, or within the class of the original and new methods, are not passed via a parameter list.&nbsp;<\/p>\n\n\n\n<p>Symbols used only in the right side of expressions (i.e., not modified) are passed by value. Symbols used in the left side of expressions (i.e., assigned) are passed by reference. Symbols referenced with dot notation within the extracted code (e.g., classes and structs) are always passed by reference.&nbsp;<\/p>\n\n\n\n<p>When a newly created method assigns a value to only one symbol, that symbol is passed by value and the assignment is done in the original method via return value.&nbsp;<\/p>\n\n\n\n<p>Symbols local to extracted code become local to the new method; they are not passed.<\/p>\n\n\n\n<p>&nbsp;<strong>3. Improve Function Interfaces<\/strong><strong>&nbsp;<\/strong><\/p>\n\n\n\n<p>Let&#8217;s now challenge ourselves with this one:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">bool Init2d(HDC hdcContext, FontMode fm, HFONT hFont, GLuint iWidth, GLuint iHeigth, GLuint iTexture, bool forceBold, bool forceItalic, size_t numGlyphs); <\/pre>\n\n\n\n<p>It&#8217;s a member function in GLFont that is responsible for creating a texture font from a Window font. We can later use this created object to draw text in an OpenGL application.&nbsp;<\/p>\n\n\n\n<p>Isn&#8217;t it worrying that this function takes nine input arguments?&nbsp;<\/p>\n\n\n\n<p>And here&#8217;s an even more suspicious-looking call site:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">glFont.Init2d(const_cast&lt;CGLApp *&gt;(glApp)-&gt;GetHdc(), fmBitmap, hFont, iWidth, iHeight, 0, false, false, 256);<\/pre>\n\n\n\n<p>Some books suggest that if you have more than 5&#8230; or 7, then it&#8217;s some worrying code smell.&nbsp; Such long lists of function arguments are hard to read, and caller sites might easily put them in a wrong order. Another issue is that there are a few boolean parameters, and it&#8217;s hard to see what they mean from the caller perspective.&nbsp;<\/p>\n\n\n\n<p>You can use Change Signature to modify all parts of a signature, including:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Method name&nbsp;<\/li><li>Return type&nbsp;<\/li><li>Visibility&nbsp;<\/li><li>Parameter names&nbsp;<\/li><li>Parameter types&nbsp;<\/li><li>Parameter order&nbsp;<\/li><li>Number of parameters&nbsp;<\/li><li>Qualifiers&nbsp;<\/li><\/ul>\n\n\n\n<p>The plan for our function is to introduce a structure that would hold all parameters. We can do it by simply copying the parameter list and putting it into a structure above the function declaration:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">struct FontCreationParams { \n\tGLuint iWidth; \n\tGLuint iHeigth; \n\tGLuint iTexture { 0 }; \n\tbool forceBold { false}; \n\tbool forceItalic { false}; \n\tsize_t numGlyphs { 256 }; \n}; <\/pre>\n\n\n\n<p>As you can see, I even assigned some default values.&nbsp;<\/p>\n\n\n\n<p>And then we can change the signature in two steps:&nbsp;<\/p>\n\n\n\n<p>1. Add new argument to the function: const FontCreationParams&amp; param with a default value of TODO.&nbsp;<\/p>\n\n\n\n<p>2. And then, again remove those extra arguments.&nbsp;<\/p>\n\n\n\n<p>Then you might compile code and fix call sites.&nbsp;<\/p>\n\n\n\n<p>The final code might look as follows:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">FontCreationParams params; \nparams.iWidth = iWidth; \nparams.iHeigth = iHeight; \nglFont.Init2d(const_cast&lt;CGLApp *&gt;(glApp)-&gt;GetHdc(), fmBitmap, hFont, params);<\/pre>\n\n\n\n<p>With this approach, if you need some more parameters (or you need to alter them), it&#8217;s just a matter of modifying the structure, and the function declaration will be the same.&nbsp;<\/p>\n\n\n\n<p>While you can do such a change manually, Visual Assist makes it much more comfortable and takes care of most of the cases at the call site. So this significantly simplifies and automates the process when you have many function calls.&nbsp;<\/p>\n\n\n\n<p>So far, we discussed basic principles for code modernization, but how about automation? Can we use extra tools that would tell us what to change in our code?&nbsp;<\/p>\n\n\n\n<p>Let&#8217;s see the fourth point.&nbsp;<\/p>\n\n\n\n<p><strong>4. Enable Code Inspections<\/strong><strong>&nbsp;<\/strong><\/p>\n\n\n\n<p>Since a few years ago, Visual Assist has come with code Inspections that automatically check your code and pinpoint any issues.&nbsp;<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Code Inspection analyzes C\/C++ for specific code quality issues as you edit. The feature, based on LLVM\/Clang, warns you of issues in your code and if possible, suggests and applies quick fixes.&nbsp;<\/p><\/blockquote>\n\n\n\n<p>You can enable them via Settings or as shown below:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/enable_code_inspections.png?ssl=1\"><img decoding=\"async\" width=\"985\" height=\"714\" data-attachment-id=\"1855\" data-permalink=\"https:\/\/stage.wholetomato.com\/blog\/enable_code_inspections\/\" data-orig-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/enable_code_inspections.png?fit=985%2C714&amp;ssl=1\" data-orig-size=\"985,714\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"enable_code_inspections\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/enable_code_inspections.png?fit=300%2C217&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/enable_code_inspections.png?fit=985%2C714&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/enable_code_inspections.png?resize=985%2C714&#038;ssl=1\" alt=\"\" class=\"wp-image-1855\" srcset=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/enable_code_inspections.png?w=985&amp;ssl=1 985w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/enable_code_inspections.png?resize=300%2C217&amp;ssl=1 300w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/enable_code_inspections.png?resize=768%2C557&amp;ssl=1 768w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/enable_code_inspections.png?resize=360%2C261&amp;ssl=1 360w\" sizes=\"(max-width: 985px) 100vw, 985px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>You can read the full description of Inspections in the excellent documentation website Introduction to Code Inspection and also see our previous article where we dive a bit deeper A Brief Introduction to Clang-Tidy and Its Role in Visual Assist \u00e2\u20ac\u201c Tomato Soup.&nbsp;<\/p>\n\n\n\n<p>Currently,&nbsp; we have almost 30 code inspections, and the number grows with each revision: List of Code Inspections.&nbsp;<\/p>\n\n\n\n<p>They range from checking <strong>.empty()<\/strong> vs <strong>size()<\/strong> , using <strong>emplace_back()<\/strong>, <strong>nullptr<\/strong> usages, and many more. They can help not only with code modernization but even with finding some performance issues in code. It&#8217;s like a small code analysis run on the code.&nbsp;<\/p>\n\n\n\n<p>Let\u00e2\u20ac\u2122s&nbsp; take a look at one related to for loops.&nbsp;<\/p>\n\n\n\n<p><strong>5. Make For-Loops More Compact<\/strong><strong>&nbsp;<\/strong><\/p>\n\n\n\n<p>One of the coolest elements of C++11 style comes from using simpler for loops. This is especially important for code that uses containers from the Standard Library.&nbsp;<\/p>\n\n\n\n<p>For example, before C++11, you had to write:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">std::vector { }... \nfor (std::vector&lt;int&gt;::iterator it = vec.begin(); it != vec.end(); ++it) std::cout &lt;&lt; *it &lt;&lt; '\\n'; <\/pre>\n\n\n\n<p>Now, this can be simplified with this simple syntax:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">std::vector&lt;int&gt; vec { 1, 2, 3, 4, 5, 6 }; \nfor (const auto&amp; elem : vec) \n\tstd::cout &lt;&lt; elem &lt;&lt; '\\n';<\/pre>\n\n\n\n<p>Why not automate this transformation?&nbsp;<\/p>\n\n\n\n<p>VA identifies places where you can use this modern syntax and then rewrites loops for you.&nbsp;<\/p>\n\n\n\n<p>Consider this example:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">std::vector&lt;PBrush*&gt; FilterAvalBrushes(const std::vector&lt;PBrush*&gt;&amp; brushes) { \/\/ ... \n\tfor (size_t i = 0; i &lt; brushes.size(); ++i) { \n\t\tif (brushes[i]-&gt;IsAval()) { \n\t\t\tout.push_back(brushes[i]); \n\t\t\t++totalBitmaps; \n\t\t} \n\t} \n} <\/pre>\n\n\n\n<p>When code inspections are enabled, we can see the following suggestions: <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/for_loop_convert.png?ssl=1\"><img decoding=\"async\" width=\"819\" height=\"397\" data-attachment-id=\"1857\" data-permalink=\"https:\/\/stage.wholetomato.com\/blog\/for_loop_convert\/\" data-orig-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/for_loop_convert.png?fit=819%2C397&amp;ssl=1\" data-orig-size=\"819,397\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"for_loop_convert\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/for_loop_convert.png?fit=300%2C145&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/for_loop_convert.png?fit=819%2C397&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/for_loop_convert.png?resize=819%2C397&#038;ssl=1\" alt=\"\" class=\"wp-image-1857\" srcset=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/for_loop_convert.png?w=819&amp;ssl=1 819w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/for_loop_convert.png?resize=300%2C145&amp;ssl=1 300w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/for_loop_convert.png?resize=768%2C372&amp;ssl=1 768w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/for_loop_convert.png?resize=360%2C175&amp;ssl=1 360w\" sizes=\"(max-width: 819px) 100vw, 819px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>What&#8217;s nice about Visual Assist is that it&#8217;s very smart and tries to deduce the correct loop syntax.&nbsp;<\/p>\n\n\n\n<p>For example, when our vector holds pointers, then the converted loop is as follows:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">std::vector&lt;PBrush*&gt; FilterAvalBrushes(const std::vector&lt;PBrush*&gt;&amp; brushes) { \/\/ ... \n\tfor (auto brushe : brushes) { \n\t\tif (brushe-&gt;IsAval()) { \n\t\t\tout.push_back(brushe); \n\t\t\t++totalBitmaps; \n\t\t\ttotalBCount += brushe-&gt;GetByteCount(); \n\t\t} \n\t} \n} <\/pre>\n\n\n\n<p>But when we change the type into <strong>const std::vector&lt;PBrush&gt;<\/strong> (so it holds not pointer but &#8220;full&#8221; objects), then the loop is as follows:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">for (const auto &amp; brushe : brushes) { \n\tif (brushe.IsAval()) { \n\t\tout.push_back(brushe); \n\t\t++totalBitmaps; \n\t\ttotalBCount += brushe.GetByteCount(); \n\t} \n}<\/pre>\n\n\n\n<p>The system is smart and avoids situations where you&#8217;d copy objects during loop iteration, as it&#8217;s more efficient to use references.&nbsp;<\/p>\n\n\n\n<p>That&#8217;s just a taste of what you can automatically do with Visual Assist and its code inspections!&nbsp;<\/p>\n\n\n\n<p><strong>Summary<\/strong><strong>&nbsp;<\/strong><\/p>\n\n\n\n<p>In this article, you&#8217;ve seen five suggestions on how to check and improve your code. We started with something fundamental but often skipped: naming. Then we made functions smaller and with better interfaces. And at the end, we enabled code inspections so that Visual Assist can automatically identify more things to repair.&nbsp;<\/p>\n\n\n\n<p>In the last item, we looked at loop transformation, which is available from C++11. Thanks to Visual Assist, we can automate this task and quickly modernize our code to the newer standard&nbsp;<\/p>\n\n\n\n<p>After such refactoring, we aim to have safer, more readable code. What\u00e2\u20ac\u2122s more, with more expressive code, we can help the compiler to identify potential errors. This is just a start, and stay tuned for the next article where you&#8217;ll see other code modernization techniques: <strong>override<\/strong> keyword, <strong>nullptr<\/strong>, <strong>enum class<\/strong>, <strong>auto<\/strong> type deduction, and deprecated headers.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.wholetomato.com\/\" target=\"_blank\"><img decoding=\"async\" width=\"600\" height=\"200\" data-attachment-id=\"1811\" data-permalink=\"https:\/\/stage.wholetomato.com\/blog\/007_banner_wholetomato_resize_emiliano_600x200\/\" data-orig-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/01\/007_banner_wholetomato_resize_emiliano_600x200.png?fit=600%2C200&amp;ssl=1\" data-orig-size=\"600,200\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"007_banner_wholetomato_resize_emiliano_600x200\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/01\/007_banner_wholetomato_resize_emiliano_600x200.png?fit=300%2C100&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/01\/007_banner_wholetomato_resize_emiliano_600x200.png?fit=600%2C200&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/01\/007_banner_wholetomato_resize_emiliano_600x200.png?resize=600%2C200&#038;ssl=1\" alt=\"\" class=\"wp-image-1811\" srcset=\"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/01\/007_banner_wholetomato_resize_emiliano_600x200.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/01\/007_banner_wholetomato_resize_emiliano_600x200.png?resize=300%2C100&amp;ssl=1 300w, https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/01\/007_banner_wholetomato_resize_emiliano_600x200.png?resize=360%2C120&amp;ssl=1 360w\" sizes=\"(max-width: 600px) 100vw, 600px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>You probably know that over time our projects seem to get old and legacy. Code written now might look suspicious five years later. With the standardization of C++11 in 2011, developers coined the term Modern&#8230;<\/p>\n","protected":false},"author":183830964,"featured_media":1858,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_jetpack_newsletter_access":"","_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[672],"tags":[910314,2426,54971,4458826,621688,885520,78684782,318578,1788,12004844],"class_list":["post-1842","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips-and-tricks","tag-analyze","tag-c","tag-c11","tag-code-inspection","tag-extract","tag-for-loop","tag-modern-c","tag-rename","tag-tips","tag-visual-assist"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/stage.wholetomato.com\/blog\/wp-content\/uploads\/2021\/03\/enablecodedevice.jpg?fit=1920%2C1080&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pfhqii-tI","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/posts\/1842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/users\/183830964"}],"replies":[{"embeddable":true,"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/comments?post=1842"}],"version-history":[{"count":18,"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/posts\/1842\/revisions"}],"predecessor-version":[{"id":1872,"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/posts\/1842\/revisions\/1872"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/media\/1858"}],"wp:attachment":[{"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/media?parent=1842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/categories?post=1842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stage.wholetomato.com\/blog\/wp-json\/wp\/v2\/tags?post=1842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}