    <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  :: Professionalism in Programming #32</title>
        <link>https://members.accu.org/index.php/journals/815</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>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">CVu Journal Vol 17, #3 - Jun 2005 + Project Management</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/journals/">All</a>

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

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

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c96/">173</a>
                    (15)
<br />

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

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

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c66/">Management</a>
                    (95)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c96-66/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c96+66/">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;Professionalism in Programming #32</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 04 June 2005 05:00:00 +01:00 or Sat, 04 June 2005 05:00:00 +01:00</p>
<p><strong>Summary:</strong>&nbsp;<p>In this article I present a series of thought provoking questions based on some topics that I've covered in past issues.</p></p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e22" id="d0e22"></a></h2>
</div>
<div class="blockquote">
<blockquote class="blockquote">
<p>Imagination grows by exercise, and contrary to common belief, is
more powerful in the mature than in the young. (W. Somerset
Maugham)</p>
</blockquote>
</div>
<div class="c1"><img src="/var/uploads/journals/resources/pip_question_mark.png" align=
"right"></div>
<p>It's time to put on our walking shoes again, and trudge back
through the annals of history to revisit some of the themes we've
encountered on our journey through the professionalism series.
We've investigated an eclectic series of themes over the years, all
to do with the day-to-day programming experience. Throughout it all
I've been trying to draw out how 'professional' programmers react,
behave, and work. This is the second such nostalgia-inducing column
I've written and, as before, this is an opportunity to asses
exactly how 'professional' you are.</p>
<p>In this article I present a series of thought provoking
questions based on some topics that I've covered in past
issues<sup>[<a name="d0e32" href="#ftn.d0e32" id=
"d0e32">1</a>]</sup>. Mull them over and see what you think about
each question; make the effort to come up with an answer. The
motivation is simple: to improve your skill you have to get
involved - passively soaking up information doesn't do you much
good. Instead, you've got to invest some effort. I make no
apologies for writing such a difficult article this issue -
<span class="emphasis"><em>it's for your own good!</em></span></p>
<p>The plan is simple: I pose the questions first, give you some
time to think about them (go fetch a coffee, sit down peacefully,
and think it all through). Then I provide some discussion on the
questions. This discussion is not a definitive set of 'answers'
(most of these questions have no single simple answer), they're
more my immediate thoughts and responses. Compare your answers with
mine.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e40" id="d0e40"></a>Questions</h2>
</div>
<p>First, here are the questions. We'll look at two different
topics here. Spend a while considering your answer to each one
before you move on to the following section.</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e45" id="d0e45"></a>The Need for Speed
(C Vu 16.2, February 2004)</h3>
</div>
<p>This article series focused on software optimisation, explained
the reasons not to optimise and also the reasons you should. We
looked at general classes of optimisation, and at some quite
specific code-level optimisations.</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>Why is optimisation an issue? Why don't we write efficient code?
What stops us from using high performance algorithms in the first
place?</p>
</li>
<li>
<p>A <tt class="classname">List</tt> data type is implemented using
an array. What is the worst case algorithmic complexity of each of
the following <tt class="classname">List</tt> methods?</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>The constructor.</p>
</li>
<li>
<p><tt class="methodname">append</tt> - places a new item on the
end of the list.</p>
</li>
<li>
<p><tt class="methodname">insert</tt> - slides a new item in
between two existing list items, at a given position.</p>
</li>
<li>
<p><tt class="methodname">isEmpty</tt> - returns <tt class=
"literal">true</tt> if the list contains no items.</p>
</li>
<li>
<p><tt class="methodname">contains</tt> - returns <tt class=
"literal">true</tt> if the list contains a specified item.</p>
</li>
<li>
<p><tt class="methodname">get</tt> - returns the item with a given
index.</p>
</li>
</ul>
</div>
</li>
<li>
<p>How important (honestly) is code performance in your current
project? What is the motivator for this performance
requirement?</p>
</li>
<li>
<p>In your last optimisation attempt:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Did you use a profiler?</p>
<div class="itemizedlist">
<ul type="circle">
<li>
<p><span class="emphasis"><em>If yes:</em></span> how much
improvement did you measure</p>
</li>
<li>
<p><span class="emphasis"><em>If no:</em></span> how did you know
whether you made any kind of improvement?</p>
</li>
</ul>
</div>
</li>
<li>
<p>Did you test the code still worked after optimising</p>
<div class="itemizedlist">
<ul type="circle">
<li>
<p><span class="emphasis"><em>If yes:</em></span> How thoroughly
did you test?</p>
</li>
<li>
<p><span class="emphasis"><em>If no:</em></span> Why not? How could
you be sure the code still worked properly for all cases?</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
<li>
<p>How well specified are your program's performance requirements?
Do you have a concrete plan to test that you meet these
criteria</p>
</li>
</ol>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e136" id="d0e136"></a>Software Testing
(C Vu 13.4, October 2001)</h3>
</div>
<p>This article looked at the thorny topic of testing the software
that we write. We looked at when, how and why we test software.</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>Write a test harness for the following piece of code: a function
to calculate the greatest common divisor of two integers. Make it
as exhaustive as you can. How many individual test cases have you
included?</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>How many of these passed?</p>
</li>
<li>
<p>How many failed?</p>
</li>
<li>
<p>Using these tests, identify any faults and repair the code.</p>
<pre class="programlisting">
int greatest_common_divisor(int low, int high)
  {
    if (low  high)
    {
      int tmp = high;
      high    = low;
      low     = tmp;
    }

    int gcd = 0;
    for (int div = low; div  0; -div)
    {
      if ((low % div == 0) 
          (high % div == 0))
        if (gcd  div)
          gcd = div;
    }
  return gcd;
  }
