    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: The Ghost of a Codebase Past</title>
        <link>https://members.accu.org/index.php/articles/2558</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + CVu Journal Vol 30, #4 - September 2018</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c390/">304</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-390/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+390/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;The Ghost of a Codebase Past</h1>
<p><strong>Author:</strong>&nbsp;Bob Schmidt</p>
<p>
<strong>Date:</strong> 09 September 2018 22:05:13 +01:00 or Sun, 09 September 2018 22:05:13 +01:00</p>
<p><strong>Summary:</strong>&nbsp;Pete Goodliffe learns lessons by reviewing his own old code.</p>
<p><strong>Body:</strong>&nbsp;<p class="quote">I will live in the Past, the Present, and the Future.<br />
The Spirits of all Three shall strive within me.<br />I will not shut out the lessons that they teach!<br />
~ Charles Dickens, A Christmas Carol</p>

<p>Nostalgia isnâ€™t what it used to be. And neither is your old code. Who knows what functional gremlins and typographical demons lurk in your ancient handiwork? You thought it was perfect when you wrote itâ€”but cast a critical eye over your old code and youâ€™ll inevitably bring to light all manner of code gotchas.</p>

<p>Programmers, as a breed, strive to move onwards. We love to learn new and exciting techniques, to face fresh challenges, and to solve more interesting problems. Itâ€™s natural. Considering the rapid turnover in the job market, and the average duration of programming contracts, itâ€™s hardly surprising that very few software developers stick with the same codebase for a prolonged period of time.</p>

<p>But what does this do to the code we produce? What kind of attitude does it foster in our work? I maintain that exceptional programmers are determined more by their attitude to the code they write and the way they write it, than by the actual code itself.</p>

<p>The average programmer tends not to maintain <em>their own</em> code for too long. Rather than roll around in our own filth, we move on to new pastures and roll around in <em>someone elseâ€™s</em> filth. Nice. We even tend to let our own â€˜pet projectsâ€™ fall by the wayside as our interests evolve.</p>

<p>Of course, itâ€™s fun to complain about other peopleâ€™s poor code, but we easily forget how bad our own work was. And youâ€™d never <em>intentionally</em> write bad code, would you?</p>
<p>Revisiting your old code can be an enlightening experience. Itâ€™s like visiting an ageing, distant relative you donâ€™t see very often. You soon discover that you donâ€™t know them as well as you think. Youâ€™ve forgotten things about them, about their funny quirks and irritating ways. And youâ€™re surprised at how theyâ€™ve changed since you last saw them (perhaps, for the worst).</p>

<p class="lesson">Looking back at your older code will inform you about the improvement (or otherwise) in your coding skills.</p>

<p>Looking back at old code youâ€™ve produced, you might shudder for a number of reasons.</p>

<h2>Presentation</h2>

<p>Many languages permit artistic interpretation in the indentation layout of code. Even though some languages have a <em>de facto</em> presentation style, there is still a large gamut of layout issues which you may find yourself exploring over time. Which ones stick depends on the conventions of your current project, or on your experiences after years of experimentation.</p>

<p>Different tribes of C++ programmers, for example, follow different presentation schemes. Some developers follow the standard library scheme:</p>

<pre class="programlisting">
  struct standard_style_cpp
  {
    int variable_name;
    bool method_name();
  };</pre>
    
<p>Some have more Java-esque leanings:</p>

<pre class="programlisting">
  struct JavaStyleCpp
  {
    int variableName;
    bool methodName();
  };</pre>
  
<p>And some follow a C# model:</p>

<pre class="programlisting">
  struct CSharpStyleCpp
  {
    int variableName;
    bool MethodName();
  };</pre>
  
<p>A simple difference, but it profoundly affects your code in several ways.</p>

<p>Another C++ example is the layout of member initialiser lists. One of my teams moved from this traditional scheme:</p>

<pre class="programlisting">
  Foo::Foo(int param)
  : member_one(1),
    member_two(param),
    member_three(42)
  {
  }</pre>
  
<p>to a style that places the comma separators at the beginning of the following line, thus:</p>

<pre class="programlisting">
  Foo::Foo(int param)
  : member_one(1)
  , member_two(param)
  , member_three(42)
  {
  }</pre>
  
