{"id":3628,"date":"2017-08-27T06:20:55","date_gmt":"2017-08-27T06:20:55","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=3628"},"modified":"2017-08-22T06:23:55","modified_gmt":"2017-08-22T06:23:55","slug":"code-complete-2-steve-mcconnell-high-quality-routines","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-high-quality-routines\/","title":{"rendered":"Code Complete 2 &#8211; Steve McConnell &#8211; High-Quality Routines"},"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<\/ul>\n<p>Routines are used to:<\/p>\n<ul>\n<li>reduce complexity<\/li>\n<li>introduce an intermediate, understandable abstraction<\/li>\n<li>avoid duplicate code<\/li>\n<li>hide sequences<\/li>\n<li>hide pointer operations<\/li>\n<li>improve portability<\/li>\n<li>simplify complicated boolean tests and<\/li>\n<li>improve performance<\/li>\n<\/ul>\n<h2>Functional cohesion<\/h2>\n<p>Functional cohesion is when a function performs <strong>one and only one<\/strong> operation.<\/p>\n<p>Cohesions that are considered to be less than ideal:<\/p>\n<ul>\n<li><strong>Sequential cohesion<\/strong>\n<ul>\n<li>exist when a routine contains operations that must be performed in a specific order, that share data from step to step.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Communicational cohesion<\/strong>\n<ul>\n<li>occurs when operations in a routine make use of the same data and aren&#8217;t related in any other way.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Temporal cohesion<\/strong>\n<ul>\n<li>occurs when operations are combined into a routine because they are all done at the same time. Think of temporal routines as organizers of other events.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Unacceptable kinds of cohesion:<\/p>\n<ul>\n<li>Procedural cohesion<\/li>\n<li>Logical cohesion<\/li>\n<li>Coincidental cohesion<\/li>\n<\/ul>\n<h2>Good routine names and length<\/h2>\n<blockquote><p>\n  Research shows that the optimum average length for a variable name is 9-15 characters and <strong>15-20<\/strong> for a routine name.\n<\/p><\/blockquote>\n<p>To name the function use the <em>description of the return value<\/em>. In object-oriented languages, you don&#8217;t need to include the name of the object in the procedure name because the object itself is included in the call.<\/p>\n<p>Avoid names like: <code>document.PrintDocument()<\/code>.<\/p>\n<p>A large percentage of routines in object-oriented programs will be accessor routines, which will be very short. From time to time, a complex algorithm will lead to a longer routine, and in those circumstances, the routine should be allowed to grow organically up to <strong>100 &#8211; 200<\/strong> lines.<\/p>\n<p>Don&#8217;t write routines longer than about <strong>200 lines<\/strong>.<\/p>\n<h2>How to use routine parameters<\/h2>\n<p>Interfaces between routines are some of the most error-prone areas of the program. Here are a few guidelines for minimizing interface problems:<\/p>\n<ul>\n<li>Put parameters in input-modify-output order<\/li>\n<li>If several routines use similar parameters, put the similar parameters in a consistent order<\/li>\n<li>Use all the parameters<\/li>\n<li>Put status or error variables last<\/li>\n<li>Limit the number of a routine&#8217;s parameters to about seven<\/li>\n<\/ul>\n<blockquote><p>\n  Psychological research has found that people generally cannot keep track of more than about seven chunks of information at once. (Miller 1956.)\n<\/p><\/blockquote>\n<p>The difference between function and procedure is that procedure doesn&#8217;t return a value.<\/p>\n<p>You should use macro routines with care and only as a last resort.<\/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":3572,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-3628","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\/3628","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=3628"}],"version-history":[{"count":1,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3628\/revisions"}],"predecessor-version":[{"id":3629,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3628\/revisions\/3629"}],"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=3628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=3628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=3628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}