    <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  :: When is a &quot;Pattern&quot; not a &quot;Pattern&quot;?</title>
        <link>https://members.accu.org/index.php/articles/229</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #61 - Jun 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/c13/">Topics</a>

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

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

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

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

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

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+151/">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;When is a &quot;Pattern&quot; not a &quot;Pattern&quot;?</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 01 June 2004 16:51:15 +01:00 or Tue, 01 June 2004 16:51:15 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<p>We all go through life listening to and telling stories: it is
an important part of human social behaviour. Parents tell children
bedtime stories, we read novels, watch TV and films, and many
successful computer games are based on allowing the player to
participate within a story. Incidentally, one of the things that
that seems to have been lost in many of these formats is the idea
of participation - although, to the annoyance of their mother, my
children re-invented it for bedtime stories (she preferred reading
from a book to collaborative invention).</p>
<p>Stories are also important in the development of computer
software. Not only the stories about how the system will be used,
but also the stories about how it will work. As with the stories in
the wider world they can take a number of forms: Among these are
&quot;design patterns&quot; and that is the form that I want to examine in
this essay.</p>
<p>Before continuing, I should mention that there are those that
think &quot;Patterns&quot; are like the Platonic Ideals to be found in
philosophy: unchangeable and perfect examples of a concept. (This
is not to say that any particular write-up of a pattern is ever
perfect - Plato's Ideals exist in the &quot;real world&quot;, not the mundane
one.)</p>
<p>For me the important aspect of a pattern is that it narrates the
way that a design context is transformed by the application of a
solution. I don't expect to ever meet an idealised design context
and so don't demand that a pattern is the one true solution to a
design problem. (Only that it tells me enough to decide for myself
if applying the solution will advance the task.)</p>
<p>To demonstrate this I am going to present two &quot;patterns&quot; with a
common beginning, one that many developers are familiar with. In
fact, a good number of developers will be familiar with the whole
of both stories to the extent that I'm not going to worry about
some of the formalities of writing about patterns - such as citing
&quot;existing implementations&quot;. These are left as an exercise for the
interested reader.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2>Story Number
1</h2>
</div>
<div class="variablelist">
<dl>
<dt><span class="term">Initial Context</span></dt>
<dd>
<p>When a program is working with numerical values it is necessary
to represent these using the types and names available in the
language. Many languages provide some native data types (such as
the C/C++ types int and double) which support a rich array of
operations and conversions. Some of these operations and conversion
may not be appropriate to the types of numerical value being worked
with. For example, double might provide the range of values and
operations required for working with temperature and pressure.
However, it is possible to make mistakes by, for example, assigning
a temperature value to a pressure variable.</p>
</dd>
<dt><span class="term">Problem</span></dt>
<dd>
<p>How can such (hard to diagnose and expensive to fix) mistakes be
avoided?</p>
</dd>
<dt><span class="term">Solution</span></dt>
<dd>
<p>Encode the problem domain types in the variable names so that
the programmer is reminded of the appropriate use of the variable.
For example, by using a prefix indicating the type of the variable.
Vis: <tt class="literal">cgdEngine</tt> and <tt class=
"literal">ntnsOil</tt>.</p>
</dd>
<dt><span class="term">Resulting Context</span></dt>
<dd>
<p>The developer can see from its name the appropriate manner in
which to make use of a variable. An error like:</p>
<pre class="programlisting">
ntnsOil = cgdEngine
</pre>
<p>is noticeable &quot;at a glance&quot;. However, there is an overhead to
this solution: the developer needs to maintain a catalogue of
problem domain type prefixes and the corresponding types within the
programming language. Note that this catalogue may be explicit
(written down) or implicit (what everyone knows).</p>
</dd>
</dl>
</div>
<p>This is a <span class="emphasis"><em>perfectly good
pattern</em></span> that is adequately captured in the above form.
It would gain little from written in a more formal structure (e.g.
Coplien). It could also be written in a less formal manner (e.g.
Alexandrian) and still be considered a valid pattern.</p>
<p>As for existing usage: this is one of several design approaches
referred to as &quot;Hungarian Notation&quot; and there is no shortage of
developers willing to attest to its effectiveness.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2>Story Number
2</h2>
</div>
<div class="variablelist">
<dl>
<dt><span class="term">Initial Context</span></dt>
<dd>
<p>When a program is working with numerical values it is necessary
to represent these using the types and names available in the
language. Many languages provide some native data types (such as
the C/C++ types <span class="type">int</span> and <span class=
"type">double</span>) which support a rich array of operations and
conversions. Some of these operations and conversion may not be
appropriate to the types of numerical value being worked with. For
example, double might provide the range of values and operations
required for working with temperature and pressure. However, it is
possible to make mistakes by, for example, assigning a temperature
value to a pressure variable.</p>
</dd>
<dt><span class="term">Problem</span></dt>
<dd>
<p>How can such (hard to diagnose and expensive to fix) mistakes be
avoided?</p>
</dd>
<dt><span class="term">Solution</span></dt>
<dd>
<p>Encode the problem domain types as user defined types so that
the compiler enforces the appropriate use of the variable. For
example, types &quot;<tt class="literal">centigrade</tt>&quot; and
&quot;<tt class="literal">newtons</tt>&quot; may be defined so that they only
support the appropriate operations and conversions.</p>
</dd>
<dt><span class="term">Resulting Context</span></dt>
<dd>
<p>An error like:</p>
<pre class="programlisting">
oil_pressure = engine_temperature
</pre>
<p>will not compile. However, there is an overhead to this
solution: the developer needs to maintain a library of problem
domain types and code the support for the allowed operations and
conversions.</p>
</dd>
</dl>
</div>
<p>Like the first story this is a <span class=
"emphasis"><em>perfectly good pattern</em></span> but it is clearly
a different one.</p>
<p>Once again it isn't hard to find developers that are willing to
attest to the effectiveness of this approach.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2>Two Paths to
Choose From</h2>
</div>
<p>What are we to make of this? We have two &quot;patterns&quot; that start
from the same position, and apply different solutions with
&quot;success&quot;. Which design should we choose for our work?</p>
<p>Well, it would be easy if one solution always leads to a better
result than the other does. Then one pattern would be better than
the other (and closer to that Ideal). But this turns out not to be
the case - it is not always the same solution that produces the
better result.</p>
<p>Clearly both approaches resolve the problem stated in the
initial context. But when the two resulting contexts are examined
closely it becomes apparent that a careful assessment of costs and
benefits is required. Specifically: we have to chose between the
cost of maintaining the &quot;prefix catalogue&quot; or of maintaining the
&quot;type library&quot; described in the two solutions. (This may appear to
be lower in the first story as the catalogue need not be maintained
explicitly.) At the same time we have to choose between the
benefits of visual and compiler based error detection.</p>
<p>In some circumstances the second solution may not even be an
option: the cost of implementing user-defined types may be so high
as to be prohibitive - in some languages there is no support for
it. (Although it may be possible to add them - some &quot;lint&quot; style
tools add the notion of &quot;strong typedef&quot; to C for this purpose.)
Even where possible in the language, the developer skills available
to the project may not be up to providing the desired user defined
types reliably.</p>
<p>It is far rarer that the first solution isn't available:
languages whose maximum identifier length is significantly
constrained are thankfully rare these days. But there are occasions
where identifiers are displayed to users who would not appreciate
these prefixes.</p>
<p>It could even be worse: the initial context may sometimes be
better than either of the resulting contexts described above. This
happens whenever the cost of introducing a new type is higher than
the risk involved in reusing an existing type - or when the
language doesn't allow user-defined types or long identifiers.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2>Will the True
Pattern Please Stand Up?</h2>
</div>
<p>What is this: two (or three) patterns that might apply to the
same problem? That doesn't fit with the popular view of patterns as
a template for solving problems: match the initial context and the
problem, and the solution follows automatically.</p>
<p>One way to defend this viewpoint is to insist that there are
additional forces at work, ones not captured by the initial
context. But look at what these forces are: the cost and/or
benefits of aspects the resulting context. Discussing these as part
of the initial context would seriously unbalance the story and lead
to turgid prose. No, there is no escaping from it: there is still a
role for the designer! These patterns don't replace thought - you
still have to decide which option is better.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
