    <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 #27</title>
        <link>https://members.accu.org/index.php/articles/687</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">Professionalism in Programming, from CVu journal + CVu Journal Vol 16, #4 - Aug 2004</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/c184/">Journal Columns</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c182/">Professionalism</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/c101/">164</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c182+101/">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 #27</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 August 2004 13:16:07 +01:00 or Tue, 03 August 2004 13:16:07 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e22" id="d0e22"></a></h2>
</div>
<p><img src="/var/uploads/journals/resources/goodliffe.png" align="right">Is it that time
already? This is the 27th column in the professionalism series.
It's been running for 4&frac12; years now, in which time it's
established itself as a part of the furniture. You either take it
for granted and have learnt to carefully ignore it, or you
diligently read and inwardly digest every word. Well, now it's time
to break with tradition, and also to see how much attention you've
been paying all these years.</p>
<p>The series index beside this article provides an overview of the
secluded tributaries we've explored in the vast software
development waterways. In this article we'll revisit some of this
ground in a more interactive manner. It's your chance to see how
'professional' you really are.</p>
<p>Here's the game plan: for a few of these past topics I have
posed some thought-provoking questions for you to mull over. Some
are purely factual, some are more personal, probing your personal
development practices and those of the team you work in. Consider
them and answer as fully and honestly as you can. Then turn to the
end of the article, where I provide my musings on each question. I
won't be so bold as to claim this is a definitive answer set, but
more of a gentle exploration of each problem.</p>
<p>You'll only get out of this exercise as much as you're prepared
to put in. So grab a cup of coffee, find a comfy chair in a quiet
corner, and it's eyes down for a full house&hellip;</p>
<div class="sidebar">
<p class="title c3">Professionalism in Programming Index</p>
<p>This is a comprehensive catalogue of the professionalism series
to date.</p>
<div class="informaltable">
<table border="0">
&lt;colgroup&gt;
&lt;col&gt;
&lt;col&gt;
&lt;col&gt;
&lt;col&gt;&lt;/colgroup&gt;
&lt;thead&gt;
<tr>
<th align="left">No</th>
<th align="left">CVu</th>
<th align="left">Title/description</th>
<th align="right">Date</th>
</tr>
&lt;/thead&gt;
&lt;tbody valign=&quot;top&quot;&gt;
<tr>
<td>1</td>
<td>12.2</td>
<td>Layout of Source Code</td>
<td align="right">April 2000</td>
</tr>
<tr>
<td>2</td>
<td>12.3</td>
<td>Team Work</td>
<td align="right">June 2000</td>
</tr>
<tr>
<td>3</td>
<td>12.4</td>
<td>Being Specific <span class="emphasis"><em>Writing software
specifications</em></span></td>
<td align="right">August 2000</td>
</tr>
<tr>
<td>4</td>
<td>12.5</td>
<td>Code Reviews</td>
<td align="right">October 2000</td>
</tr>
<tr>
<td>5</td>
<td>12.6</td>
<td>Documenting Code</td>
<td align="right">December 2000</td>
</tr>
<tr>
<td>6</td>
<td>13.1</td>
<td>Good Design</td>
<td align="right">February 2001</td>
</tr>
<tr>
<td>7</td>
<td>13.2</td>
<td>Practising Safe Source <span class="emphasis"><em>Source
control systems</em></span></td>
<td align="right">April 2001</td>
</tr>
<tr>
<td>8</td>
<td>13.3</td>
<td>The Programmer's Toolbox</td>
<td align="right">June 2001</td>
</tr>
<tr>
<td>9</td>
<td>13.4</td>
<td>Software Testing</td>
<td align="right">October 2001</td>
</tr>
<tr>
<td>10</td>
<td>13.5</td>
<td>Defensive Programming</td>
<td align="right">August 2001</td>
</tr>
<tr>
<td>11</td>
<td>13.6</td>
<td>Software Development: Fantasy, Fiction or Face <span class=
"emphasis"><em>A cautionary parable for software
developers</em></span></td>
<td align="right">December 2001</td>
</tr>
<tr>
<td>12</td>
<td>14.1</td>
<td>Recipe For a Program <span class="emphasis"><em>Software
development methodologies</em></span></td>
<td align="right">February 2002</td>
</tr>
<tr>
<td>13</td>
<td>14.2</td>
<td>How Long is a Piece of String? <span class=
"emphasis"><em>Software time-scale estimation</em></span></td>
<td align="right">April 2002</td>
</tr>
<tr>
<td>14</td>
<td>14.3</td>
<td>There and Back Again <span class="emphasis"><em>Personal
development</em></span></td>
<td align="right">June 2002</td>
</tr>
<tr>
<td>15</td>
<td>14.4</td>
<td>The Outer Limits <span class="emphasis"><em>Overview of
programming disciplines</em></span></td>
<td align="right">August 2002</td>
</tr>
<tr>
<td>16</td>
<td>14.5</td>
<td>What's in a Name? <span class="emphasis"><em>Naming program
elements appropriately</em></span></td>
<td align="right">October 2002</td>
</tr>
<tr>
<td>17</td>
<td>14.6</td>
<td>The Code that Jack Built <span class="emphasis"><em>Code build
systems</em></span></td>
<td align="right">December 2002</td>
</tr>
<tr>
<td>18</td>
<td>15.1</td>
<td>Engineering a Release <span class="emphasis"><em>The real
software development process</em></span></td>
<td align="right">February 2003</td>
</tr>
<tr>
<td>19</td>
<td>15.2</td>
<td>A Passing Comment <span class="emphasis"><em>Writing effective
code comments</em></span></td>
<td align="right">April 2003</td>
</tr>
<tr>
<td>20</td>
<td>15.3</td>
<td>Software Evolution or Software Revolution? <span class=
"emphasis"><em>How software grows over time</em></span></td>
<td align="right">June 2003</td>
</tr>
<tr>
<td>21</td>
<td>15.4</td>
<td>Software Architecture</td>
<td align="right">August 2003</td>
</tr>
<tr>
<td>22</td>
<td>15.5</td>
<td>Finding Fault <span class="emphasis"><em>Debugging your
programs</em></span></td>
<td align="right">October 2003</td>
</tr>
<tr>
<td>23</td>
<td>15.6</td>
<td>To Err is Human <span class="emphasis"><em>Managing error
conditions</em></span></td>
<td align="right">December 2003</td>
</tr>
<tr>
<td>24</td>
<td>16.1</td>
<td>The Need For Speed (Part One) <span class=
"emphasis"><em>Optimisation series</em></span></td>
<td align="right">February 2004</td>
</tr>
<tr>
<td>25</td>
<td>16.2</td>
<td>The Need For Speed (Part Two)</td>
<td align="right">April 2004</td>
</tr>
<tr>
<td>26</td>
<td>16.3</td>
<td>The Need For Speed (Part Three)</td>
<td align="right">June 2004</td>
</tr>
<tr>
<td>27</td>
<td>16.4</td>
<td>And Now For Something Completely Different...</td>
<td align="right">August 2004</td>
</tr>
&lt;/tbody&gt;
</table>
</div>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e324" id=
"d0e324"></a>Questions</h2>
</div>
<p>First, here are the questions. 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="d0e329" id="d0e329"></a>Defensive
Programming (C Vu 13.5, August 2001)</h3>
</div>
<p>In this article we looked at the need for a 'defensive' approach
to programming, learnt to assume nothing, and investigated some
practical defensive coding techniques. We saw how to use
constraints effectively, as typified by C's <tt class=
"function">assert</tt> macro. However:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>Could there be such a thing as <span class="emphasis"><em>too
much</em></span> defensive programming?</p>
</li>
<li>
<p>Should assertions conditionally compile away to nothing in
production builds? If not, which assertions should remain in
release builds?</p>
</li>
<li>
<p>Should the defensive checking of pre- and postconditions be put
<span class="emphasis"><em>inside</em></span> each function, or
beside each important function <span class=
"emphasis"><em>call</em></span>?</p>
</li>
<li>
<p>Are constraints a perfect defensive tool? What are their
drawbacks?</p>
</li>
<li>
<p>Can you <span class="emphasis"><em>avoid</em></span> defensive
programming?</p>
<div class="orderedlist">
<ol type="a">
<li>
<p>If you designed a <span class="emphasis"><em>better</em></span>
language, would defensive programming still be necessary? How could
you do this?</p>
</li>
<li>
<p>Does this show that C and C++ are flawed because they have so
many areas for problems to manifest?</p>
</li>
</ol>
</div>
</li>
<li>
<p>What sort of code need you not worry about writing
defensively?</p>
</li>
<li>
<p>When you document a function, do you state the pre- and
postconditions?</p>
<div class="orderedlist">
<ol type="a">
<li>
<p>Are they always implicit in the description of what it does?</p>
</li>
<li>
<p>If there are no pre/postconditions do you explicitly document
this?</p>
</li>
</ol>
</div>
</li>
<li>
<p>Many companies pay lip service to defensive programming. Does
your team recommend it? Take a look at the code base - do they
really? How widely are constraints codified in assertions? How
thorough is the error checking in each function?</p>
</li>
<li>
<p>Are you naturally paranoid enough? Do you look both ways before
crossing the road? Do you eat your greens? Do you check for every
potential error in your code, no matter how unlikely?</p>
<div class="orderedlist">
<ol type="a">
<li>
<p>How easy is it to do this thoroughly? Do you forget to think
about errors?</p>
</li>
<li>
<p>Are there any ways to help yourself write more thorough
defensive code?</p>
</li>
</ol>
</div>
</li>
</ol>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e401" id="d0e401"></a>Layout of Source
Code (C Vu 12.2, April 2000)</h3>
</div>
<p>This first ever professionalism column looked into the
contentious topic of source code layout, demonstrating that
consistency is more important than any one particular coding style.
We concluded that holy wars over topics like this are pointless and
unprofessional. So:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>Should you alter the layout of legacy code to conform to your
latest code style? Is this a valuable use of code reformatting
tools?</p>
</li>
<li>
<p>One common layout convention is to split source lines at a set
number of columns. What are the pros and cons of this approach?</p>
</li>
<li>
<p>How detailed should a <span class=
"emphasis"><em>reasonable</em></span> coding standard be?</p>
<div class="orderedlist">
<ol type="a">
<li>
<p>How serious are deviations from the style? How many limbs should
be amputated for not following it?</p>
</li>
<li>
<p>Can such a specification become too detailed and restrictive?
What would happen if it did?</p>
</li>
</ol>
</div>
</li>
<li>
<p>Is good code <span class="emphasis"><em>presentation</em></span>
or good code <span class="emphasis"><em>design</em></span> more
important? Why?</p>
</li>
<li>
<p>Do you write in a consistent style?</p>
<div class="orderedlist">
<ol type="a">
<li>
<p>When you touch other people's code, what layout style do you
adopt - theirs or your own?</p>
</li>
<li>
<p>How much of your coding style is dictated by your editor's
autoformatting? Is this an adequate reason for adopting a
particular style?</p>
</li>
</ol>
</div>
</li>
<li>
<p>Tabs: are they a work of the devil, or the best thing since
sliced bread? Explain why.</p>
<div class="orderedlist">
<ol type="a">
<li>
<p>Do you know if your editor inserts tabs automatically? Do you
know what your editor's tab stop is?</p>
</li>
<li>
<p>Some <span class="emphasis"><em>hugely</em></span> popular
editors indent with a mixture of tabs and spaces. Does this make
the code any less maintainable?</p>
</li>
<li>
<p>How many spaces should a tab correspond to?</p>
</li>
</ol>
</div>
</li>
<li>
<p>Grab a text editor and have a go at this bit of C++, it
calculates the n<sup>th</sup> prime number. It's written in one
particular coding style. Have a crack at presenting it as
<span class="emphasis"><em>you'd</em></span> like to see it. Don't
try to change the implementation at all.</p>
<pre class="programlisting">
/* Returns whether num is prime. */
bool
isPrime(int num ) {
    for ( int x = 2; x &amp;lt; num; ++x ) {
      if ( !(num % x ) ) return false;
    }
    return true;
}

