{"id":3662,"date":"2017-10-15T05:00:56","date_gmt":"2017-10-15T05:00:56","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=3662"},"modified":"2017-09-05T23:55:08","modified_gmt":"2017-09-05T23:55:08","slug":"code-complete-2-steve-mcconnell-using-conditionals","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-using-conditionals\/","title":{"rendered":"Code Complete 2 \u2013 Steve McConnell \u2013 Using Conditionals"},"content":{"rendered":"<p>I just love Steve McConnell&#8217;s classic book <a href=\"http:\/\/amzn.to\/2vdwv5v\">Code Complete 2<\/a>, and I recommend it to everyone in the Software &#8216;world&#8217; who&#8217;s willing to progress and sharpen his skills.<\/p>\n<p>Other blog posts in this series:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-part-1-laying-foundation\/\">Part 1 (Chapters 1 &#8211; 4): Laying the Foundation<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-design-construction\/\">Chapter 5: Design in Construction<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-working-classes\/\">Chapter 6: Working Classes<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-high-quality-routines\/\">Chapter 7: High-Quality Routines<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-defensive-programming\/\">Chapter 8: Defensive programming<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-pseudocode-programming-process\/\">Chapter 9: Pseudocode Programming Process<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-general-issues-using-variables\/\">Chapter 10: General Issues in Using Variables<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-general-issues-using-variables\/\">Chapter 11: General Issues in Using Variables<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-fundemental-data-types\/\">Chapter 12: Fundemental Data Types<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-unusual-data-types\/\">Chapter 13: Unusual Data Types<\/a><\/li>\n<\/ul>\n<p>A conditional is a statement that controls the execution of other statements; execution of the other statements is &#8220;conditioned&#8221; on statements such as <code>if<\/code>, <code>else<\/code>, <code>case<\/code> and <code>switch<\/code>.<\/p>\n<h3>Guidelines for <em>if<\/em> statements<\/h3>\n<ul>\n<li>Write the normal path through the code first; then write the unusual cases<\/li>\n<li>Make sure that you branch correctly on equality &#8211; using <code>&gt;<\/code> instead of <code>&gt;=<\/code> or <code>&lt;<\/code> instead of <code>&lt;=<\/code> is analogous to making an off-by-one error.<\/li>\n<li>Put the normal case after the <code>if<\/code> rather than after the <code>else<\/code> &#8211; put the case you normally expect to process first.<\/li>\n<li>Check for reversal of the <code>if<\/code> and <code>else<\/code> clauses &#8211; a common mistake in programming <code>if-else<\/code> statements is to flip-flop the code that&#8217;s supposed to follow the <code>if<\/code> clause and the code that&#8217;s supposed to follow the <code>else<\/code> clause or to get the logic of <code>if<\/code> test backward.<\/li>\n<\/ul>\n<h3>Guidelines for <em>if-else if<\/em> statements<\/h3>\n<ul>\n<li>Simplify complicated tests with boolean function calls &#8211; To improve readability, you can replace them with calls to boolean functions, here&#8217;s example without boolean function calls.\n<pre><code>if ( \n    ('a' &lt;= inputCharacter &amp;&amp; inputCharacter &lt;= 'z') || \n    ('A' &lt;= inputCharacter &amp;&amp; inputCharacter &lt;= 'Z')    \n    ) {\n    \/\/ do something...\n}\n<\/code><\/pre>\n<p>and here is the code simplified:<\/p>\n<pre><code>if ( isLetter( inputCharacter) ) {\n    \/\/ do something\n}\n<\/code><\/pre>\n<\/li>\n<li>Put the most common cases first &#8211; That way you minimize the amount of exception-case handling code someone has to read to find the usual cases. You improve efficiency because you minimize the number of tests the code does to find most common cases.<\/li>\n<li>Make sure that all cases are covered &#8211; Code the final _else clause with an error message or assertion to catch cases you didn&#8217;t plan for.<\/li>\n<\/ul>\n<h2>Case statements<\/h2>\n<h3>Choosing the most effective ordering of cases<\/h3>\n<p>If you have a long _case` statement that handles dozens of events in an event-driven program, the order is significant. Following are some ordering possibilities:<br \/>\n+ <strong>Order cases alphabetically or numerically<\/strong> &#8211; If cases are <strong>equally<\/strong> important, putting them in A-B-C order improves readability.<\/p>\n<ul>\n<li>\n<p><strong>Put the normal case first<\/strong> &#8211; If you have a normal case and several exceptions, put the normal case first.<\/p>\n<\/li>\n<li>\n<p><strong>Order cases by frequency<\/strong> &#8211; Put the most frequently executed cases first and the least frequently executed last.<\/p>\n<\/li>\n<\/ul>\n<h3>Tips for using <em>case<\/em> statements<\/h3>\n<ul>\n<li>\n<p><strong>Keep the actions of each case simple<\/strong> &#8211; short code following each case helps make the structure of the <code>case<\/code> statement clear. If actions performed for a case are complicated, write a routine and call the routine from the case rather than putting the code into the case itself.<\/p>\n<\/li>\n<li>\n<p><strong>Use the default clause only to detect legitimate defaults<\/strong> &#8211; you might sometimes have only one case remaining and decide to code that case as the default clause. Though sometimes tempting, that&#8217;s dumb. You lose the automatic documentation provided by <em>case<\/em>-statement labels, and you lose the ability to detect errors with the default clause.<\/p>\n<\/li>\n<li>\n<p><strong>Use the default clause to detect errors<\/strong> &#8211; if the default clause is used for some purpose other than error detection, the implication is that every case selector is correct.<\/p>\n<\/li>\n<li>\n<p><strong>Avoid dropping through the end of a case statement<\/strong> &#8211; languages like C, C++, and Java don&#8217;t automatically break out of each case. If you don&#8217;t code the end of the case, the program drops through the end and executes the code for the next case.<\/p>\n<\/li>\n<li>\n<p><strong>Clearly and unmistakably identify flow-throughs at the end of a case<\/strong> &#8211; if you intentionally write code to drop through the end of a case, clearly comment the place at which it happens and explain why it needs to be coded that way. This technique should be avoided.<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I just love Steve McConnell&#8217;s classic book Code Complete 2, and I recommend it to everyone in the Software &#8216;world&#8217; who&#8217;s willing to progress and sharpen his skills.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":3572,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-3662","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=3662"}],"version-history":[{"count":1,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3662\/revisions"}],"predecessor-version":[{"id":3663,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3662\/revisions\/3663"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/3572"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=3662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=3662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=3662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}