{"id":5276,"date":"2017-06-18T05:41:45","date_gmt":"2017-06-18T05:41:45","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/why-do-we-need-guidlines-for-modern-c\/"},"modified":"2017-06-18T05:41:45","modified_gmt":"2017-06-18T05:41:45","slug":"why-do-we-need-guidlines-for-modern-c","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/why-do-we-need-guidlines-for-modern-c\/","title":{"rendered":"Why do we need Guidelines for Modern C++?"},"content":{"rendered":"<p>This subjective post is based on more than 15 years of experience as a trainer for C++, Python, and software development in general. In the last years, I was responsible for the software and the team for building software for defibrillators. That included the regulatory affairs for our devices. Writing software for a defibrillator is exceptionally challenging because it can cause death or severe injury to the patient and the operator.<\/p>\n<p><!--more--><\/p>\n<p>I have a question we should answer as a C++ community.<\/p>\n<h2>Why do we need guidelines for modern C++?<\/h2>\n<p>Here are my thoughts. I&#8217;m only concentrating on three facts. Of course, there are more than three. This three represent my personal opinion.<\/p>\n<h3>C++ is inherently complicated for the novice<\/h3>\n<p>C++ is, in particular for beginners, an inherently complicated language. When you teach people C++, <strong>you should provide a set of rules that work for them in at least 90% of all use cases.<\/strong> I think about rules such as using <span style=\"font-family: courier new,courier;\">auto,<\/span> initializing with curly braces, preferring tasks over threads, or don&#8217;t touch memory.&nbsp; I more and more teach them and incorporate into my training. Therefore, we need a canon of best practices in C++. They state how you should write code.<\/p>\n<p>I&#8217;m actively searching for these rules. Therefore, I gave a few presentations including <a href=\"http:\/\/www.grimm-jaud.de\/index.php\/oeffentliche-vortraege\">10 tips<\/a>&nbsp;on how to write modern C++. Currently, I write a <a href=\"http:\/\/www.linux-magazin.de\/Ausgaben\/2016\/12\/C\">ten-part series<\/a> for the German Linux Magazine about tips. I used as a starting point the Zen of Python by Tim Peters: These are 20 aphorisms of how to write code in Python.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5275\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/06\/ZenOfPython.PNG\" alt=\"ZenOfPython\" width=\"600\" height=\"395\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/06\/ZenOfPython.PNG 685w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/06\/ZenOfPython-300x198.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>These are 20 rules for writing idiomatic code in Python. Twenty? These are only 19 rules. The last one is still missing.<\/p>\n<p>I&#8217;m not too fond of the idea that each trainer will develop rules. In a good case, the work is redundant. In the wrong case, we teach different or maybe contradicting rules.<\/p>\n<h3>C++ becomes more and more challenging for the professional programmer<\/h3>\n<p>I&#8217;m not so concerned about the sheer amount of new features we get with each new C++ standard. I&#8217;m more concerned about the new concepts modern C++ supports. These are the most challenging topics for the professional C++ programmer. <strong>You have to rethink and maybe change the way you solve problems<\/strong>. Think about event-driven programming with coroutines, lazy evaluation, infinite data structures, function composition, or range comprehensions with the ranges library. Consider contract-based programming, reflection, or more functional and mathematical concepts in modern C++. That is not all. Concepts will revolutionize templates.<\/p>\n<p>I stated that this plethora of new concepts would, in particular, overwhelm the professional programmer. He is the one who is used to solving problems with his classical tools. He will fall with a high probability into the <a href=\"https:\/\/en.wiktionary.org\/wiki\/if_all_you_have_is_a_hammer,_everything_looks_like_a_nail\">hammer-nail trap<\/a>.<\/p>\n<h3>C++ is very often used in safety-critical systems<\/h3>\n<p>In the end, I have a vital concern. In embedded software development, you often have to stick to MISRA C++. The Motor Industry Software Reliability Association published the current MISRA C++:2008 guidelines. They are based on the <a href=\"https:\/\/de.wikipedia.org\/wiki\/MISRA-C\">MISRA C<\/a> guidelines from the year 1998. Initially designed for the automotive industry, they became the de facto standard for implementing safety-critical software in the aviation, military, and medical sector. As MISRA C, MISRA C++ describes guidelines for a safe subset of C++. If you don&#8217;t know MISRA C++, here are more details: <a href=\"https:\/\/www.modernescpp.com\/index.php\/facts\">Facts<\/a>.&nbsp;<\/p>\n<p>There is a conceptual problem. We have to follow a C++ standard that is from 2008. To say it differently, guidelines are not state-of-the-art for modern software development in C++. <strong>It&#8217;s three standards (including C++17) behind. <\/strong><\/p>\n<p>I want<span><span> to <\/span>provide an example: <\/span>MISRA C++ doesn&#8217;t allow operator overloading. That is not a state of the art of writing C++. I teach in my seminars literal operators for implementing type-safe arithmetic such as<span style=\"font-family: courier new,courier;\"> auto constexpr dist = 4 * 5_m + 10_cm &#8211; 3_dm<\/span>. To implement such type-safe arithmetic, you must overload the arithmetic and literal operators for the suffixes. Here are the details of <a href=\"https:\/\/www.modernescpp.com\/index.php\/tag\/benutzerdefinierte-literale\">user-defined literals<\/a>.<strong><br \/><\/strong><\/p>\n<p>To be honest. I don&#8217;t believe MISRA C++ will ever come in lockstep with the current C++ standard. My hope is a different one.<\/p>\n<\/p>\n<h2>My hope<\/h2>\n<p>I hope that the C++ Core Guidelines I introduced in the post &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/what-is-modern-c\">What is Modern C++<\/a>&#8220;<strong> become the guidelines for Modern C++.<\/strong> That means, in particular, that the C++ Core Guidelines will replace in the long run MISRA C++ as the obligatory standard for C++ in safety-critical systems.<span id=\"transmark\"> <\/span><\/p>\n<p><span id=\"transmark\">Of course, that was an extremely subjective post. What are your thoughts? I&#8217;m curious about your comments.<\/span><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>The rules of the C++ Core Guidelines are not intended to be read as a book. They are intended to be the target for tools. But before using a tool, I want to know the details. Therefore, I will present the rules of the C++ Core Guidelines in my <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-the-philosophy\">next posts<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This subjective post is based on more than 15 years of experience as a trainer for C++, Python, and software development in general. In the last years, I was responsible for the software and the team for building software for defibrillators. That included the regulatory affairs for our devices. Writing software for a defibrillator is [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":5275,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[],"class_list":["post-5276","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5276"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5276\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5275"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}