/* This function calculates the 'n'th prime
   number.*/
int
prime( int pos ) {
    if ( pos ) {
        int x = prime( pos-1 ) + 1;
        while ( !isPrime( x ) ) {
            ++x;
        }
        return x;
    } else {
        return 1;
    }
}
</pre></li>
</ol>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e472" id="d0e472"></a>What's in a
Name? (C Vu 14.5, October 2002)</h3>
</div>
<p>This article showed the impact of good naming on the quality of
our source code, and demonstrated practical naming techniques for
many common code constructs. What do you think about these
issues:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>Are these good variable names? Answer with either <span class=
"emphasis"><em>yes</em></span> (explain why, and in what context),
<span class="emphasis"><em>no</em></span> (explain why), or
<span class="emphasis"><em>can't tell</em></span> (explain
why).</p>
<div class="orderedlist">
<ol type="a">
<li>
<p><tt class="literal">int num_apples</tt></p>
</li>
<li>
<p><tt class="literal">char foo</tt></p>
</li>
<li>
<p><tt class="literal">bool num_apples</tt></p>
</li>
<li>
<p><tt class="literal">char *string</tt></p>
</li>
<li>
<p><tt class="literal">int loop_counter</tt></p>
</li>
</ol>
</div>
</li>
<li>
<p>When would these be appropriate function names? What return
types/parameters might you expect? What return types would make
them nonsensical?</p>
<div class="orderedlist">
<ol type="a">
<li>
<p><tt class="function">doIt(...)</tt></p>
</li>
<li>
<p><tt class="function">value(...)</tt></p>
</li>
<li>
<p><tt class="function">sponge(...)</tt></p>
</li>
<li>
<p><tt class="function">isApple(...)</tt></p>
</li>
</ol>
</div>
</li>
<li>
<p>Should a naming scheme favour the easy reading or easy writing
of code? How would you make either easy?</p>
</li>
<li>
<p>What do you do when naming conventions collide? Say you're
working on camelCase C++ code, and need to do STL
(using_underscore) library work. What's the best way to handle this
situation?</p>
</li>
<li>
<p>If <tt class="function">assert</tt> is a macro why is its name
lower case? Why should we name macros so they stand out?</p>
</li>
<li>
<p>Long calculations can be made more readable by putting
intermediate results in temporary variables. Suggest good naming
heuristics for these types of variable.</p>
</li>
<li>
<p>Do you have to port code between platforms? How has this
affected filenames, any other naming, and the overall code
structure?</p>
</li>
</ol>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e549" id="d0e549"></a>A Passing
Comment ( C Vu 15.2, April 2003)</h3>
</div>
<p>The final article we'll exhume looked at how to write code
comments. We learnt what makes a good comment, how to avoid
pointless and intrusive comments, and how to use comments to help
us write source code. Given this, then:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>How might the <span class="emphasis"><em>need for</em></span>
and the <span class="emphasis"><em>content of</em></span> comments
differ in the following types of code:</p>
<div class="orderedlist">
<ol type="a">
<li>
<p>Low level assembly language (machine code)</p>
</li>
<li>
<p>Shell scripts</p>
</li>
<li>
<p>A single file test harness</p>
</li>
<li>
<p>A large C/C++ project</p>
</li>
</ol>
</div>
</li>
<li>
<p>You can run tools to calculate what percentage of your source
code lines are comments. How useful are they? How accurate a
measure is this of comment quality?</p>
</li>
<li>
<p>When you document a C/C++ API with a code comment block, should
it go in the public header file that declares the function, or the
source file containing the implementation? What are the pros and
cons of each location?</p>
</li>
<li>
<p>Look carefully at the source files you've recently worked on.
Inspect your commenting. Is it honestly any good? (I bet as you
read through the code you'll find yourself making a few
changes!)</p>
</li>
<li>
<p>How do you ensure that your comments are genuinely valuable and
not just personal ramblings that only you can understand?</p>
</li>
<li>
<p>Do the people you work with all comment to the same standard, in
about the same way?</p>
<div class="orderedlist">
<ol type="a">
<li>
<p>Who's the best at writing comments? Why do you think that? Who's
the worst? How much of a correlation does this bear to their
general quality of coding?</p>
</li>
<li>
<p>Do you think any imposed coding standards could raise the
quality of the comments written by your team?</p>
</li>
</ol>
</div>
</li>
<li>
<p>Do you include history logging information in each source file?
If yes:</p>
<div class="orderedlist">
<ol type="a">
<li>
<p>Do you do maintain it manually? Why, if your revision control
system will insert this for you automatically? Is the history kept
particularly accurate?</p>
</li>
<li>
<p>Is this a <span class="emphasis"><em>really</em></span> sensible
practice? How often is this information needed? Why is it better
placed in the source file than in another, separate mechanism?</p>
</li>
</ol>
</div>
</li>
<li>
<p>Do you add your initials to or otherwise mark the comments you
make in other people's code? Do you ever date comments? When and
why do you do this - is it a useful practice? Has it ever been
useful to find someone else's initials and time stamping?</p>
</li>
</ol>
</div>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e615" id="d0e615"></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="d0e620" id="d0e620"></a>Defensive
Programming</h3>
</div>
<div class="orderedlist">
<ol type="1">
<li>
<p><span class="bold"><b>Could there be such a thing as
<span class="emphasis"><em>too much</em></span> defensive
programming?</b></span></p>
<p>Yes - just as too many comments can degrade code readability, so
too can many defensive checks. Redundant checks can be avoided with
careful coding, for example with a good choice of types.</p>
</li>
<li>
<p><span class="bold"><b>Should assertions conditionally compile
away to nothing in production builds? If not, which assertions
should remain in release builds?</b></span></p>
<p>People hold passionate beliefs on this subject. I don't think
the answer is necessarily black and white; I can see the argument
both ways. There are always some very nit-picky assertions that
really won't <span class="emphasis"><em>need</em></span> to be left
in production builds. But some assertion occurrences may still
interest you in the field. Now, if you do leave any constraint
checks in, they <span class="emphasis"><em>must</em></span> change
behaviour - the program shouldn't abort on failure, just log the
problem and move on.</p>
<p>Remember, real run-time error checks should <span class=
"emphasis"><em>never</em></span> be removed; they should never be
coded in assertions anyway.</p>
</li>
<li>
<p><span class="bold"><b>Should the defensive checking of pre- and
postconditions be put <span class="emphasis"><em>inside</em></span>
each function, or beside each important function <span class=
"emphasis"><em>call</em></span>?</b></span></p>
<p>In the function, without a doubt. This way, you only need to
write tests once. The only reason you'd want to move them out is to
gain flexibility, to choose what happens when a constraint fails.
This isn't a compelling gain for such an explosion in complexity
and potential for failure.</p>
</li>
<li>
<p><span class="bold"><b>Are constraints a perfect defensive tool?
What are their drawbacks?</b></span></p>
<p>No, they are nowhere near perfect. Redundant constraints can be
at best a pest, and at worst a hindrance. For example, you could
<tt class="function">assert</tt> that a function parameter
<tt class="literal">i &gt;= 0</tt>. But it's much better to make i
an unsigned type that can't contain 'invalid values' anyway.</p>
<p>Treat constraints that can be compiled out with a certain degree
of suspicion: we must carefully check for any side effects
(assertions can have subtle indirect consequences), and for timing
issues in the debug build that alters its behaviour from a release
build. Ensure that assertions are logical constraints and not
genuine run-time checks that mustn't be compiled out. It
<span class="emphasis"><em>is</em></span> possible to put bugs in
the bug-defence code!</p>
<p>Carefully used, constraints are still far better than dancing
barefoot over the hot coals of chance.</p>
</li>
<li>
<p><span class="bold"><b>Can you <span class=
"emphasis"><em>avoid</em></span> defensive
programming?</b></span></p>
<div class="orderedlist">
<ol type="a">
<li>
<p><span class="bold"><b>If you designed a <span class=
"emphasis"><em>better</em></span> language, would defensive
programming still be necessary? How could you do
this?</b></span></p>
</li>
<li>
<p><span class="bold"><b>Does this show that C and C++ are flawed
because they have so many areas for problems to
manifest?</b></span></p>
</li>
</ol>
</div>
<p>Some language features certainly could be designed to avoid
errors. For example, C doesn't check the index of any array lookup
you perform. As a result you can crash the program by accessing an
invalid memory address. The Java runtime, on the other hand, checks
<span class="emphasis"><em>every</em></span> array index before
lookup, so such an catastrophe will never arise. (Bad indexes will
still cause an error though, which is just a better defined class
of failure).</p>
<p>Despite the long list of 'improvements' you could make to the
liberal C specification (and I urge you to think of as many as you
can) you'll never be able to create a language that doesn't need
defensive programming. Functions will always need to validate
parameters, and classes will always need invariants to check their
data is internally consistent.</p>
<p>Although C and C++ do provide plenty of opportunity for things
to go wrong, they also provide a great deal of power and
expression. Whether that makes the languages 'flawed' depends on
your viewpoint - this is a topic ripe for holy war.</p>
</li>
<li>
<p><span class="bold"><b>What sort of code need you not worry about
writing defensively?</b></span></p>
<p>I've worked with people who refused to put any defensive code
into an old program because it was <span class="emphasis"><em>so
bad</em></span> that their defences would make no difference. I
managed to resist the urge to whack them with a large mallet!</p>
<p>You might argue that a small, stand-alone, single file program
or perhaps a small test harness file doesn't need this sort of
careful defensive code or any rigorous constraints - but even in
these situations not being careful is just being sloppy. We should
be defensive all the time.</p>
</li>
<li>
<p><span class="bold"><b>When you document a function, do you state
the pre- and postconditions?</b></span></p>
<div class="orderedlist">
<ol type="a">
<li>
<p><span class="bold"><b>Are they always implicit in the
description of what it does?</b></span></p>
</li>
<li>
<p><span class="bold"><b>If there are no pre/postconditions do you
explicitly document this?</b></span></p>
</li>
</ol>
</div>
<p>No matter how obvious you think a contract is from the function
name or its description, explicitly stating the constraints removes
any ambiguity - remember, it's always better to remove areas of
assumption. Explicitly writing <span class=
"emphasis"><em>Preconditions: None</em></span> will document a
contract explicitly.</p>
</li>
<li>
<p><span class="bold"><b>Many companies pay lip service to
defensive programming. Does your team recommend it? Take a look at
the codebase - do they really? How widely are constraints codified
in assertions? How thorough is the error checking in each
function?</b></span></p>
<p>Very few companies have a culture of excellent code with the
right level of defence. Code reviews are a good way to bring a
team's code up to a reasonable standard; many eyes see many more
potential errors.</p>
</li>
<li>
<p><span class="bold"><b>Are you naturally paranoid enough? Do you
look both ways before crossing the road? Do you eat your greens? Do
you check for every potential error in your code, no matter how
unlikely?</b></span></p>
<div class="orderedlist">
<ol type="a">
<li>
<p><span class="bold"><b>How easy is it to do this thoroughly? Do
you forget to think about errors?</b></span></p>
</li>
<li>
<p><span class="bold"><b>Are there any ways to help yourself write
more thorough defensive code?</b></span></p>
</li>
</ol>
</div>
<p>No one finds it naturally easy - thinking the worst of your
carefully crafted new code runs contrary to the programmer
instinct. Instead, expect the worst of any people who will be using
your code. They're nowhere near as conscientious a programmer as
you!</p>
<p>A very helpful technique is to write unit tests for each
function/class. Some experts strongly advise doing this
<span class="emphasis"><em>before</em></span> writing a function;
this makes a lot of sense. It helps you to think about all the
error cases, rather than blithely trusting that your code will
work.</p>
</li>
</ol>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e764" id="d0e764"></a>Layout of Source
Code</h3>
</div>
<div class="orderedlist">
<ol type="1">
<li>
<p><span class="bold"><b>Should you alter the layout of legacy code
to conform to your latest code style? Is this a valuable use of
code reformatting tools?</b></span></p>
<p>It's usually safest to leave legacy code however you find it,
even if it's ugly and hard to work with. I'd only entertain
reformatting if I was absolutely sure that none of the original
authors would ever need to return.</p>
<p>By reformatting you lose the ability to easily compare a
particular revision of the source with a previous one - you'll be
thrown by many, many, formatting changes which may hide the one
difference you really needed to see. You also risk introducing
program errors in the reformatting.</p>
<p>As far as code reformatting tools go, they're nice curiosities,
but I don't advocate the use of them. Some companies insist on
running source files through these tools before checking any code
into their repository. The advantage is that all source is
homogenised, pasteurised, and uniformly formatted. The major
disadvantage is that no tool is perfect; you'll lose some helpful
nuances of the author's layout. Unless all the programmers on your
team are gibbons, don't use a reformatting tool.</p>
</li>
<li>
<p><span class="bold"><b>One common layout convention is to split
source lines at a set number of columns. What are the pros and cons
of this approach?</b></span></p>
<p>As with many such presentation concerns there is no absolute
answer; it is a matter of personal taste.</p>
<p>I like to split my code up so that it fits on an 80 column
display. I've always done that, so it's a matter of habit as much
as anything else. I don't disagree with people who like long lines,
but I find long lines hard to work with. I set my editor up to
'wrap' continuous lines rather than provide a horizontal scrollbar
(horizontal scrolling is clumsy). In this environment long lines
tend to ruin the effect of any indentation.</p>
<p>As I see it, the main advantage of fixed column widths is not
printability, as some would claim. It's the ability to have several
editor windows open side-by-side on the same display.</p>
<p>In practice, C++ seems to produce very long lines. It's more
verbose than C; you end up calling member functions on objects
referenced by another object through a templated container... There
are other strategies to manage the many, many, long lines this may
lead to. You could store intermediate references in temporary
variables, for example.</p>
</li>
<li>
<p><span class="bold"><b>How detailed should a <span class=
"emphasis"><em>reasonable</em></span> coding standard
be?</b></span></p>
<div class="orderedlist">
<ol type="a">
<li>
<p><span class="bold"><b>How serious are deviations from the style?
How many limbs should be amputated for not following
it?</b></span></p>
</li>
<li>
<p><span class="bold"><b>Can such a specification become too
detailed and restrictive? What would happen if it
did?</b></span></p>
</li>
</ol>
</div>
<p>Six limbs should be amputated for deviations from any coding
standard.</p>
<p>I have seen many coding standards that are so prescriptive and
paralysing that the poor programmers have just plain ignored it. To
be useful, and to be accepted, a coding standard should provide a
little room for manoeuvre, perhaps with a <span class=
"emphasis"><em>best practice</em></span> approach given as an
example.</p>
</li>
<li>
<p><span class="bold"><b>Is good code <span class=
"emphasis"><em>presentation</em></span> or good code <span class=
"emphasis"><em>design</em></span> more important?
Why?</b></span></p>
<p>This is really a very artificial question. Both are fundamental
for good code, and you should never be asked to sacrifice one for
the other. If you ever are, fear. However, which one you just chose
may say a lot about you as a programmer.</p>
<p>Bad formatting is certainly easier to fix than bad design,
especially if you use clever tools to homogenise your code's
formatting.</p>
<p>There is an interesting connection between presentation and
design: Bad presentation often shows that the code was produced by
a bad programmer, which probably means that it suffers from bad
internal design too. Or it may imply that the code has been
maintained by a series of different programmers, with a subsequent
loss of the initial code design.</p>
</li>
<li>
<p><span class="bold"><b>Do you write in a consistent
style?</b></span></p>
<div class="orderedlist">
<ol type="a">
<li>
<p><span class="bold"><b>When you touch other people's code, what
layout style do you adopt - theirs or your own?</b></span></p>
</li>
<li>
<p><span class="bold"><b>How much of your coding style is dictated
by your editor's autoformatting? Is this an adequate reason for
adopting a particular style?</b></span></p>
</li>
</ol>
</div>
<p>If you can't alter the way your editor positions the cursor for
you then you shouldn't be using it (either you're too inept, or
your editor is).</p>
<p>If you can't write code in a consistent style then you should
have your programmer's licence revoked. If you can't follow someone
else's presentation style then you should be forced to maintain
BASIC for the rest of your career.</p>
<p>[<i><span class="remark">FORTRAN 77 is far more restrictive on
its code style and formatting. Depending on the flavour of BASIC,
style and format can be varied greatly - Ed</span></i>]</p>
</li>
<li>
<p><span class="bold"><b>Tabs: are they a work of the devil, or the
best thing since sliced bread? Explain why.</b></span></p>
<div class="orderedlist">
<ol type="a">
<li>
<p><span class="bold"><b>Do you know if your editor inserts tabs
automatically? Do you know what your editor's tab stop
is?</b></span></p>
</li>
<li>
<p><span class="bold"><b>Some <span class=
"emphasis"><em>hugely</em></span> popular editors indent with a
mixture of tabs and spaces. Does this make the code any less
maintainable?</b></span></p>
</li>
<li>
<p><span class="bold"><b>How many spaces should a tab correspond
to?</b></span></p>
</li>
</ol>
</div>
<p>Since this is such a religious issue, I'll just say tabs:
<span class="emphasis"><em>they suck</em></span> and back away
quickly. Well, actually I'll add afterwards that the only thing
more evil than indenting with tabs is indenting with tabs
<span class="emphasis"><em>and</em></span> spaces - nightmare!</p>
<p>If your editor <span class="emphasis"><em>is</em></span>
inserting tabs (and probably spaces) without you noticing, try
using another editor for a while, to appreciate how frustrating it
is. Try setting your tabstop to a different value and see what a
mess it makes of the code. <span class="emphasis"><em>Everyone uses
the same editor, so it doesn't matter</em></span> is not a
professional attitude. They don't.</p>
<p>You'll hear people recommend their choice of tabstop length and
carefully justify their opinion. That's all very well; in fact a
respected study claims that a <span class=
"emphasis"><em>three</em></span> or <span class=
"emphasis"><em>four</em></span> space tabstop provides optimum
readability. (I favour four spaces because I don't like odd
numbers!) However, a tab should 'correspond' to <span class=
"emphasis"><em>no</em></span> fixed number of spaces. A tab is a
tab, which is not a space or any multiple thereof. For code laid
out using tabs, it shouldn't matter exactly how many spaces the tab
is displayed as - the code should read well regardless.
Unfortunately, I have rarely seen tab-indented code that works this
way. All too often tabs and spaces are mixed together to make code
line up neatly. This works fine with a tabstop set as the author
intended. It makes an unholy mess with any other setting.</p>
</li>
<li>
<p><span class="bold"><b>Grab a text editor and have a go at this
bit of C++, it calculates the n<sup>th</sup> prime number. It's
written in one particular coding style. Have a crack at presenting
it as <span class="emphasis"><em>you'd</em></span> like to see it.
Don't try to change the implementation at all.</b></span></p>
<p>This is a representative bit of Real World code, so don't
dismiss this as a stupid and tedious exercise. Note that I haven't
given any suggested answer here. My reformatting is just as valid
as yours, and indeed as the original format.</p>
<p>If you're reading these answers without chewing over the
questions at all, go on - have a go at this one. The magazine can
wait whilst you type in a few lines...</p>
<p>Now, take a look at what you've written.</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>How different is your version? How many specific changes did you
make?</p>
</li>
<li>
<p>For each change: is it a personal aesthetic preference, or can
you justify the change with some rationale? Question this rationale
- is it truly valid? How strongly would you be prepared to defend
it?</p>
</li>
<li>
<p>How comfortable were you with the original format? Did it bother
you to read? Could you work in that coding style if you encountered
code like it? Should you be able to become comfortable with it?</p>
</li>
</ul>
</div>
<p>[<i><span class="remark">I have to admit I found it very
difficult to restrain myself from reformatting the code as I
processed this article! - Production Ed</span></i>]</p>
</li>
</ol>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e929" id="d0e929"></a>What's in a
Name?</h3>
</div>
<div class="orderedlist">
<ol type="1">
<li>
<p><span class="bold"><b>Are these good variable names? Answer with
either <span class="emphasis"><em>yes</em></span> (explain why, and
in what context), <span class="emphasis"><em>no</em></span>
(explain why), or <span class="emphasis"><em>can't tell</em></span>
(explain why).</b></span></p>
<div class="orderedlist">
<ol type="a">
<li>
<p><tt class="literal">int num_apples</tt></p>
</li>
<li>
<p><tt class="literal">char foo</tt></p>
</li>
<li>
<p><tt class="literal">bool num_apples</tt></p>
</li>
<li>
<p><tt class="literal">char *string</tt></p>
</li>
<li>
<p><tt class="literal">int loop_counter</tt></p>
</li>
</ol>
</div>
<p>The quality of a name depends on its context, and we can't
honestly tell whether any of these are good or bad names. That's
why the question asks for example contexts. There are some obvious
contexts where the names might be bad: <tt class=
"varname">num_apples</tt> wouldn't be a particularly good name for
a grapefruit counter.</p>
<p><tt class="varname">foo</tt> is <span class=
"emphasis"><em>never</em></span> a good name. I've yet to see
anyone counting <tt class="varname">foo</tt>s. <tt class=
"varname">loop_counter</tt> is also bad; even if a loop gets too
big for a short counter name you can still pick a more descriptive
name, one that reflects the actual <span class=
"emphasis"><em>use</em></span> of loop counter rather than its role
<span class="emphasis"><em>as</em></span> a loop counter.</p>
<p>We can't really tell whether <tt class="literal">bool
num_apples</tt> is a good name, but it looks like it's not - a
boolean cannot hold a number. Perhaps it's recording whether a
separate count of apples is valid, but in this case it ought to be
called something like <tt class=
"varname">is_num_apples_valid</tt>.</p>
</li>
<li>
<p><span class="bold"><b>When would these be appropriate function
names? What return types/parameters might you expect? What return
types would make them nonsensical?</b></span></p>
<div class="orderedlist">
<ol type="a">
<li>
<p><tt class="function">doIt(...)</tt></p>
</li>
<li>
<p><tt class="function">value(...)</tt></p>
</li>
<li>
<p><tt class="function">sponge(...)</tt></p>
</li>
<li>
<p><tt class="function">isApple(...)</tt></p>
</li>
</ol>
</div>
<p>What each of these might mean depends on where you find them.
Again we see how a name depends on its context, and that context
can be provided by the enclosing scope of the function. Context
information can even be given by function parameters or return
variables.</p>
</li>
<li>
<p><span class="bold"><b>Should a naming scheme favour the easy
reading or easy writing of code? How would you make either
easy?</b></span></p>
<p>How many times do you write a piece of code? (Think about it).
How many times do you read it? That should give some indication as
to the relative importances.</p>
</li>
<li>
<p><span class="bold"><b>What do you do when naming conventions
collide? Say you're working on camelCase C++ code, and need to do
STL (using_underscore) library work. What's the best way to handle
this situation?</b></span></p>
<p>I've worked on C++ codebases that used such a collision of
naming conventions to their advantage. The internal logic used
camelCase, whereas libraries and components that could be
considered extensions of the standard library followed STL naming
conventions. It actually worked quite well.</p>
<p>Unfortunately, it doesn't always work that nicely. I've seen
plenty of inconsistent code where there was no rhyme or reason
behind the changing styles.</p>
</li>
<li>
<p><span class="bold"><b>If <tt class="function">assert</tt> is a
macro why is its name lower case? Why should we name macros so they
stand out?</b></span></p>
<p><tt class="function">assert</tt> isn't capitalised because
<tt class="function">assert</tt> isn't capitalised. In an ideal
world it would be, but standards being what they are we have to
live with this tatty macro name. Sigh.</p>
<p>Macros and <tt class="literal">#defined</tt> constant
definitions are downright dangerous - adopting the UPPER CASE name
convention will prevent nasty collisions with ordinary names. It's
as sensible as wearing glasses when a lunatic is walking round with
a big pointy stick.</p>
<p>Because macros can be so painful you should chose names that are
very unlikely to cause headaches. More importantly, avoid using the
preprocessor as much as humanly possible.</p>
</li>
<li>
<p><span class="bold"><b>Long calculations can be made more
readable by putting intermediate results in temporary variables.
Suggest good naming heuristics for these types of
variable.</b></span></p>
<p>Bad temporary names are <tt class="varname">tmp</tt>, <tt class=
"varname">tmp1</tt>, <tt class="varname">tmp2</tt>... or <tt class=
"varname">a</tt>, <tt class="varname">b</tt>, <tt class=
"varname">c</tt>... These, unfortunately, are all common
intermediate names.</p>
<p>Like any other item, temporary names should be meaningful. In
fact, in a complex calculation good names can really serve to
document the internal logic, showing what's going on.</p>
<p>If you find a value that really has no nameable purpose, if it
truly is an arbitrary intermediate value that's hard to name, then
you'll begin to understand why tmp is so popular. Avoid calling
anything tmp if possible - try to break the calculation in some
other way to help it make more sense.</p>
</li>
<li>
<p><span class="bold"><b>Do you have to port code between
platforms? How has this affected filenames, any other naming, and
the overall code structure?</b></span></p>
<p>Older filing systems limited the number of characters you could
use in a filename. This made file naming much messier. Unless you
have to port code to such an archaic system this kind of limitation
can be safely ignored.</p>
<p><span class="emphasis"><em>File-based polymorphism</em></span>
is a cunning way to exploit filenames to achieve code
substitutability at build-time. It's often used to select
platform-specific implementations in portable code. You can set up
header file search paths, allowing one <tt class=
"literal">#include</tt> to pull in a different file depending on
the current build platform.</p>
</li>
</ol>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e1098" id="d0e1098"></a>A Passing
Comment</h3>
</div>
<div class="orderedlist">
<ol type="1">
<li>
<p><span class="bold"><b>How might the <span class=
"emphasis"><em>need for</em></span> and the <span class=
"emphasis"><em>content of</em></span> comments differ in the
following types of code:</b></span></p>
<div class="orderedlist">
<ol type="a">
<li>
<p><span class="bold"><b>Low level assembly language (machine
code)</b></span></p>
</li>
<li>
<p><span class="bold"><b>Shell scripts</b></span></p>
</li>
<li>
<p><span class="bold"><b>A single file test harness</b></span></p>
</li>
<li>
<p><span class="bold"><b>A large C/C++ project</b></span></p>
</li>
</ol>
</div>
<p>Assembly language is less expressive, providing fewer
opportunities for self-documenting code. You'd therefore expect
more comments in assembly code, and those comments to be at a much
lower level than in other languages. Although the golden comment
rule is &quot;comments describe <span class=
"emphasis"><em>why</em></span> not <span class=
"emphasis"><em>how</em></span>&quot;, generally assembly comments
<span class="emphasis"><em>would</em></span> explain how as well as
why.</p>
<p>There isn't an enormous a difference between the remaining
three. Shell scripts can be quite hard to read back; they are a
proto-Perl in this respect. Careful commenting helps. You're more
likely to use literate programming techniques on a large C/C++
codebase.</p>
</li>
<li>
<p><span class="bold"><b>You can run tools to calculate what
percentage of your source code lines are comments. How useful are
they? How accurate a measure is this of comment
quality?</b></span></p>
<p>This kind of metric will give an insight into the code, but you
shouldn't get too concerned about it. It isn't an accurate
reflection of code quality. Well documented code might not contain
<span class="emphasis"><em>any</em></span> comments. Enormous
revision histories or large corporate copyright messages can
dominate small files, affecting this metric.</p>
</li>
<li>
<p><span class="bold"><b>When you document a C/C++ API with a code
comment block, should it go in the public header file that declares
the function, or the source file containing the implementation?
What are the pros and cons of each location?</b></span></p>
<p>This question was the cause of a big fight at one place I
worked. Some argued for descriptions to go in the <tt class=
"filename">.c</tt> file. Being close to the function means that
it's harder to write an incorrect comment, and harder to write code
that doesn't match the documentation. The comment is also more
likely to be changed in line with any code changes.</p>
<p>However, when placed in a header file, the description is
visible alongside the public interface; a logical location. Why
should someone have to look into the implementation to read any
public API docs?</p>
<p>A <span class="emphasis"><em>literate programming</em></span>
documentation tool should be able to pull comments out of either
place, but sometimes it's quicker not to use the tool and just read
comments in the source; a bonus of the literate code approach. I
favour placing the comments in header files.</p>
<p>Of course, in Java it's all one file anyway, and you'd
conventionally use the Javadoc format.</p>
</li>
<li>
<p><span class="bold"><b>Look carefully at the source files you've
recently worked on. Inspect your commenting. Is it honestly any
good? (I bet as you read through the code you'll find yourself
making a few changes!)</b></span></p>
<p>When you read and review your own code it's very easy to skip
the comments, presuming they're correct, or at least adequate. It
is a good idea to spend some time looking at them, to assess how
well they're written. Perhaps you could ask a trusted colleague to
give you their (constructive) opinion on your commenting style.</p>
</li>
<li>
<p><span class="bold"><b>How do you ensure that your comments are
genuinely valuable and not just personal ramblings that only you
can understand?</b></span></p>
<p>Some considerations for this are: write whole sentences, avoid
abbreviations, keep comments neatly formatted, and in a common
language (both the native language and the selection of words used
from the problem domain). Avoid in-jokes, any throw-away
statements, or anything that you're not entirely sure about.</p>
<p>Code reviews will highlight weaknesses in your comment
strategy.</p>
</li>
<li>
<p><span class="bold"><b>Do the people you work with all comment to
the same standard, in about the same way?</b></span></p>
<div class="orderedlist">
<ol type="a">
<li>
<p><span class="bold"><b>Who's the best at writing comments? Why do
you think that? Who's the worst? How much of a correlation does
this bear to their general quality of coding?</b></span></p>
</li>
<li>
<p><span class="bold"><b>Do you think any imposed coding standards
could raise the quality of the comments written by your
team?</b></span></p>
</li>
</ol>
</div>
<p>Use code reviews to inspect your peers' comment quality, and to
move your team towards a consistent quality of commenting.</p>
</li>
<li>
<p><span class="bold"><b>Do you include history logging information
in each source file? If yes:</b></span></p>
<div class="orderedlist">
<ol type="a">
<li>
<p><span class="bold"><b>Do you do maintain it manually? Why, if
your revision control system will insert this for you
automatically? Is the history kept particularly
accurate?</b></span></p>
</li>
<li>
<p><span class="bold"><b>Is this a <span class=
"emphasis"><em>really</em></span> sensible practice? How often is
this information needed? Why is it better placed in the source file
than in another, separate mechanism?</b></span></p>
</li>
</ol>
</div>
<p>It's human nature not to keep a history accurate, even with the
best intentions in the world. It requires a lot of manual work that
gets pushed out when time is tight. You should use tools to help,
and put the right information in the right place (which I don't
believe is the source file at all).</p>
</li>
<li>
<p><span class="bold"><b>Do you add your initials to or otherwise
mark the comments you make in other people's code? Do you ever date
comments? When and why do you do this - is it a useful practice?
Has it ever been useful to find someone else's initials and time
stamping?</b></span></p>
<p>For some comments this is a useful practice. In other places,
it's just inconvenient - extra comment noise that you have to read
past to get to the really interesting stuff.</p>
<p>It's most useful with temporary FIXME or TODO comments, marking
work in progress. Released production code probably shouldn't have
these; no finished code should need a reader to understand the
author or date of a particular change.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e1224" id="d0e1224"></a>And
Finally&hellip;</h2>
</div>
<p>Just in case you got this far, here's a parting shot - one final
question for a few bonus points. Answer this: How many programmers
does it take to change a light bulb?</p>
<p>I have five answers. If you're good then I'll tell you next
time. Let me know your answers.</p>
<p><span class="emphasis"><em>Pete Goodliffe</em></span></p>
<p>[With apologies to the ghost of Monty Python for the second
vague and fleeting reference of this series. Do you remember the
first one?]</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