<p>We found a number of advantages with the latter style (itâ€™s easier to â€˜knock outâ€™ parts in the middle via preprocessor macros or comments, for example). This prefix-comma scheme can be employed in a number of layout situations (e.g., many kinds of lists: members, enumerations, base classes, and more), providing a nice consistent shape. There are also disadvantages, one of the major cited issues being that itâ€™s not as â€˜commonâ€™ as the former layout style. IDEsâ€™ default auto-layout also tends to fight with it.</p>

<p>I know over the years that my own presentation style has changed wildly, depending on the company Iâ€™m working for at the time.</p>

<p>As long as a style is employed consistently in your codebase, this is really a trivial concern and nothing to be embarrassed about. Individual coding styles rarely make much of a difference once you get used to them, but inconsistent coding styles in one project make everyone slower.</p>

<h2>The state of the art</h2>

<p>Most languages have rapidly developed their in-built libraries. Over the years, the Java libraries have grown from a few hundred helpful classes to a veritable plethora of classes, with different skews of the library depending on the Java deployment target. Over C#â€™s revisions, its standard library has also burgeoned. As languages grow, their libraries accrete more features.</p>

<p>And as those libraries grow, some of the older parts become deprecated.</p>

<p>Such evolution (which is especially rapid early in a languageâ€™s life) can unfortunately render your code anachronistic. Anyone reading your code for the first time might presume that you didnâ€™t understand the newer language or library features, when those features simply did not exist when the code was written.</p>

<p>For example, when C# added generics, the code you would have written like this:</p>

<pre class="programlisting">
  ArrayList list = new ArrayList(); // untyped

  list.Add(&quot;Foo&quot;);
  list.Add(3); // oops!
</pre>

<p>with its inherent potential for bugs, would have become:</p>

<pre class="programlisting">
  List&lt;string&gt; list = new List&lt;string&gt;();
  list.Add(&quot;Foo&quot;);
  list.Add(3); // compiler error - nice
</pre>

<p>There is a very similar Java example with surprisingly similar class names!</p>

<p>The state of the art moves much faster than your code. Especially your old, untended code.</p>

<p>Even the (relatively conservative) C++ library has grown considerably with each new revision. C++ language features and library support have made much old C++ code look old-fashioned. The introduction of a language-supported threading model renders third-party thread libraries (often implemented with rather questionable APIs) redundant. The introduction of lambdas removes the need for a lot of verbose handwritten â€˜trampolineâ€™ code. The range-based <code>for</code> helps remove a lot of syntactical trees so you can see the code-design wood. Once you start using these facilities, returning to older code without them feels like a retrograde step.</p>

<h2>Idioms</h2>

<p>Each language, with its unique set of language constructs and library facilities, has a particular â€˜best practiceâ€™ method of use. These are the <em>idioms</em> that experienced users adopt, the modes of use that have become honed and preferred over time.</p>

<p>These idioms are important. They are what experienced programmers expect to read; they are familiar shapes that enable you to focus on the overall code design rather than get bogged down in macro-level code concerns. They usually formalise patterns that avoid common mistakes or bugs.</p>

<p>Itâ€™s perhaps most embarrassing to look back at old code, and see how un-idiomatic it is. If you now know more of the accepted idioms for the language youâ€™re working with, your old non-idiomatic code can look quite, quite wrong.</p>

<p>Many years ago, I worked with a team of C programmers moving (well, shuffling slowly) towards the (then) brave new world of C++. One of their initial additions to a new codebase was a <code>max</code> helper macro:</p>

<pre class="programlisting">
  #define max(a,b) ((a)&gt;(b)) ? (a) : (b))
  // do you know why we have all those brackets?

  void example()
  {
    int a = 3, b = 10;
    int c = max(a, b);
  }</pre>
  