</pre></li>
</ul>
</div>
</li>
<li>
<p>Should you test the all of the test code that you write?</p>
</li>
<li>
<p>How does a programmer's testing differ from a QA department
member's testing?</p>
</li>
<li>
<p>For what percentage of your code do you write tests? Are you
happy with this? What sort of testing do you give the remaining
code? Is this adequate? What will you do about it?</p>
</li>
<li>
<p>What's your usual response to finding an error in your code?</p>
</li>
</ol>
</div>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e169" id="d0e169"></a>Discussion
and Answers</h2>
</div>
<p>Lazy readers will have jumped here already. Please do spend some
time considering your answer to each question first. It will be
interesting to compare your response with mine. Do you disagree
with anything? Do you agree? Let me know.</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e174" id="d0e174"></a>The Need for
Speed</h3>
</div>
<div class="blockquote">
<blockquote class="blockquote">
<div class="orderedlist">
<ol type="1">
<li>
<p>Why is optimisation an issue? Why don't we write efficient code?
What stops us from using high performance algorithms in the first
place?</p>
</li>
</ol>
</div>
</blockquote>
</div>
<p>There are many perfectly valid reasons for not writing
'optimised' code on the first attempt:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>You don't know the final pattern of usage. With no Real World
test data, how can you choose the code design that will work
best?</p>
</li>
<li>
<p>It's hard enough to get the programming working, let alone fast.
To prove it's feasible we choose designs that are easy to implement
so prototypes get finished quickly.</p>
</li>
<li>
<p>'High performance' algorithms can be more complex and daunting
to implement. Programmers naturally shy away from them, since it's
an area where faults can be easily introduced.</p>
</li>
</ul>
</div>
<p>An obvious mistake is to think that the taken to run something
is proportional to the amount of effort spent writing
it<sup>[<a name="d0e196" href="#ftn.d0e196" id=
"d0e196">2</a>]</sup>. You might have written some file parsing
code in hours; it will always takes ages to execute, because disks
are slow. The complex code you spent half a week getting right may
only consume a few hundred processor cycles. The efficiency of a
piece of code, or the amount of time you need to spend optimising
it, bears no relation to the amount of time you spent writing
it.</p>
<div class="blockquote">
<blockquote class="blockquote">
<div class="orderedlist">
<ol start="2" type="1">
<li>
<p>A <tt class="classname">List</tt> data type is implemented using
an array. What is the worst case algorithmic complexity of each of
the following <tt class="classname">List</tt> methods?</p>
</li>
</ol>
</div>
</blockquote>
</div>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>The constructor is <tt class="literal">O(1)</tt> since it only
needs to create an array; the list is initially empty. However,
it's worth considering that the size of this array will affect the
complexity of the constructor - most languages create arrays fully
populated with objects, even if you don't plan to use them yet. If
the constructors for these objects are non-trivial, then the
<tt class="classname">List</tt> constructor will take some time to
execute.</p>
</li>
<li>
<p>The array size might not be fixed - the constructor could take a
parameter to determine this size (effectively setting the maximum
list size possible). The method then becomes <tt class=
"literal">O(n)</tt>.</p>
</li>
<li>
<p>The append operation is <tt class="literal">O(1)</tt>. It simply
has to write an array entry and update the list size.</p>
</li>
<li>
<p><tt class="methodname">insert</tt> is <tt class=
"literal">O(n)</tt>. In the worst case you will be asked to insert
an element at the very beginning of the list. This requires all the
elements in the array to be shuffled up one place before writing
the first element. The more items in the <tt class=
"classname">List</tt>, the longer this will take.</p>
</li>
<li>
<p>Unless you have a ridiculously bad implementation, <tt class=
"methodname">isEmpty</tt> is <tt class="literal">O(1)</tt>. The
list size will be known, so the return value is a single
calculation based on this number.</p>
</li>
<li>
<p><tt class="methodname">contains</tt> is <tt class=
"literal">O(n)</tt>, presuming the list contents are unordered. In
the worst case you will be asked to look for an item that doesn't
exist, and will have to traverse every single list item.</p>
</li>
<li>
<p><tt class="methodname">get</tt> is <tt class="literal">O(1)</tt>
thanks to the array implementation. Indexing an array is a constant
time operation. If <tt class="classname">List</tt> had been
implemented as a <span class="emphasis"><em>linked
list</em></span>, then this would have been an <tt class=
"literal">O(n)</tt> operation.</p>
</li>
</ul>
</div>
<div class="blockquote">
<blockquote class="blockquote">
<div class="orderedlist">
<ol start="3" type="1">
<li>
<p>How important (honestly) is code performance in your current
project? What is the motivator for this performance
requirement?</p>
</li>
</ol>
</div>
</blockquote>
</div>
<p>The performance requirements should not be arbitrarily chosen.
They should be justified; not just a time limit pulled out of thin
air. Every performance requirement is important - there are no
specifications that don't matter. How much concern a particular
requirement generates depends on how hard it is to meet. Whether
it's hard or not, you still have to come up with a design that
satisfies it.</p>
<div class="blockquote">
<blockquote class="blockquote">
<div class="orderedlist">
<ol start="4" type="1">
<li>
<p>In your last optimisation attempt:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Did you use a profiler?</p>
<div class="itemizedlist">
<ul type="circle">
<li>
<p><span class="emphasis"><em>If yes:</em></span> how much
improvement did you measure</p>
</li>
<li>
<p><span class="emphasis"><em>If no:</em></span> how did you know
whether you made any kind of improvement?</p>
</li>
</ul>
</div>
</li>
<li>
<p>Did you test the code still worked after optimising</p>
<div class="itemizedlist">
<ul type="circle">
<li>
<p><span class="emphasis"><em>If yes:</em></span> How thoroughly
did you test?</p>
</li>
<li>
<p><span class="emphasis"><em>If no:</em></span> Why not? How could
you be sure the code still worked properly for all cases?</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ol>
</div>
</blockquote>
</div>
<p>Only the most dramatic performance improvements can be detected
without a profiler, or some other good timing tests. Human
perception is easily fooled - when you've just slaved to speed up
the program, it will always <span class=
"emphasis"><em>appear</em></span> faster to you.</p>
<p>Test performance improvements carefully, and discard those that
are not worthwhile. It's better to have clear code than a miniscule
speed increase and unmaintainable logic.</p>
<div class="blockquote">
<blockquote class="blockquote">
<div class="orderedlist">
<ol start="5" type="1">
<li>
<p>How well specified are your program's performance requirements?
Do you have a concrete plan to test that you meet these
criteria?</p>
</li>
</ol>
</div>
</blockquote>
</div>
<p>Without a clear specification, no one can really complain that
your program isn't fast enough!</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e333" id="d0e333"></a>Software
Testing</h3>
</div>
<div class="blockquote">
<blockquote class="blockquote">
<div class="orderedlist">
<ol type="1">
<li>
<p>Write a test harness for the following piece of code: a function
to calculate the greatest common divisor of two integers. Make it
as exhaustive as you can. How many individual test cases have you
included?</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>How many of these passed?</p>
</li>
<li>
<p>How many failed?</p>
</li>
<li>
<p>Using these tests, identify any faults and repair the code.</p>
</li>
</ul>
</div>
</li>
</ol>
</div>
</blockquote>
</div>
<p>There are a large number of tests you should run, even though
there are very few 'invalid' input combinations. Thinking of
invalid inputs first: test for <span class=
"emphasis"><em>zero</em></span>. It may or may not be an invalid
value (we've seen no spec, so we can't tell), but you'd expect the
code to cope reasonably with it.</p>
<p>Next, write tests considering combinations of 'usual' inputs
(say of 1, 10, and 100 in all orders). Then try numbers with no
common multiple like 733 and 449. Test for some very large numbers,
and also for some negative numbers.</p>
<p>How do you write these test cases? Use a good test framework, or
put them all in a single test function. For each test, don't
programmatically calculate what the correct output value should
be<sup>[<a name="d0e360" href="#ftn.d0e360" id=
"d0e360">3</a>]</sup>, just check against a known constant value.
Keep your test code as simple as possible:</p>
<pre class="programlisting">
assert(greatest_common_divisor(10,  100) == 10);
assert(greatest_common_divisor(100, 10)  == 10);
assert(greatest_common_divisor(733, 449) == 0);
... more tests ... 
</pre>
<p>There are a surprisingly large number of tests for this simple
function. You could argue that for such a small piece of code it's
easier to inspect, review, and prove correctness rather than
laboriously create a set of tests. This seems like a valid
argument. But what if later on someone makes modifications? Without
the tests you'd have to carefully re-inspect and re-validate the
code, an easy task to overlook.</p>
<p>Did you find the mistake in <tt class=
"function">greatest_common_divisor</tt>? There's a clue coming up.
If you don't want the puzzle spoiled then look away now. Try
feeding it <span class="emphasis"><em>a negative
argument</em></span>. This is a more robust (and more efficient)
version written in C++:</p>
<pre class="programlisting">
int greatest_common_divisor(int a, int b)
{
  a = std::abs(a);
  b = std::abs(b);
  for (int div = std::min(a,b); div  0; -div)
    {
      if ((a % div == 0)  (b % div == 0))
          return div;
  }
  return 0;
}
</pre>
<div class="blockquote">
<blockquote class="blockquote">
<div class="orderedlist">
<ol start="2" type="1">
<li>
<p>Should you test the all of the test code that you write?</p>
</li>
</ol>
</div>
</blockquote>
</div>
<p>If you think about this for long enough it will give you a
headache. You can't keep testing test code how can you be sure the
test code for your test code's test code is correct? The trick is
to keep tests as <span class="emphasis"><em>simple as
possible</em></span>. This way, the most likely testing errors will
be missing important test cases, not problems with the actual lines
of test code.</p>
<div class="blockquote">
<blockquote class="blockquote">
<div class="orderedlist">
<ol start="3" type="1">
<li>
<p>How does a programmer's testing differ from a QA department
member's testing?</p>
</li>
</ol>
</div>
</blockquote>
</div>
<p>Testers are more concerned with the black box style of testing,
and usually only perform product testing. It's rare to have testers
working at the code level, because most products are executable
software; there are comparatively few code library
vendors.Programmers are more concerned with white-box tests, making
sure their masterful creations work as they planned.</p>
<p>The secret aim of any programmer writing tests is to prove that
their code works, not to find cases where it doesn't! I can easily
write a load of tests to show how perfect my code is by
deliberately avoiding all the bits I know are dodgy. This is a good
argument for getting someone other than the original programmer to
create test harnesses.</p>
<div class="blockquote">
<blockquote class="blockquote">
<div class="orderedlist">
<ol start="4" type="1">
<li>
<p>For what percentage of your code do you write tests? Are you
happy with this? What sort of testing do you give the remaining
code? Is this adequate? What will you do about it?</p>
</li>
</ol>
</div>
</blockquote>
</div>
<p>Don't feel obliged to a write test harness for every scrap of
code. Use your head. Inspections are fine for small functions. But
you must be sure that you are performing the adequate and
appropriate testing for which you are responsible, not just
skipping an unpleasant task.</p>
<p>Ask yourself this: how many errors have bitten you recently
which could have been prevented with a good set of tests? Make sure
you do something about it.</p>
<div class="blockquote">
<blockquote class="blockquote">
<div class="orderedlist">
<ol start="5" type="1">
<li>
<p>What's your usual response to finding an error in your code?</p>
</li>
</ol>
</div>
</blockquote>
</div>
<p>There are several possible reactions:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>disgust and disappointment,</p>
</li>
<li>
<p>the urge to blame someone else,</p>
</li>
<li>
<p>happiness, if not downright excitement, and even</p>
</li>
<li>
<p>pretending you didn't find it, ignoring it, and hoping it will
go away (as if that's likely)</p>
</li>
</ul>
</div>
<p>Some of those are so plainly wrong that I'll assume you can rise
above them.</p>
<p>Does it seem a little daft to suggest you might be <span class=
"emphasis"><em>happy</em></span> to find a fault? Surely that's the
reasonable reaction for a quality-conscious engineer, as it's far
better to find faults during development than for a user to find
them in the field.</p>
<p>Your level of excitement will depend on where in the development
lifecycle the fault is found. Discovering a show-stopping bug the
day before release won't make anyone smile.</p>
</div>
</div>
<div class="footnotes"><br>
<hr class="c4" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e32" href="#d0e32" id=
"ftn.d0e32">1</a>]</sup> You might want to go back and revisit the
specific articles before ploughing into these questions.</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e196" href="#d0e196" id=
"ftn.d0e196">2</a>]</sup> That looks stupid when you see it written
down, but it's a very easy trap to fall into at the codeface.</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e360" href="#d0e360" id=
"ftn.d0e360">3</a>]</sup> This would open the door to more coding
errors - image the pain of bugs in the test code!</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
