{"id":466,"date":"2018-02-24T23:44:47","date_gmt":"2018-02-24T23:44:47","guid":{"rendered":"http:\/\/cppdepend.com\/blog\/?p=466"},"modified":"2023-05-31T16:34:17","modified_gmt":"2023-05-31T16:34:17","slug":"cpp-standards-and-best-practices-perfect-combo","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/cpp-standards-and-best-practices-perfect-combo\/","title":{"rendered":"C++ Standards and Best Practices: The Perfect Combo"},"content":{"rendered":"<p>C++ has been stagnated for many years, and many developers were confident that the language would have the same destiny as Cobol, Fortran, and VB6.\u00a0 On the contrary and against all odds, C++ is reborn from its ashes and the new standards are importantly changing how the language is used.<\/p>\n<p>The evolution of C++ by adding many interesting features does not mean that c++98 standard was a bad standard. Indeed, it gives us many interesting features to develop all kinds of applications. We can enumerate many well-developed old C++ projects, where the code is easy to maintain and evolve.<!--more--><\/p>\n<p>For example, the Doom game developed by John Carmack is one of the well- developed C++ game using only\u00a0basic mechanism of the C++98 standard.<\/p>\n<p>Here&#8217;s a\u00a0 kind of testimonial we can find when searching for articles about Doom3:<\/p>\n<pre>I spent a bit of time going through the Doom3 source code. It\u2019s probably the cleanest and nicest looking code I\u2019ve ever seen.<\/pre>\n<p>The key of the success was not the richness of the language but by following some best practices which let the code easy to read, maintains and evolve.<\/p>\n<p>It&#8217;s true that not all the developers have the John Carmack skills and maybe they need a more powerful language which helps them\u00a0to focus on the business layer of their applications. However,\u00a0 even if you use the most powerful programming language and you adopt some bad practices the code will be very difficult to understand or maintains.<\/p>\n<p>And even with the most complicated programming language like the assembly language, we can have a nice code,\u00a0 the Prince of Persia game is the proof that keeps it simple works even when using a low-level programming language.<\/p>\n<p>Prince of Persia is originally developed by Jordan Mechner and released in 1989 for the Apple II, that represented a great leap forward in the quality of animation seen in video games.\u00a0On Apr 17, 2012 Jordan Mechner\u00a0<a href=\"https:\/\/github.com\/jmechner\/Prince-of-Persia-Apple-II\">released the source code<\/a>\u00a0of Prince of Persia. Exploring its codebase we can remark that:<\/p>\n<ul>\n<li>The naming is\u00a0easy to understand<\/li>\n<\/ul>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/pp2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1390\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/pp2.png\" alt=\"pp2\" width=\"540\" height=\"313\" \/><\/a><\/p>\n<ul>\n<li>\u00a0The code is split into many small subroutines<\/li>\n<\/ul>\n<p>The\u00a06502 assembly language is very low level, and to make the code easier to understand and maintain, the \u201cDivide and Conquer\u201d principle is applied. Indeed the code is split into many small subroutines, what makes them easy to read and maintain. Here\u2019s an\u00a0example of a small subroutine defined in its source code:<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/pp3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1391\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/pp3.png\" alt=\"pp3\" width=\"560\" height=\"306\" \/><\/a><\/p>\n<p>Back to the new C++ standards, we can have a beautiful code using all the new interesting features. On the other hand, if we follow some bad practices we can have a code not easy to understand and maintain. For example this interesting <a href=\"http:\/\/www.acodersjourney.com\/2016\/02\/c-11-auto\/\">post <\/a>talk about the overuse of the auto keyword.<\/p>\n<p>Bjarne Stroustrup and Herb Sutter are aware of\u00a0the need for some basic guidelines to write an efficient modern C++ code. They introduced few years ago the &#8220;<a href=\"https:\/\/github.com\/isocpp\/CppCoreGuidelines\/blob\/master\/CppCoreGuidelines.md\">C++ Core Guidelines<\/a>&#8220;.\u00a0 Here&#8217;s from\u00a0 their github repository the motivation behind introducing the guidelines:<\/p>\n<pre>This document is a set of guidelines for using C++ well. The aim of this document is to help people to use modern C++ effectively. By \"modern C++\" we mean C++17, C++14, and C++11. In other words, what would you like your code to look like in 5 years' time, given that you can start now? In 10 years' time?\r\n\r\nThe guidelines are focused on relatively high-level issues, such as interfaces, resource management, memory management, and concurrency. Such rules affect application architecture and library design. Following the rules will lead to code that is statically type safe, has no resource leaks, and catches many more programming logic errors than is common in code today. And it will run fast -- you can afford to do things right.\r\n<\/pre>\n<p>Having the guidelines is good but generally, it&#8217;s difficult for a developer to spend some hours to do a review manually and check these guidelines. For this reason, it&#8217;s better to use a tool to check some basic guidelines.<\/p>\n<p>clang-tidy is the tool to use, it provides many interesting <a href=\"https:\/\/clang.llvm.org\/extra\/clang-tidy\/checks\/list.html\">checks <\/a>including the check of some cppcoreguidelines rules.<\/p>\n<p>To resume it&#8217;s good to have these new C++ standards, thanks to all the actors of the C++ community who contributed to achieving this goal, it&#8217;s not really an easy task. However, without some basic best practices and guidelines the code could become quickly unreadable, not maintainable and difficult to evolve.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ has been stagnated for many years, and many developers were confident that the language would have the same destiny as Cobol, Fortran, and VB6.\u00a0 On the contrary and against all odds, C++ is reborn from its ashes and the new standards are importantly changing how the language is used. The evolution of C++ by &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/cpp-standards-and-best-practices-perfect-combo\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;C++ Standards and Best Practices: The Perfect Combo&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[45,7,206,12],"class_list":["post-466","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-best-practices","tag-c","tag-c-standards","tag-c11"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=466"}],"version-history":[{"count":21,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/466\/revisions"}],"predecessor-version":[{"id":1497,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/466\/revisions\/1497"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}