    <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  :: Editorial: The Value of What You Know</title>
        <link>https://members.accu.org/index.php/journals/234</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 #62 - Aug 2004 + Journal Editorial</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/c150/">62</a>
                    (8)
<br />

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

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c184/">Journal Columns</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c185/">Editorial</a>
                    (221)
<br />

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

                    -                        <a href="https://members.accu.org/index.php/journals/c150+185/">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;Editorial: The Value of What You Know</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 09 August 2004 22:52:10 +01:00 or Mon, 09 August 2004 22:52: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="d0e20" id="d0e20"></a></h2>
</div>
<p>One of the things that constantly surprises me is the
differences in value placed upon knowledge by those that have it
and those that lack it. It often seems that anything that one knows
is considered trivial or easy - and that anything one doesn't know
is correspondingly complicated or hard.</p>
<p>Thus it is not uncommon to see a developer who spent days or
weeks learning how to manage a technology expecting another to
&quot;pick it up&quot; in a matter of minutes. Naturally, as developers are
not a race apart, this also happens in other areas of endeavour:
I've seen chess players run through the moves and rules in less
than a minute and expect the explanation to be understood. Or those
versed in cooking giving explanations of a recipe that would only
make sense to someone that already knew most of the answer.</p>
<p>On the other end of such discussions the slightest confusion or
ambiguity becomes a major setback and an obstacle to understanding.
However, to my bemusement, the enigmatic mutterings are not seen as
a failure of explanation but as a failure of understanding.</p>
<p>This is the context in which Overload operates: we all have
pieces of knowledge that may be of use to others - but we fail to
see the value in them and often lack the expertise to explain them.
There can be very few of you reading Overload that do not have some
knowledge or expertise to share, and the &quot;readers&quot; are here to
provide assistance in conveying such expertise in a manner that is
comprehensible. One only has to note the number of authors that
feel the need to acknowledge their assistance to realise that this
assistance is both necessary and valued. But most importantly it is
available.</p>
<p>Why am I telling you all this? It is because Overload is
dependent upon the willingness of ACCU members to write for it.
And, despite the increasing number of ACCU members the number
writing for Overload is not healthy. We get by but, on this
occasion, it was only achieved by the last minute efforts of a
number of contributors who responded to a plea from the editor. To
avoid placing that pressure on them again, I'm making a plea now:
please make a contribution to Overload. <span class=
"emphasis"><em>This means you!</em></span></p>
<p>Think back over the last week: how many things have you
explained to other developers? How many of these do you understand
well enough to think, &quot;no-one would be interested in that&quot;? Well,
those are the things that you are expert enough on to write about.
Try it - most of the authors find that the feedback they get makes
the effort worthwhile.</p>
<p>And speaking of feedback: I'd like to thank all of those that
helped with this issue, especially those that contributed to the
last minute effort to fill the pages. I know that this time of year
there are other demands on your time.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e38" id="d0e38"></a>Three
Perplexing Properties</h2>
</div>
<p>There is rhetorical value in the number three (&quot;The Three
Bears&quot;, &quot;The Three Billy Goats Gruff&quot;, or any number of political
speeches). And it is also said that accidents happen in threes. I'm
sure that the following three incidents don't quite qualify as
accidents - there was a certain amount of intent involved. But they
certainly represent missteps, did come as a triplet, and reflect
some of the difficulties that occur when working in our field.</p>
<p>There is often a need to store arbitrary elements of
configuration information and developers in many programming
languages have come up with the same approach: store an associative
collection of strings mapping keys to values. The keys provide
convenient tags for values to be retrieved without the collection
needing to be written with any knowledge of the information stored.
And, since most languages allow values of various types to be
represented as string values this affords the necessary degree of
flexibility. Sometimes other types of value are used as a key (see
&quot;The tale of a struggling template programmer&quot; [Overload 61] or its
sequels [Overload 61, 62]), sometimes it is possible to store the
values without converting their type.</p>
<p>In Java I've use the <tt class="classname">Properties</tt> class
for this purpose and wrote a translation of this design in C++ for
a recent project. The translation wasn't exact - there are a number
of design decisions in the Java libraries that I find questionable.
For example, the Java library treats <tt class=
"classname">Properties</tt> as a specialisation of <tt class=
"classname">HashMap</tt> whereas my implementation used delegation
to a <tt class="classname">std::map</tt>. In a strongly typed
language why allow a <tt class="classname">Properties</tt> class
(which specifically contains <tt class="classname">String</tt>s for
both keys and values) to be treated as a <tt class=
"classname">HashMap</tt> (that can contain arbitrary objects).
Anyway, I did things my way and refused to expose the container
interface.</p>
<p>The implementation language forced another difference - C++
doesn't allow null references, so I had to decide what to do when
an invalid key was supplied. My decision caused a lot of discussion
during the code review (yes, this client has code reviews; no, the
moderators don't stop digression into solutions). What did I decide
to do? Well, as I intend to cover the design options later, I'll
avoid that discussion at this point.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e70" id="d0e70"></a>First
Motif</h2>
</div>
<p>The code went into production and I didn't look at it for
several months. I had no reason to until I happened to revisit some
code that had used it - when I noticed that the classname had
changed. Curiosity aroused I went to have a look. During the
project lifecycle the original properties class had been renamed to
foo_properties and &quot;improved&quot; by giving it a constructor that
parsed a domain specific string format and a member function to
produce such a string. This format (similar to field value pairs in
a URL) contains embedded key/value pairs. The developer in question
needed this functionality and was evidently convinced that this was
the right class to support this functionality. (After all there was
no other class to which these functions belonged!) There is even
prior art: the Java library <tt class="classname">Properties</tt>
class can serialise and deserialise itself.</p>
<p>Personally I didn't (and don't) see why these functions belonged
as part of this class. It already did one thing well, and adding a
second role makes it less, not more usable. The class didn't need
these functions to perform its role: they could be implemented
efficiently via its public interface. And, if cohesion isn't a
strong enough argument then consider the coupling introduced: the
<tt class="classname">properties</tt> class was now attached to
this <tt class="classname">foo</tt> string format.</p>
<p>There are several points to be considered here: when the change
was made there was only one client of the <tt class=
"classname">properties</tt> class, so it was simple to make the
change. It is also a good pocket example of how adding
functionality to a component reduces its reusability. For many of
our colleagues this is counter-intuitive and a good supply of
examples is needed to convince them otherwise.</p>
<p>By the time I saw it, the code was in production (changes manage
to achieve that without being reviewed) and there was enough work
to do without fixing things that were not manifesting problems to
the user. (Like fixing things that didn't work.)</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e93" id="d0e93"></a>Second
Motif</h2>
</div>
<p>&quot;How do I return a NULL string?&quot; came the voice from behind me.
Like most of these questions it is worth finding out a bit of the
context: I could guess that the language was C++, but why would
anyone want to return a NULL <tt class=
"classname">std::string</tt>?</p>
<p>The answer to that wasn't illuminating: &quot;because that's what I'd
do in C&quot;. My colleague knew that in C a string is represented as a
<tt class="type">char*</tt> and that can be NULL. But he also knew
that a C++ <tt class="classname">std::string</tt> cannot be NULL.
Stop. Rewind. What was he trying to do?</p>
<p>It turns out that the problem is how to indicate a lookup
failure in an associative map of <tt class="classname">string</tt>
key to <tt class="classname">string</tt> value. And, as with the
way my colleague would have implemented it in C, returning NULL is
exactly the choice made in the Java library's <tt class=
"classname">Properties</tt> class:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p><tt class="literal">public String getProperty(String
key)</tt></p>
<p>Searches for the property with the specified key in this
property list. If the key is not found in this property list, the
default property list, and its defaults, recursively, are then
checked. The method returns null if the property is not found.</p>
</blockquote>
</div>
<p>So there are precedents for returning NULL. Also, in the C++
standard library is a similar solution in a different but analogous
situation. From &quot;Associative container requirements&quot;
(23.1.2/7):</p>
<div class="blockquote">
<blockquote class="blockquote">
<p><tt class="literal">a.find(k)</tt></p>
<p>returns an iterator pointing to an element with the key
equivalent to k , or a.end() if such an element is not found.</p>
</blockquote>
</div>
<p>This may not be NULL, but it is definitely a special value -
with all the difficulties that this causes the client code.</p>
<p>Returning to Java the <tt class="classname">Properties</tt>
class also provides an alternative solution:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p><tt class="literal">public String getProperty(String key, String
defaultValue)</tt></p>
<p>Searches for the property with the specified key in this
property list. If the key is not found in this property list, the
default property list, and its defaults, recursively, are then
checked. The method returns the default value argument if the
property is not found.</p>
</blockquote>
</div>
<p>While we are considering possible solutions there is another one
in the C++ standard library. (From 23.1.1/12)</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>The member function <tt class="methodname">at()</tt> provides
bounds-checked access to container elements. <tt class=
"methodname">at()</tt> throws <tt class=
"exceptionname">out_of_range</tt> if <tt class="literal">n &gt;=
a.size()</tt>.</p>
</blockquote>
</div>
<p>Lets recap: we have seen three possible behaviours in the face
of a request for the value associated with an unknown key:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>Return a special value to indicate failure</p>
</li>
<li>
<p>Return a default value supplied by the caller</p>
</li>
<li>
<p>Throwing an exception</p>
</li>
</ol>
</div>
<p>While these may all be reasonable solutions they are not all
appropriate to every situation. And, there are additional options:
&quot;fallible&quot; return types; stipulating the result as &quot;undefined
behaviour&quot;; aborting the program; user-supplied callbacks and
status flags (of various scopes) indicators are all possibilities
(but less commonly used).</p>
<p>In order to recommend a solution it is necessary to know even
more about the problem. It turns out that my questioner was in the
process of designing a class to hold the configuration parameters
for an application. Hey! That sounds familiar, maybe there is some
prior art - even an existing implementation somewhere?</p>
<p>But before I could look for prior art or a library to use my
questioner was gone with: &quot;the configuration values should be there
- I'll throw an exception. Thanks!&quot;</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e182" id="d0e182"></a>Third
Motif</h2>
</div>
<p>When I started this editorial I had three discussions of
implementations of &quot;Property&quot; classes to discuss. But between then
and now one of them has evaporated into the mists of memory and
been dispersed by the force of ongoing commitments. I'll have to
trust that you have your own story to tell.</p>
<p>I fear that you will.</p>
<p>I wonder: how many times has this particular wheel been
invented?</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
