    <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 Part 6</title>
        <link>https://members.accu.org/index.php/articles/1092</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 13, #1 - Feb 2001</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/c122/">131</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c182+122/">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 Part 6</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 06 February 2001 13:15:43 +00:00 or Tue, 06 February 2001 13:15:43 +00:00</p>
<p><strong>Summary:</strong>&nbsp;<p>Good design</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>Sometimes you see some code and can do nothing but sigh.</p>
</blockquote>
</div>
<p>Recently I wrote a device driver for an embedded product. The
driver's interface to the OS was quite complex. The interface to
the hardware I was using was also quite complex. In order to keep
myself sane when writing the code, I split it into two sections.
The first was an internal 'library' that accessed the hardware,
performed some relevant data buffering and provided a simple API to
access that buffered data. Then I wrote a second, distinct layer
that implemented the fiddly OS driver interface, in terms of this
internal library. The structure of this device driver was therefore
like this:</p>
<div class="c3"><img src="/var/uploads/journals/resources/goodliffe_pip_6_1.png" align=
"middle"></div>
<p>Later I was sent, from the manufacturer of the hardware, a
sample implementation of the same device driver. However, the
author of this code had clearly not thought it out at all. The code
was a sprawling mess, tightly intermingling the complex OS
interface with the hardware logic in a completely incomprehensible
manner. The structure was approximately that shown in the figure
below.</p>
<div class="c3"><img src="/var/uploads/journals/resources/goodliffe_pip_6_2.png" align=
"middle"></div>
<p>Now, I am not trying to blow my own trumpet (any more than is
necessary, anyway). The point of this illustration is clear. A two
year old could tell you which of the software designs depicted
above is the better. The first is clearly easier to understand
because it is so much simpler. One of the signs of a mature
programmer is the quality of design in their code. This design
pervades more levels than the topmost 'architecture' of the
implementation. (The 'architecture' is a structural design of the
<span class="emphasis"><em>entire</em></span> system. When we talk
about highly distributed systems then this architecture encompasses
the split of tasks between computers and what software components
are bought in, as well as the shape of the implementation code's
design. However, here we are not talking about this sort of
high-level design in this article.).</p>
<p>There are two subtly different types of design. The first is the
overall structural design which, depending on project size, may be
the system architecture. This ends up written formally as a design
specification [<a href="#Goodliffe">Goodliffe</a>]. The other
design stage, which we are discussing in this article, describes
the decisions made in the implementation of this specification. If
a programmer has a self-contained block of code to write, there
will be more than one way they can write it. We all know that we
should think before we type, that we should design before we code,
and that we should specify before ploughing into something. But
even so, how often is this observed? But what is our motivation for
writing well designed code? Why not just quickly hack out what
occurs to us as we type? We come back to the issue of the
'audience' for our code, which we discussed in the last article.
Good design actually saves implementation time and makes it easier
to detect (and therefore fix) bugs. The code is therefore easier
and cheaper to maintain<sup>[<a name="d0e43" href="#ftn.d0e43" id=
"d0e43">1</a>]</sup>. Getting the design right is really very
important. The design of your code is the foundation upon which it
is built. If you get it wrong then the code will be unstable,
unsafe, not fit for purpose and possibly even dangerous. If the
foundation design is not properly in place then the end result will
be code that resembles the Leaning Tower of Pisa. It might even be
novel if it manages to stand up under the strain of real use,
however it will never be as good as it ought, and in time this cost
will make itself felt. So as professional programmers we need to be
able to design well. How are we going to get there? The factors
that will answer this question are:</p>
<div class="orderedlist">
<ol type="i">
<li>
<p>What makes for good software design, and</p>
</li>
<li>
<p>How do we learn to design well?</p>
</li>
</ol>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e54" id="d0e54"></a>What makes for
good software design?</h2>
</div>
<p>First thing that we need to know in order to design well is to
know the language of the target implementation. Maybe this is a
choice left up to the programmer, in which case the choice of the
language itself is in the design domain. However, more often than
not the language is fixed and we have to come up with a design for
the code structure in it.</p>
<p>It is important to know the language that will be used well,
since this knowledge will greatly affect our design. Now, that may
seem like a pretty obvious point to make, but it is frightening how
much it may need to be said. I was shocked when I read the log
message for some code an experienced C coder wrote: &quot;<span class=
"quote">Removed ';' after 'if' statements that meant the following
blocks always executed.</span>&quot; Now, he was not a bad software
engineer, or even a bad C programmer, and it is possible that this
elementary mistake was just that, a simple mistake. However, if you
do not know how to use the target language properly, how can you
design good code in it? This becomes more important for C++ than C
(and to some extent Java) since it has such a richer set of tools
for implementing particular functions.</p>
<p>But what is good design? It is very difficult to define; it has
got a lot to do with balance and aesthetics. The Patterns movement
has developed a lot of experience related to design.</p>
<p>Below is a list of some characteristics of well-designed code.
The list is probably nowhere near exhaustive, and by itself will
not guarantee a good design. However their converses are all sure
indications of <span class="emphasis"><em>bad</em></span> code
design.</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p><span class="bold"><b>Well-designed code separates the client
from the implementor</b></span></p>
<p>Of course, you can only successfully separate the user and
implementor if both parties have been successfully identified, and
their individual needs are understood. The point of separation is
well defined, be it function API or some middleware interface. It
is both easy to find, and adequately specified, for example there
are enough API functions to make the interface useable.</p>
</li>
<li>
<p><span class="bold"><b>Well-designed code is
elegant</b></span></p>
<p>Elegant is a dangerous word. It does <span class=
"emphasis"><em>not</em></span> mean that your code should be
baroque, confusingly clever and overly complex. Possibly the most
important characteristic of well-designed code is simplicity. In
fact, that's so important I'll repeat it: the most important
characteristic of well-designed code is simplicity.</p>
<p>You should be able to look at well-designed code and see a
beauty in its simplicity.</p>
<p>Elegant code uses the correct constructs in the correct place.
It makes use of language facilities or design patterns when and
where they are needed, and avoids gratuitous use of them elsewhere.
Clarity is key in the professional programmer's work. Clarity does
not come from 1000 lines of comments - it comes from simple,
elegantly structured code.</p>
</li>
<li>
<p><span class="bold"><b>Well-designed code is as small as possible
but no smaller</b></span></p>
<p>This is the <span class="emphasis"><em>less is more</em></span>
principle, another aspect of simplicity. You should carefully work
out how little code is actually necessary, and then write just
that. Remember, you can always add more code later on in the
product's life to add extra facilities, but you can usually never
remove something that is already in the codebase.</p>
</li>
<li>
<p><span class="bold"><b>Well-designed code is
extensible</b></span></p>
<p>It should allow extra functionality or data to be easily slotted
in in the appropriate places in the future. This should not lead to
over engineered code, though.</p>
<p>This may be accommodated through carefully chosen class
hierarchies, or callback functions, even just a fundamentally
logical and extensible code structure.</p>
</li>
<li>
<p><span class="bold"><b>Well-designed code associates like
things</b></span></p>
<p>In order to understand the code, all related things should be
conceptually grouped. This can be just from being held in the same
source file, to some syntactic grouping: all data held in a single
structure rather than in randomly scattered global variables, for
example. It is a clear aid to comprehension.</p>
<p>A side affect of this is the prevention of errors when a change
in one place actually affects something a million miles away. A
clear sign of dense, complex code is when a single, simple change
in one place leads to modifications of the code in many other
places.</p>
</li>
<li>
<p><span class="bold"><b>Well-designed code has a well-structured
error handling convention</b></span></p>
<p>In most cases, the choice of error handling <span class=
"emphasis"><em>mechanism</em></span> will be specified by the
choice of language and/or environment. However, a good design
clearly understands the difference between expected errors and
truly exceptional errors and clearly distinguishes between the
two.</p>
<p>The structure of the design will accommodate erroneous
situations, and the handling will not have been hacked in at the
end when the code was found not to work properly.</p>
</li>
<li>
<p><span class="bold"><b>Well-designed code abstracts non-portable
stuff</b></span></p>
<p>Time and time again I have seen code that was never intended to
be portable, so there was no requirement to design it as such. But
when a new platform was used it was simpler to adapt the old code
than rewrite some new code. This new platform need not be anything
as radical as a different OS or hardware, just a subtle variation
in a chip revision.</p>
<p>Unless portability is born in mind, later in its life code can
begin to look like, rather than being written by a professional
programmer, it has been plumbed by a philosopher. Just making a few
careful choices in how you structure OS-dependant, or
hardware-dependant areas of code can pay great dividends in the
future, and usually need not affect performance or clarity
(sometimes it may even improve clarity).</p>
</li>
<li>
<p><span class="bold"><b>Well designed code contains no
duplication</b></span></p>
<p>It should never have to repeat itself. Even repetition of
program structure without an exact match (the sane algorithm but
with different variable names, for example) is usually an indicator
that the code could be altered. Subclasses that are strikingly
similar indicate that some functionality could be shared in the
superclass, for example.</p>
<p>If there are two closely related pieces of code, you may find
and fix a bug in one of them and then forget to fix the same bug in
the other. This is clearly, therefore, a compromise in code
safety.</p>
</li>
<li>
<p><span class="bold"><b>Well-designed code does not have
methods/functions that are too big</b></span></p>
<p>This is an oft-cited fact. If a function is really big it is
hard to understand. It is also probably trying to do too much, and
should be split into a number of smaller self-contained functions.
On the other hand, in some situations the converse <span class=
"emphasis"><em>may</em></span> be true: if a function is too small
is it really needed? (Note, though, that the answer may often be
'yes').</p>
<p>Similarly, classes should not have too much or too little code,
or too many/few methods, or too many/too few instance variables.
These are warning signs of poorly structured object oriented
code.</p>
</li>
<li>
<p><span class="bold"><b>Well-designed code avoids the use of
global variables</b></span></p>
<p>If some data needs to be shared between different parts of a
program, global variables are not the way to do it. They create
dependencies between parts of code that are very hard to track.
They build a brick wall into the extensibility of the code. Public
global variables are very nearly downright evil.</p>
<p>There are other ways of doing it, for example hiding the shared
data behind a shared API; this can also aid code extensibility.</p>
</li>
<li>
<p><span class="bold"><b>Well-designed code is
documented</b></span></p>
<p>Last, but by no means least. A good design should be documented.
The documentation should be up to date. The documentation should be
small, because the design is so simple.</p>
</li>
</ul>
</div>
<p>In summary, good code design is a complex balance between
performance, elegance, and maintenance. Simplicity is the key.
Keeping the design as simple as possible will really pay off in the
long run.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e170" id="d0e170"></a>How do we
learn to design well?</h2>
</div>
<p>Are good designers born or made? Can we really learn to design
well? I believe the answer is (to a large extent) yes. Certainly,
absorbing all the points made above goes some way towards
understanding how to design well. But more than this, good design
largely comes from experience - seeing how particular problems have
been solved in the Real World, and gaining insights into the pros
and cons of different approaches to implementation. This
information should be stored in your software developer's toolbox,
from where the appropriate piece of code design can be later pulled
out for a given problem.</p>
<p>One of the best methodologies for accomplishing software design
is the <span class="emphasis"><em>divide and conquer</em></span>
approach. If you cannot see how to structure something in its
entirety, break it up into smaller pieces (or abstractions). Then
create a strategy for how to glue them together. In doing so you
will probably be defining some of the internal API. You have done
some design! Then go on to 'design' each of those components as if
it were a self-contained unit. You may end up changing where your
abstractions sit by the final design, but this method provides a
clear route into the design problem. One of the biggest design
challenges is identifying the appropriate abstractions. Again, this
largely comes with experience.</p>
<p>Although performance is often a factor in the design of code,
and may be a specified constraint, the <span class=
"emphasis"><em>design first, optimise later</em></span> rule is
important. You should ensure that the design is simple, elegant and
coherent <span class="emphasis"><em>before</em></span> complicating
it with only the necessary optimisations. How will you know which
are the 'necessary' optimisations? By inspecting the running code,
profiling it for run-time information and checking the memory
footprint. Similar to the fact that the initially code should be
designed carefully, any optimisations applied should also be
carefully thought through. This is, however, very much a secondary
activity to the good initial design.</p>
<p>If you are not sure how best to design something, you will learn
a lot by discussing the problem with your peers. This will provide
opportunity for you <span class="emphasis"><em>all</em></span> to
learn, work together, and ensure the design is the best possible
one you can come up with.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e193" id=
"d0e193"></a>Conclusion</h2>
</div>
<p>Good code is well designed. It has a certain aesthetic appeal
that makes it 'feel good'. There are a number of things you should
be considering in the design before beginning to write code: the
structure, the possible future extensions, error detection and
handling, specifying the correct interfaces and abstractions, the
correct use of the language, and portability. The design should be
documented as it is developed.</p>
<p>We should constantly be learning about the design of other
pieces of software, building upon our knowledge of the successes
and failures of other designs.</p>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e200" id="d0e200"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Goodliffe" id="Goodliffe"></a>
<p class="bibliomixed">[Goodliffe] Pete Goodliffe. Professionalism
in Programming: Being Specific. In: <span class=
"citetitle"><i class="citetitle">C Vu</i></span>, Volume 12, No 4.
England: ACCU, July 2000. ISSN: 1354-3164.</p>
</div>
</div>
</div>
<div class="footnotes"><br>
<hr class="c4" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e43" href="#d0e43" id=
"ftn.d0e43">1</a>]</sup> It is a (perhaps unfortunate) fact that
good professional programming practice is necessarily tied up with
the economics behind it.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
