{"id":110,"date":"2005-10-25T18:16:45","date_gmt":"2005-10-25T08:16:45","guid":{"rendered":"http:\/\/www.somethinkodd.com\/oddthinking\/?p=110"},"modified":"2007-04-10T21:57:33","modified_gmt":"2007-04-10T11:57:33","slug":"obvious-coding-style-observation","status":"publish","type":"post","link":"https:\/\/www.somethinkodd.com\/oddthinking\/2005\/10\/25\/obvious-coding-style-observation\/","title":{"rendered":"Obvious Coding Style Guidelines"},"content":{"rendered":"<p>Are you a programmer? Care to do a quick review of a completely artificial example of some code? The programming language isn&#8217;t important &#8211; I am just making a point.<\/p>\n<pre><code>function max_squared(number: integer; no : integer)\r\n    \/* returns square of the maximum of the two parameters *\/\r\n{\r\n    integer num;\r\n\r\n    if (no &gt; number)\r\n    {\r\n        num = no;\r\n    }\r\n    else\r\n    {\r\n        num = number;\r\n    }\r\n\r\n    return sqr(num);\r\n}\r\n<\/code><\/pre>\n<p>So what&#8217;s wrong with that code? Well, I expect you can come up with lots of issues, but there is one that stands out.<\/p>\n<p>Some of you will prefer a different brace-style and\/or indenting. <\/p>\n<p>Some of you will prefer that the <code>num<\/code> variable was initialised the moment that it was defined.<\/p>\n<p>Some of you would prefer the function was called <code>maxSquared<\/code> or <code>Max_Squared<\/code>.<\/p>\n<p>Some of you might recommend that we check the programming language to see if it already has a <code>max<\/code> function.<\/p>\n<p>However, I hope all of you complained that having three different variables, <code>num<\/code>, <code>no<\/code> and <code>number<\/code> was confusing. <\/p>\n<p>It certainly confused me. While writing the first draft of this code, I slipped up and returned the wrong one!<\/p>\n<p>It is a common coding standard\/guideline to avoid similar sounding or appearing names for variables in the same scope. <\/p>\n<p><em>(Why am I going on about silly errors in coding style? Bear with me. I&#8217;m building up a framework for an argument. I&#8217;ll come back to this later on.)<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>An example of an obviously poor coding style<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"","footnotes":""},"categories":[29,21,34],"tags":[],"class_list":["post-110","post","type-post","status-publish","format-standard","hentry","category-influencing-others","category-observation","category-software-development"],"_links":{"self":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/comments?post=110"}],"version-history":[{"count":0,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/110\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/media?parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/categories?post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/tags?post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}