<p>In time, someone revisited that early code and, knowing more about C++, realised how bad it was. They rewrote it in the more idiomatic C++ shown here, which fixed some very subtle lurking bugs (see Listing 1).</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
template &lt;typename T&gt;
inline T max(const T &amp;a, const T &amp;b)
{
  // Look mum! No brackets needed!
  return a &gt; b ? a : b;
}
void better_example()
{
  int a = 3, b = 10;
  // this would have failed using the macro
  // because ++a would be evaluated twice
  int c = max(++a, b);
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 1</td>
	</tr>
</table>

<p>The original version also had another problem: wheel reinvention. The best solution is to just use the built-in <code>std::max</code> function that always existed. Itâ€™s obvious in hindsight:</p>

<pre class="programlisting">
  // don't declare any max function
  
  void even_better_example()
  {
    int a = 3, b = 10;
    int c = std::max(a,b);
  }</pre>
  
<p>This is the kind of thing youâ€™d cringe about now, if you came back to it. But you had no idea about the right idiom back in the day.</p>

<p>Thatâ€™s a simple example, but as languages gain new features (e.g., lambdas) the kind of idiomatic code youâ€™d write today may look very different from previous generations of the code.</p>

<h2>Design decisions</h2>

<p>Did I <em>really</em> write that in Perl; what was I thinking?! Did I <em>really</em> use such a simplistic sorting algorithm? Did I <em>really</em> write all that code by hand, rather than just using a built-in library function? Did I <em>really</em> couple those classes together so unnecessarily? Could I <em>really</em> not have invented a cleaner API? Did I <em>really</em> leave resource management up to the client code? I can see many potential bugs and leaks lurking there!</p>

<p>As you learn more, you realise that there are better ways of formulating your design in code. This is the voice of experience. You make a few mistakes, read some different code, work with talented coders, and pretty soon find you have improved design skills.</p>

<h2>Bugs</h2>

<p>Perhaps this is the reason that drags you back to an old codebase. Sometimes coming back with fresh eyes uncovers obvious problems that you missed at the time. After youâ€™ve been bitten by certain kinds of bugs (often those that the common idioms steer you away from) you naturally begin to see potential bugs in old code. Itâ€™s the programmerâ€™s <em>sixth sense</em>.</p>

<h2>Conclusion</h2>

<p class="quote">No space of regret can make amends for one lifeâ€™s opportunity misused.<p class="blockquote">~ Charles Dickens, </p>
<em>A Christmas Carol</em></p>

<p>Looking back over your old code is like a code review for yourself. Itâ€™s a valuable exercise; perhaps you should take a quick tour through some of your old work. Do you like the way you used to program? How much have you learnt since then?</p>

<p>Does this kind of thing actually <em>matter</em>? If your old code isnâ€™t perfect, but it works, should you do anything about it? Should you go back and â€˜adjustâ€™ the code? Probably not â€“ <em>if it ainâ€™t broke donâ€™t fix it</em>. Code does not rot, unless the world changes around it. Your bits and bytes donâ€™t degrade, so the meaning will likely stay constant. Occasionally a compiler or language upgrade or a third-party library update might â€˜breakâ€™ your old code, or perhaps a code change elsewhere will invalidate a presumption you made. But normally, the code will soldier on faithfully, even if itâ€™s not perfect.</p>

<p>Itâ€™s important to appreciate how times have changed, how the programming world has moved on, and how your personal skills have improved over time. Finding old code that no longer feels â€˜rightâ€™ is a good thing: it shows that you have learnt and improved. Perhaps you donâ€™t have the opportunity to revise it now, but knowing where youâ€™ve come from helps to shape where youâ€™re going in your coding career. </p>

<p>Like the Ghost of Christmas Past, there are interesting cautionary lessons to be learnt from our old code if you take the time to look at it.</p>

<h2>Questions</h2>

<ul>
	<li>How does your old code shape up in the modern world? If it doesnâ€™t look too bad, does that mean that you havenâ€™t learnt anything new recently?</li>
	
	<li>How long have you been working in your primary language? How many revisions of the language standard or built-in library have been introduced in that time? What language features have been introduced that have shaped the style of the code you write?</li>
	
	<li>Consider some of the common idioms you now naturally employ. How do they help you avoid errors?</li>
</ul>

<p class="bio"><span class="author"><b>Pete Goodliffe</b></span>  Pete Goodliffe is a programmer who never stays at the same place in the software food chain. He has a passion for curry and doesnâ€™t wear shoes. Pete can be contacted at pete@goodliffe.net or @petegoodliffe</p>

<p>Pete's latest book, <i>Becoming a Better Programmer</i>, is published by O'Reilly.  It's available at http://shop.oreilly.com/product/0636920033929.do (and all good book stores).</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
