{"id":3899,"date":"2018-03-12T08:58:07","date_gmt":"2018-03-12T08:58:07","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=3899"},"modified":"2018-03-12T09:00:04","modified_gmt":"2018-03-12T09:00:04","slug":"code-complete-2-steve-mcconnell-unusual-control-structures","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-unusual-control-structures\/","title":{"rendered":"Code Complete 2 \u2013 Steve McConnell \u2013 Unusual Control Structures"},"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<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-using-conditionals\">Chapter 15: Using Conditionals<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-controlling-loops\/\">Chapter 16: Controlling Loops<\/a><\/li>\n<\/ul>\n<h2>Recursion<\/h2>\n<p>For a small group of problems, recursion can produce simple, elegant solutions. For a slightly larger group of problems, it can produce simple, elegant, hard-to-understand solutions. ?<\/p>\n<p>Java Example:<\/p>\n<pre><code>void QuickSort( int firstIndex, int lastIndex, String [] names ) {\n    if ( lastIndex &gt; firstIndex ) {\n        int midPoint =  Partition( firstIndex, lastIndex, names );\n        QuickSort( firstIndex, midPoint-1, names );\n        QuickSort( midPoint+1, lastIndex, names );            \n    }\n}\n<\/code><\/pre>\n<p>In this case, the sorting algorithm chops an array in two and then calls itself to sort each half of the array. When it calls itself with a subarray that&#8217;s too small to sort-such as <em>( lastIndex &lt;= firstIndex )<\/em> &#8211; it stops calling itself.<\/p>\n<h3>Tips for Using Recursion<\/h3>\n<ul>\n<li><strong>Make sure the recursion stops<\/strong> &#8211; Check the routine to make sure that it includes a nonrecursive path.<\/li>\n<li><strong>Limit recursion to one routine<\/strong> &#8211; Cyclic recursion (A calls B calls C calls A) is dangerous because it&#8217;s hard to detect.<\/li>\n<li><strong>Keep an eye on the stack<\/strong> &#8211; With recursion, you have no guarantees about how much stack space your program uses. To prevent stack overflow, you can use safety counter and set the limit low enough or allocate objects on the heap using new.<\/li>\n<li><strong>Don&#8217;t use recursion for factorials or Fibonacci numbers<\/strong> &#8211; You should consider alternatives to recursion before using it. You can do anything with stacks and iteration that you can do with recursion. Sometimes one approach works better, sometimes the other does. Consider both before you choose either one.<\/li>\n<\/ul>\n<h2><code>goto<\/code><\/h2>\n<p>You might think the debate related to <em>gotos<\/em> is extinct, but a quick trip through modern source-code repositories like <em>SourceForge.net<\/em> shows that the <em>goto<\/em> is still alive and well and living deep in your company&#8217;s server.<\/p>\n<h3>The Argument Against <code>goto<\/code>s<\/h3>\n<p>The general argument against <code>goto<\/code>s is that code without them is higher-quality code<br \/>\n+ <strong>Dijkstra<\/strong> observed that the quality of the code was inversely proportional to the number of <code>goto<\/code>s the programmer used<br \/>\n+ Code containing <code>goto<\/code>s is hard to format<br \/>\n+ Use of <code>goto<\/code>s defeats compiler optimizations<br \/>\n+ The use of <code>goto<\/code>s leads to the violation of the principle that code should flow strictly from top to bottom<\/p>\n<h3>The Arguments for <code>goto<\/code>s<\/h3>\n<p>The argument for the <code>goto<\/code> is characterized by advocacy of its careful use in specific circumstances rather than its indiscriminate use.<\/p>\n<p>A well-placed <code>goto<\/code> can eliminate the need for duplicate code. Duplicate code leads to problems if the two sets of code are modified differently. Duplicate code increases the size of the source and executable files. The bad effects of the <code>goto<\/code> are outweighed in such a case by the risks of duplicate code.<\/p>\n<p>Good programming doesn&#8217;t mean eliminating <code>goto<\/code>s. Methodical decomposition, refinement, and selection of control structures automatically lead to <code>goto<\/code>-free programs in most cases. <strong>Achieving goto-less code is not the aim but the outcome<\/strong>, and putting the focus on avoiding <code>goto<\/code>s isn&#8217;t helpful.<\/p>\n<h3>Summary of Guidelines for Using <code>goto<\/code>s<\/h3>\n<p>Use of <code>goto<\/code>s is a matter of religion. My dogma is that in modern languages, you can easily replace nine out of ten <code>goto<\/code>s with equivalent sequential constructs.<\/p>\n<ul>\n<li>Use <code>goto<\/code>s to emulate structured control constructs in languages that don&#8217;t support them directly. When you do, emulate them exactly. Don&#8217;t abuse the extra flexibility the <code>goto<\/code> gives you.<\/li>\n<li>If <code>goto<\/code> improve efficiency, document the efficiency improvement so that <code>goto<\/code>-less evangelists won&#8217;t remove it.<\/li>\n<li>Limit yourself to one <code>goto<\/code> label per routine<\/li>\n<li>Limit yourself to <code>goto<\/code>s that go forward, not backward<\/li>\n<li>Make sure all <code>goto<\/code>s labels are used.<\/li>\n<li>Make sure a <code>goto<\/code> doesn&#8217;t create unreachable code.<\/li>\n<\/ul>\n<blockquote class=\"twitter-tweet\" data-width=\"550\">\n<p lang=\"und\" dir=\"ltr\"><a href=\"https:\/\/t.co\/AGH65l2L9F\">https:\/\/t.co\/AGH65l2L9F<\/a><\/p>\n<p>&mdash; Nikola Bre\u017enjak (@HitmanHR) <a href=\"https:\/\/twitter.com\/HitmanHR\/status\/973121345386176513?ref_src=twsrc%5Etfw\">March 12, 2018<\/a><\/p><\/blockquote>\n<p><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\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":3897,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-3899","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\/3899","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=3899"}],"version-history":[{"count":2,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3899\/revisions"}],"predecessor-version":[{"id":3901,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3899\/revisions\/3901"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/3897"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=3899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=3899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=3899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}