{"id":5789,"date":"2019-10-10T18:25:35","date_gmt":"2019-10-10T18:25:35","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-naming-and-layout-rules\/"},"modified":"2023-06-26T10:00:07","modified_gmt":"2023-06-26T10:00:07","slug":"c-core-guidelines-naming-and-layout-rules","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-naming-and-layout-rules\/","title":{"rendered":"C++ Core Guidelines: Naming and Layout Rules"},"content":{"rendered":"<p>The C++ core guidelines have about twenty naming and layout rules. A few of them are obvious; a few of them may be controversial. Let&#8217;s see what I mean.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5788\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/10\/mistake-968334_1280.jpg\" alt=\"mistake 968334 1280\" width=\"500\" height=\"333\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/10\/mistake-968334_1280.jpg 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/10\/mistake-968334_1280-300x200.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/10\/mistake-968334_1280-1024x682.jpg 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/10\/mistake-968334_1280-768x512.jpg 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#S-naming\">Naming and Layout Rules<\/a><\/h2>\n<p>First of all, consistency is more important than these naming and layout rules. With this in mind, here is an overview of the rules.<\/p>\n<ul>\n<li>NL.1: Don\u2019t say in comments what can be clearly stated in the code<\/li>\n<li>NL.2: State intent in comments<\/li>\n<li>NL.3: Keep comments crisp<\/li>\n<li>NL.4: Maintain a consistent indentation style<\/li>\n<li>NL.5: Don\u2019t encode type information in names<\/li>\n<li>NL.7: Make the length of a name roughly proportional to the length of its scope<\/li>\n<li>NL.8: Use a consistent naming style<\/li>\n<li>NL.9: Use ALL_CAPS for macro names only<\/li>\n<li>NL.10: Avoid CamelCase<\/li>\n<li>NL.11: Make literals readable<\/li>\n<li>NL.15: Use spaces sparingly<\/li>\n<li>NL.16: Use a conventional class member declaration order<\/li>\n<li>NL.17: Use K&amp;R-derived layout<\/li>\n<li>NL.18: Use C++-style declarator layout<\/li>\n<li>NL.19: Avoid names that are easily misread<\/li>\n<li>NL.20: Don\u2019t place two statements on the same line<\/li>\n<li>NL.21: Declare one name (only) per declaration<\/li>\n<li>NL.25: Don\u2019t use void as an argument type<\/li>\n<li>NL.26: Use conventional const notation<\/li>\n<\/ul>\n<p>I will not write about the naming and layout rules of the C++ core guidelines, which already have sufficient explanations. I only write about the rules, which need additional wording or are often discussed in my seminars.<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rl-comments\">NL.1: Don\u2019t say in comments what can be clearly stated in the code<\/a><\/h3>\n<p>I&#8217;m not a friend of commenting on each piece of code. Commenting on each piece of code may be a code smell because you code it too sophisticated. I&#8217;m more with the Python rule: explicit is better than implicit. I only write a comment if I have to apply a trick that is not apparent. For example, young professionals tend to remove curly braces from their code, such as you would remove redundant round braces from an arithmetic expression. If you don&#8217;t believe me, visit my seminars. But curly braces are essential to scope <a href=\"https:\/\/www.modernescpp.com\/index.php\/garbage-collectio-no-thanks\">RAII<\/a> objects such as locks or smart pointers. Removing a curly brace from a lock may give you a slower program or a deadlock. In the end, many of my customers comment on using curly braces.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">std<span style=\"color: #555555;\">::<\/span>mutex mut;\r\n{   <span style=\"color: #0099ff; font-style: italic;\">\/\/ necessary to manage the lifetime of the lock<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>lock_guard<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>mutex<span style=\"color: #555555;\">&gt;<\/span> lock(mut);\r\n    ...\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>What is terrible about comments is that they become out of date. By definition, this is not possible for the source code. It&#8217;s always up to date.&nbsp; As a freshman, my job was quite often to refactor legacy code. I often had no clue what it does, and I was pretty frustrated. To my rescue, I found a few comments. But the comments were completely outdated. It took me a while to recognize this; you can&#8217;t imagine how I felt. Comments have to be maintained as code but are often very sloppy maintained.<\/p>\n<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rl-name-type\">NL.5: Don\u2019t encode type information in names<\/a><\/h3>\n<p>Really? I thought since the last millennium, we were done with Hungarian Notation. I mean these <em>good old times<\/em> in which our variables had no type. <a href=\"https:\/\/en.wikipedia.org\/wiki\/Hungarian_notation\">Hungarian Notation&nbsp;<\/a>is redundant with the type-checking of the compiler, contradicts generic programming, and &#8211; this is my main argument &#8211; becomes outdated as comments become outdated. Can you guess for what the following variable names stand?<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">bBusy;\r\nfBusy;\r\npFoo;\r\nszLastName;\r\nfnFunction;\r\npszOwner;\r\nrgfpBalances;\r\nlpszBar;\r\ng_nWhells;\r\nm_whells;\r\n_whells;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>If you don&#8217;t know, here is the solution: <a href=\"https:\/\/en.wikipedia.org\/wiki\/Hungarian_notation\">Hungarian Notation. <\/a><\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rl-name-length\">NL.7: Make the length of a name roughly proportional to the length of its scope<\/a><\/h3>\n<p>This rule sounds strange, but we are already used to it. Giving a variable the name <span style=\"font-family: courier new, courier;\">i or j <\/span>or the name<span style=\"font-family: courier new, courier;\"> T<\/span> will immediately make the code&#8217;s intention clear: i and j are indices, and T is a template type parameter.<span style=\"font-family: courier new, courier;\"><br \/><\/span><\/p>\n<div style=\"background: #f0f3f3 none repeat scroll 0% 0%; overflow: auto; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>    <span style=\"color: #0099ff; font-style: italic;\"><\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> print(ostream<span style=\"color: #555555;\">&amp;<\/span> os, <span style=\"color: #006699; font-weight: bold;\">const<\/span> vector<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;&amp;<\/span> v)\r\n{\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;<\/span> v.size(); <span style=\"color: #555555;\">++<\/span>i)\r\n        os <span style=\"color: #555555;\">&lt;&lt;<\/span> v[i] <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>There is a meta-rule behind this rule. A name should be self-explanatory. In a short context, you get what the variable means with a glance. This self-evidence will not automatically hold for more extended contexts; therefore, you should use longer names.<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rl-order\">NL.16: Use a conventional class member declaration order<\/a><\/h3>\n<p>Okay, this is an easy but quite helpful rule.<\/p>\n<ul>\n<li>When you declare a class, use the following order: constructors, assignments, destructor before functions, and data.<\/li>\n<li>The same goes for <span style=\"font-family: courier new, courier;\">public,<\/span> <span style=\"font-family: courier new, courier;\">protected,<\/span> and <span style=\"font-family: courier new, courier;\">private:<\/span> Use the <span style=\"font-family: courier new, courier;\"><code class=\"highlighter-rouge no-highlight\">public<\/code><\/span> before <span style=\"font-family: courier new, courier;\"><code class=\"highlighter-rouge no-highlight\">protected<\/code><\/span> before <span style=\"font-family: courier new, courier;\"><code class=\"highlighter-rouge no-highlight\">private<\/code><\/span> order.<\/li>\n<li>Don&#8217;t use the access specifiers in a class more than once:<\/li>\n<\/ul>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">X<\/span> {   <span style=\"color: #0099ff; font-style: italic;\">\/\/ bad<\/span>\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> f();\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">g<\/span>();\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n};\r\n<\/pre>\n<\/div>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rl-misread\">NL.19: Avoid names that are easily misread<\/a><\/h3>\n<p>Can you read this example without any hesitation?<\/p>\n<div style=\"background: #f0f3f3 none repeat scroll 0% 0%; overflow: auto; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">if<\/span> (readable(i1 <span style=\"color: #555555;\">+<\/span> l1 <span style=\"color: #555555;\">+<\/span> ol <span style=\"color: #555555;\">+<\/span> o1 <span style=\"color: #555555;\">+<\/span> o0 <span style=\"color: #555555;\">+<\/span> ol <span style=\"color: #555555;\">+<\/span> o1 <span style=\"color: #555555;\">+<\/span> I0 <span style=\"color: #555555;\">+<\/span> l0)) surprise();<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Honestly, I often have issues when I type in a password having the number <span style=\"font-family: courier new, courier;\">0 or<\/span> the significant capital <span style=\"font-family: courier new, courier;\">O. <\/span>Two years ago; it took me quite a time to log into a server. The automatically generated password had a character <span style=\"font-family: courier new, courier;\">O. <br \/><\/span><\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rl-stmt\">NL.20: Don\u2019t place two statements on the same line<\/a><\/h3>\n<p>Let me give you two examples. Did you spot the two issues?<\/p>\n<div style=\"background: #f0f3f3 none repeat scroll 0% 0%; overflow: auto; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">*<\/span> p, p2;\r\n<span style=\"color: #007788; font-weight: bold;\">char<\/span> a <span style=\"color: #555555;\">=<\/span> <span style=\"color: #cc3300;\">'a'<\/span>;\r\np <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">&amp;<\/span>a;\r\np2 <span style=\"color: #555555;\">=<\/span> a;                              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> a <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">7<\/span>, b <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">9<\/span>, c, d <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">10<\/span>, e <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">3<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: courier new, courier;\">p2<\/span> is a<span style=\"font-family: courier new, courier;\"> char&nbsp;<\/span> (1), and <span style=\"font-family: courier new, courier;\">c<\/span> is not initialized (2).<\/p>\n<p>With C++17, we got an exception to this rule: structured binding. Structured binding allows me to declare more than one name in one declaration (line 1).<\/p>\n<p>Now, I can write the if statement with an initializer that is cleaner and more readable.<\/p>\n<div style=\"background: #f0f3f3 none repeat scroll 0% 0%; overflow: auto; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\">std<span style=\"color: #555555;\">::<\/span>map<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>,std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> myMap;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">if<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> [iter, succeeded] <span style=\"color: #555555;\">=<\/span> myMap.insert(value); succedded){  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    useResult(iter);  \r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n} \r\n<span style=\"color: #006699; font-weight: bold;\">else<\/span>{\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n} <span style=\"color: #0099ff; font-style: italic;\">\/\/ iter and succeeded are automatically destroyed            <\/span>\r\n<\/pre>\n<\/div>\n<h2>What&#8217;s next?<\/h2>\n<p>DONE! After over one hundred posts on the<a href=\"https:\/\/www.modernescpp.com\/index.php\/category\/modern-c?start=0\"> C++ core guidelines<\/a>, I have two good pieces of news for you.<\/p>\n<p>First, I write a book about the C++ core guidelines, in which I try my best to make a readable and concise story out of this precious content. My idea is to base my story on C++17 and the C++ core guidelines rules, which are essential to writing modern C++. Of course, modern C++ means, in particular, writing type-safe, bounds safe, and lifetime safe C++ code. So, stay tuned; I will start in the next few days and give you an update if appropriate.<\/p>\n<p>Second, my blog will shift to the hot topic in C++: the upcoming C++20 standard. My posts start in a breadth-first search and will end in a depth-first search. C++20 is presumably as powerful as C++11. You can, therefore, assume that I have a lot to write.&nbsp;<\/p>\n<pre class=\"moz-signature\">&nbsp;<br \/><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C++ core guidelines have about twenty naming and layout rules. A few of them are obvious; a few of them may be controversial. Let&#8217;s see what I mean.<\/p>\n","protected":false},"author":21,"featured_media":5788,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[464],"class_list":["post-5789","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-naming"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5789","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=5789"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5789\/revisions"}],"predecessor-version":[{"id":6770,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5789\/revisions\/6770"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5788"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}