    <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  :: Writing Extendable Software</title>
        <link>https://members.accu.org/index.php/journals/402</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">Overload Journal #49 - Jun 2002 + Design of applications and programs</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/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c196/">49</a>
                    (8)
<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/c67/">Design</a>
                    (236)
<br />

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

                    -                        <a href="https://members.accu.org/index.php/journals/c196+67/">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;Writing Extendable Software</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 June 2002 17:46:10 +01:00 or Wed, 26 June 2002 17:46:10 +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="d0e18" id="d0e18"></a></h2>
</div>
<div class="blockquote">
<blockquote class="blockquote">
<p>&quot;A hallmark - if not the hallmark - of good object oriented
design is that you can modify and extend a system by adding code
rather than hacking it.... In short, change is additive, not
invasive. Additive change is potentially easier, more localized,
less error-prone, and ultimately more maintainable than invasive
change.&quot; (John Vlissides, The C++ Report, February 1998)</p>
</blockquote>
</div>
<p>This is one of my favourite quotes about software development -
and I should apologise for mentioning it in more Overload columns
than perhaps I should. By the time I originally read this quote I
already had several systems under my belt, I had already read the
seminal <i class="citetitle">Design Patterns</i> (<a href=
"#Gamma1995">Gamma1995</a>) and many, many other books so the
essence of the quote shouldn't have come as a surprise to me but it
did. Vlissides pinpoints and explicitly states something that is
only implicit in a lot of writing.</p>
<p>You could view the entire contents of the GoF book as recipes
for extensible code. Maybe, but it was never spelt out quite so
explicitly.</p>
<p>Here, I'd like to spend a bit of time talking about what
extendable code means to me, and look at some mechanisms for
creating extendable systems.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e35" id="d0e35"></a>What is
extensible code?</h2>
</div>
<p>One could argue that all code is extensible because all software
is infinitely flexible. If we wished we could change our washing
machine control software into a nuclear power station control
system - we could do it, but it just isn't the best way to do
it.</p>
<p>All software is infinitely modifiable, this is a big big problem
because the point at which change is impractical is down to
individuals' judgement. The decision is based on one's experience
with the software, overall experience of software and your current
business environment.</p>
<p>While it is possible to change and modify all software, only
software which keeps its original shape and absorbs additions
gracefully is truly extensible.</p>
<p>For example, I once worked on an evaluator for electricity
futures contracts. To add a new type of contract meant: adding a
big chunk of evaluation code, changing the user interface, changing
the main control loop, adding a new case statement to half a dozen
evaluation routines, and a myriad of minor changes throughout the
code base.</p>
<p>Many of the evaluation routines looked something like this:</p>
<pre class="programlisting">
double ContractPaymentMultiplier
                     (int contractType) {
  switch (contractType) {
    case 1 : return 1;
    case 2 : return 0.9;
    ....
    default : assert(false);
  }
  return 1;
}
</pre>
<p>True, this is C not C++ code, a properly object-oriented system
wouldn't be like this but not all C++ is properly
object-oriented<sup>[<a name="d0e52" href="#ftn.d0e52" id=
"d0e52">1</a>]</sup>.</p>
<p>Yes, OO supports extensibility better than procedural code but
it doesn't force extensibility.</p>
<p>A system built like this can be changed, you can add to it but
it involves intrusive changes in many places. Before you could add
anything to this software you had to hack it. To borrow a metaphor
from the days of 640K limited MS-DOS<sup>[<a name="d0e60" href=
"#ftn.d0e60" id="d0e60">2</a>]</sup>, the software could be
<span class="emphasis"><em>expanded</em></span>, but it could not
be <span class="emphasis"><em>extended</em></span>.</p>
<p>An extendable system would allow the new contract to be added
without any changes to the existing source code. Realistically, we
may have to accept &quot;minimum changes&quot; rather than &quot;any changes&quot; but
the point is intrusive changes to existing code should be
minimised, let's say three places at most - OK, I just pulled
&quot;three&quot; out of the air. One or two would be better, but if we are
attempting to separate the system elsewhere (e.g. GUI interface
separated from calculation engine) then centralising all changes at
just one point may break other abstractions.</p>
<p>If my contract evaluator was written as a truly extendable
system I may only need to write a couple of new objects: one to
represent the GUI aspects of the contract and one to represent the
evaluation. Next I would recompile my system with the new objects -
maybe I would need to add them to an existing list of contracts or
maybe there is some magic in the make process that would do this
for me.</p>
<p>To emphasise the point: the system has been changed without need
to change the existing code - even though we may recompile the code
the existing code is unchanged.</p>
<p>This may seem obvious when I write it here but stop for a minute
and dwell on it. Can you do this with your current system? How
would your life be improved if you could make changes like this?
What would it mean if your system could be changed like this? How
can you do this?</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e78" id="d0e78"></a>When is code
extensible?</h2>
</div>
<p>There are three points at which code may be extended:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>compile time : change our source code to pick up new
functionality; this may mean adding new objects in new source code
files and changing a factory function.</p>
</li>
<li>
<p>link time : arrange for changes to be picked up by the linker
only; this may involve some magic for new objects to be found, this
can be self defeating as it inevitably adds some obscurity to the
code and possibly the makefiles too.</p>
</li>
<li>
<p>run time : dynamically loaded libraries were invented for this
sort of thing. This can also lead to obscurity in the code and
usually makes debugging more complex because you may have to wait
for a library to be loaded before you can set break points.</p>
</li>
</ul>
</div>
<p>Although run time extension is truest to the idea of extendable
code (because you don't change any of the existing code) I don't
think this buys much over a good compile-time extension system.
Run-time extension has its uses, such as in very dynamic systems,
or non-stop applications but it also complicates version tracking
and configuration management.</p>
<p>Sometimes the simplest thing is to actually change some of the
existing code. What is simplest and best depends on your
circumstances. Actually adding a line and recompiling will be the
simplest solution.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e97" id="d0e97"></a>Mechanisms for
extending code</h2>
</div>
<p>Many of the classic design patterns are directly concerned with
allowing code to be extended with minimal intrusion. It is easy to
see how command, chain-of-responsibility and factory patterns can
be useful but patterns are not the end of the story. (If this isn't
obvious have another look at the GoF book and think about them for
a few minutes.)</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e102" id="d0e102"></a>Interfaces and
substitutability</h3>
</div>
<p>The key to extensible code is common, well-known interfaces,
which allow one object, module or library to be substituted for
another - the <i class="firstterm">Liskov substitution
principle</i> (see <a href="#Martin1996">Martin1996</a>).</p>
<p>The program framework handles all objects in a common fashion,
no special cases are allowed, it is oblivious to the concrete type
of the object. The same idea lies at the heart of the <i class=
"firstterm">dependency inversion principle</i> - see <a href=
"#Griffiths">Griffiths</a>.</p>
<p>In my extendable contract evaluator example, the framework would
ask the contract to evaluate itself, it has no need to know
anything about the contract class itself, only the interface for
communicating with the contract class.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e123" id="d0e123"></a>State of the
program - data model</h3>
</div>
<p>One problem we quickly run into when adding new objects to an
existing system is that the objects must have access to the state
of the program, that is, the data contained in the system at the
current time.</p>
<p>Again, think of the extendable contract evaluator example.
Before evaluating any contracts the system will load data models of
the supply and demand for the period the contracts are being
evaluated for. It is useful to centralise the data model for the
system so that all contracts have equal access to the data.</p>
<p>Since the data model is used by all contracts we need to ensure
it is accessible. The data model itself may be some easily
accessible object, which contains the pre-loaded data and
configuration information. All contracts have equal access to this
data, there are no special allowances for Contract X to have
special access.</p>
<p>Separating the state out also makes it clear what is data, and
what is algorithms. This simplifies reasoning about the system.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e134" id="d0e134"></a>Dynamically
loaded .DLL/.so</h3>
</div>
<p>Dynamic link libraries, shared libraries in Unix speak, are
loaded by an application at run time, often we are only interested
in them as libraries not their dynamic properties. However, most
OSs allow you to explicitly specify the filename of a library you
wish to load and, once loaded, use the functions contained within -
this provides a powerful extension mechanism.</p>
<p>You can write several DLLs, each with a common set of functions,
and decide which one to load and use at run time, thus you can
extend the program at run time.</p>
<p>However, this comes at a cost. Firstly, you must take more care
with your version management. Instead of having one large
executable to manage you now have several discrete libraries.
Secondly, you must add configuration details to your system so it
knows which DLLs to load.</p>
<p>Finally, there are portability problems. On Solaris the action
of loading a DLL places all symbols in the run-time symbol table so
extra care is required to ensure you don't call a function with the
same name in another DLL, while Microsoft traditionally provide a
stub library to link against.</p>
<p>If we wish to load a DLL and call a function by name the process
is actually quite similar. On Windows we use <tt class=
"function">LoadLibrary</tt> and <tt class=
"function">GetProcAddress</tt>, while on Unix we use <tt class=
"function">dlload</tt> and <tt class="function">dlsym</tt> to the
same effect.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e159" id="d0e159"></a>COM &amp;
CORBA</h3>
</div>
<p>Both COM and CORBA can be used to for extensible systems.
However, the literature on both emphasises different aspects of
each system. Essentially, both implement the loading of dynamic
libraries.</p>
<p>If your system already uses, or you plan to use either COM or
CORBA you can take full advantage to make your program extensible.
However, if you are only interested in their extensibility
properties I would advise against using either of them. There are
simpler techniques (some outlined here) which provide the same
benefit without the cost.</p>
<p>When I say cost I'm not talking about monetary cost - although
simply buying the literature on either product is expensive, and
purchasing a brand name ORB is not cheap - rather I am thinking
of:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>both have steep learning curves : even if you know COM think of
the maintenance requirements</p>
</li>
<li>
<p>both have a reputation for poor performance</p>
</li>
<li>
<p>both force you to design your system around them</p>
</li>
<li>
<p>both have reference counting problems</p>
</li>
<li>
<p>both force you to get into IDL writing which may be overkill</p>
</li>
<li>
<p>COM locks you into Microsoft systems</p>
</li>
<li>
<p>CORBA code can become specific to one vendor's ORB if care is
not taken</p>
</li>
</ul>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e190" id="d0e190"></a>Poor-man's
COM</h3>
</div>
<p>Even without using COM or CORBA you can pass objects out of
dynamic libraries you have loaded. All that is required is three
steps:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>Simply define an abstract base class, e.g.</p>
<pre class="programlisting">
class Base {
public:
  virtual bool Action() = 0;
};
</pre></li>
<li>
<p>In each of your dynamic DLLs provide a concrete implementation
of this base class, e.g.</p>
<pre class="programlisting">
class Concrete : public Base {
public:
  virtual bool Action() { return true; }
};
</pre></li>
<li>
<p>In each DLL provide a <tt class="function">Factory</tt> function
which returns a pointer to your Base class, e.g.</p>
<pre class="programlisting">
Base* Factory() { return new Concrete; }
</pre></li>
</ol>
</div>
<p>You can now write as many objects as you like, each packaged
inside a DLL, and choose which to load at run time.</p>
<p>Of course, should you decide to change the interface on the Base
class you will need to recompile everything in your system. This is
equally true if you change the IDL interface on a COM or CORBA
class.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e218" id="d0e218"></a>Exception
handling</h3>
</div>
<p>It may not be obvious at first that exception handling has a
part to play in writing extensible code but it does, a very
important part.</p>
<p>In the days before exception handling we typically had one file
with a large number of error codes in it, such as <tt class=
"filename">ErrorCodes.h</tt><sup>[<a name="d0e227" href=
"#ftn.d0e227" id="d0e227">3</a>]</sup>.</p>
<p>Whenever a new error was added ErrorCodes.h needed updating and,
since every file in the system depended on ErrorCodes.h, the entire
system needed re-compiling.</p>
<p>By defining a hierarchy of exception classes derived from a
simple base we can allocate error codes and messages as needed. We
may still wish to provide each object with its sub-system code.</p>
<p>When a simple error code is passed up the call stack it is
difficult for the top-level code to take any special action without
knowing specifics about the circumstances. Contrast this with an
exception class, which can itself provide specific methods for such
a circumstance.</p>
<p>For example:</p>
<pre class="programlisting">
class ContractEvaluatorException
                : public std::exception {
public:
  virtual int SubSystem()=0;
  virtual int SpecificCode()=0;
  virtual const char* ExtendedDescription()=0;
  virtual bool EvaluationComplete()=0;
  virtual void StoreEvaluation()=0;
  virtual bool IsFatal()=0;
};
...
int EvaluateAll(std::list&lt;Contract&gt; contracts,
                DataModel&amp; dataModel) {
  for (int i=0; i&lt;=contracts.size(); i++) {
    try { contracts[i].Evaluate(dataModel); }
    catch (ContractEvaluatorException&amp; exp) {
      cerr &lt;&lt; exp.what()
           &lt;&lt; &quot; in subsystem &quot; &lt;&lt; exp.SubSystem()
           &lt;&lt; &quot; code = &quot; &lt;&lt; exp.SpecificCode()
           &lt;&lt; &quot;: &quot; &lt;&lt; exp.ExtendedDescription();
      LogError(exp);
      if (exp.IsFatal()) throw;
      if (exp.EvaluationComplete()) {
        exp.StoreEvaluation();
      }
    }
  }
}
</pre>
<p>The higher levels of the program are still ambivalent to what
was actually happening - beyond the fact that some contract was
being evaluated. Again, the exception system has allowed us to
separate the cause from the effect (see <a href=
"#Kelly2000">Kelly2000</a>) - this is dependency inversion at
work.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e246" id="d0e246"></a>State
machines</h3>
</div>
<p>State machines are particularly good at absorbing extra code.
The simplest state machines (a big switch statement and a whole set
of functions) can have extra states added with little pain but
beware, beyond a certain point the big-switch statement becomes a
pain to maintain.</p>
<p>More advanced state machines can be completely reconfigured at
run time and may use look up tables rather than hard coded
settings. Equally, I have read several articles on object based
state machines over the years.</p>
<p>One of my favourite features of state machines is that they are
very easy to debug and to explain to users. You can sit down with a
piece of paper and trace the route against a diagnostic printout,
or with a user who wants a change.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e255" id="d0e255"></a>Putting it
all together</h2>
</div>
<p>This article draws heavily on my own experience. In these kind
of extendable systems I frequently find a large number of &quot;action
objects.&quot; These are C++ classes which exist for one purpose only,
indeed, as in the example above they may have just one significant
method called <tt class="methodname">Action()</tt>.</p>
<p>To keep these objects decoupled they are usually passed a means
of accessing the program state when they are <span class=
"emphasis"><em>action'ed</em></span>. These objects are ideal
candidates for being placed in a queue and processed sequentially.
Sometimes the processing order is important, sometimes the
processing could be farmed out to worker threads to allow several
objects to be <span class="emphasis"><em>action'ed</em></span> at
the same time.</p>
<p>Another characteristic is that the actioning of the objects may
further populate the queue of objects to be <span class=
"emphasis"><em>action'ed</em></span>. Sometimes this is direct, the
action method will actually add a new item to the processing queue,
other times it is indirect, the action method will trigger some
other process which results in the queue being populated.</p>
<p>In fact, what I have just described is the <span class=
"emphasis"><em>Command</em></span> pattern in a slight
disguise.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e281" id="d0e281"></a>Example
code</h2>
</div>
<p>By the time this article appears I should have some example code
available on my web-site - <a href="http://www.allankelly.net"
target="_top">www.allankelly.net</a>. This demonstrates the use of
dependency inversion to allow extensions to the code and poor-man's
COM system. At the moment the code compiles on Windows 2000 using
either Visual C++ or GCC. Overtime I would like to extend this code
in several directions.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e289" id="d0e289"></a>Summary</h2>
</div>
<p>Extensibility is a worthy design goal. It is the goal of many
design patterns and development techniques but it is seldom stated
explicitly. Once we recognise extensibility as an explicit aim it
is not rocket science. There are a variety of mechanisms for
implementing it and with a little practice it becomes easy.</p>
<p>Of course even the most extensible systems suffer from sodslaw -
change requests can always occur for items your didn't expect to
need changing - the classic <i class=
"firstterm">outside-context-problem</i><sup>[<a name="d0e298" href=
"#ftn.d0e298" id="d0e298">4</a>]</sup>.</p>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e305" id="d0e305"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Banks" id="Banks"></a>
<p class="bibliomixed">[Banks] Banks, Iain. 1996; <span class=
"citetitle"><i class="citetitle">Excession</i></span>, Orbit,
1996</p>
</div>
<div class="bibliomixed"><a name="Bruntlett" id="Bruntlett"></a>
<p class="bibliomixed">[Bruntlett] Bruntlett, Ian 2000; &quot;User
Defined Types: Qualities, Principles &amp; Archetypes&quot;,
<span class="citetitle"><i class="citetitle">Overload
39</i></span>, September 2000.</p>
</div>
<div class="bibliomixed"><a name="Gamma1995" id="Gamma1995"></a>
<p class="bibliomixed">[Gamma1995] Gamma, E., Helm, R., Johnson,
R., Vlissides, J., 1995; <span class="citetitle"><i class=
"citetitle">Design Patterns</i></span>, Addison Wesley 1995 - also
called the Gang of Four book or GoF for short.</p>
</div>
<div class="bibliomixed"><a name="Griffiths" id="Griffiths"></a>
<p class="bibliomixed">[Griffiths] Griffiths, Alan; &quot;Dependency
Inversion&quot;, <span class="bibliomisc"><a href=
"http://www.octopull.demon.co.uk/c++/dependency_inversion.html"
target=
"_top">www.octopull.demon.co.uk/c++/dependency_inversion.html</a></span></p>
</div>
<div class="bibliomixed"><a name="Kelly2000" id="Kelly2000"></a>
<p class="bibliomixed">[Kelly2000] Kelly, Allan 2000: &quot;Error
Handling and Logging&quot;, <span class="citetitle"><i class=
"citetitle">Overload 35</i></span>, January 2000</p>
</div>
<div class="bibliomixed"><a name="Martin1996" id="Martin1996"></a>
<p class="bibliomixed">[Martin1996] Martin, R.C. 1996; &quot;Liskov
Substitution Principle&quot;, <span class="citetitle"><i class=
"citetitle">C++ Report</i></span>, 1996, <span class=
"bibliomisc"><a href=
"http://www.objectmentor.com/resources/articles/lsp.pdf" target=
"_top">www.objectmentor.com/resources/articles/lsp.pdf</a></span></p>
</div>
</div>
</div>
<div class="footnotes"><br>
<hr class="c2" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e52" href="#d0e52" id=
"ftn.d0e52">1</a>]</sup> In fact, the system I'm actually talking
about was actually written in Pascal.</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e60" href="#d0e60" id=
"ftn.d0e60">2</a>]</sup> For those who don't recall. MS-DOS was
limited to 1Mb of accessible memory and 640K of user space.
Initially to get beyond this Lotus, Intel and Microsoft introduced
a system of page swapping which allowed memory to be &quot;expanded&quot;,
think of the memory map getting fatter as different pages were
swapped in and out of the 1Mb memory map. Eventually this system
gave way to &quot;extended&quot; memory where the CPU could access beyond
1Mb, instead of getting fatter the memory map got taller.</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e227" href="#d0e227" id=
"ftn.d0e227">3</a>]</sup> On a side note I urge everyone to avoid
using the word &quot;error&quot; in filenames. Grepping a long compiler log
for errors is much easier if there are no false positives.</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e298" href="#d0e298" id=
"ftn.d0e298">4</a>]</sup> From Iain Banks' novel &quot;Excession&quot; (1996)
- although as far as I know Ian <a href="#Bruntlett">Bruntlett</a>
(2000) was the first to use Outside Context Problem in connection
with software.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
