{"id":62,"date":"2024-02-04T18:00:00","date_gmt":"2024-02-04T18:00:00","guid":{"rendered":"https:\/\/permutationcity.co.uk\/bp\/?p=62"},"modified":"2024-04-24T18:14:43","modified_gmt":"2024-04-24T18:14:43","slug":"a-step-too-far","status":"publish","type":"post","link":"https:\/\/permutationcity.co.uk\/bp\/2024\/02\/04\/a-step-too-far\/","title":{"rendered":"A step too far"},"content":{"rendered":"\n<p>One of the seeds for this blog was some code that I was reviewing. It was based on a recommendation by\nSonarQube. I don&#8217;t have it to hand but it went roughly like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">    \/\/ Prepare query...\n    if (auto entry = FetchEntry(query); entry.Active()) {\n       \/\/ Use entry...\n    }<\/pre><\/div>\n\n\n\n<p>and was based on the rule:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>&#8220;if&#8221;, &#8220;switch&#8221;, and range-based for loop initializer should be used to reduce scope of variables<\/p>\n<\/blockquote>\n\n\n\n<p>The code is perfectly correct and the rule makes sense but I don&#8217;t like the result.\nIt seems just as bizarre as seeing code like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">    foo(); bar();<\/pre><\/div>\n\n\n\n<p>C++ lets you put as many statements as you like on one like but that doesn&#8217;t mean you should do it!\nSome people out there are probably nodding, some shaking their heads and others shrugging.<\/p>\n\n\n\n<p>I was going to talk about everybody having their own unwritten rules. Instead I can quote another\nSonarQube rule:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Statements should be on separate lines<\/p>\n<\/blockquote>\n\n\n\n<p>Their rule uses exactly the same foo \/ bar example that I did. It does also list some exceptional\ncircumstances but not the specific one that lead me here.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-why-is-important\">The &#8220;why&#8221; is important<\/h2>\n\n\n\n<p>Lists of rules in books, online and in automated tools are useful. They have the &#8220;what&#8221; but often don&#8217;t\nhave the &#8220;why&#8221;. I think automation in particular can end up with good rules used in a bad way.<\/p>\n\n\n\n<p>So lets think about the &#8220;why&#8221; behind these rules.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>&#8220;if&#8221;, &#8220;switch&#8221;, and range-based for loop initializer should be used to reduce scope of variables<\/p>\n<\/blockquote>\n\n\n\n<p>The advantages:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The variable doesn&#8217;t have to be considered when considering any other code.<\/li>\n\n\n\n<li>Resources connected to the variable can be freed earlier.<\/li>\n\n\n\n<li>The identifier can be safely re-used in a different scope.<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Statements should be on separate lines<\/p>\n<\/blockquote>\n\n\n\n<p>The advantages:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A single statement is easier to reason about.<\/li>\n\n\n\n<li>Unlikely to miss any statements.<\/li>\n<\/ul>\n\n\n\n<p>I already recommend using smaller functions rather than larger ones so the reduction in scope has a\nsmaller impact. Being able to re-use the variable name in a different scope might be handy but not\nessential. On the other hand, having someone miss code because it&#8217;s in a strange place seems like\na terrible idea. I want my code to be as quick and easy to understand as possible.\nFor me the first rule is nice but not as fundamental as the second rule.<\/p>\n\n\n\n<p>In the code review I recommend switching to this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">    \/\/ Prepare query...\n    auto entry = FetchEntry(query);\n    if (entry.Active()) {\n       \/\/ Use entry...\n    }<\/pre><\/div>\n\n\n\n<p>It takes an extra line but, to me, each line is simpler so it&#8217;s easier to understand and less likely\nto have a problem.<\/p>\n\n\n\n<p>I don&#8217;t know if you agree or disagree with my preference. I do want other developers to think about\nthe &#8220;why&#8221; of rules they are using so they can make a considered choice themselves.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the seeds for this blog was some code that I was reviewing. It was based on a recommendation by SonarQube. I don&#8217;t have it to hand but it went roughly like this: and was based on the rule: &#8220;if&#8221;, &#8220;switch&#8221;, and range-based for loop initializer should be used to reduce scope of variables [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[8],"class_list":["post-62","post","type-post","status-publish","format-standard","hentry","category-general","tag-standards"],"_links":{"self":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":2,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":179,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/62\/revisions\/179"}],"wp:attachment":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}