    <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  :: Mutate? or Create?</title>
        <link>https://members.accu.org/index.php/journals/380</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 #51 - Oct 2002 + Programming Topics</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/c194/">51</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/c65/">Programming</a>
                    (877)
<br />

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

                    -                        <a href="https://members.accu.org/index.php/journals/c194+65/">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;Mutate? or Create?</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 02 October 2002 22:58:12 +01:00 or Wed, 02 October 2002 22:58:12 +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>
<p>&quot;Who is the audience?&quot; - is a key question to consider for any
piece of writing and one that programmers often forget to address.
Just because the language is C++, Java or Cobol doesn't relieve the
author of the responsibility to consider the audience. The code
that I write to explore an idea for myself will look very different
to the code I write to illustrate the idea in an article and the
code that uses the idea in a production environment will look
different to both of these.</p>
<p>It is interesting to hear some of the answers programmers give
when asked to describe the audience for whom they are writing. The
most common answer is to deny that the question is meaningful.
Other answers include &quot;there isn't one&quot; and &quot;the compiler&quot;. And, to
judge from most of the code I see, this is the belief in even more
cases than the frequency of these answers suggests.</p>
<p>When I write production code the audience splits into two
categories: The main audience is those that will use it from their
code but there is also a smaller group who will maintain it. (And
the audience for this article also writes code for these
audiences.)</p>
<p>What follows describes the meeting between a typical developer
and an audience that will be making use of it. The context of this
meeting is a review of the public interfaces to the classes in a
package prior to implementing them. Present at the review are
George (the author), Alice (the system designer) and Harris
(another programmer on the project). While this is a true account I
have coloured the events and changed the names.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e28" id="d0e28"></a>What do we mean
by &quot;add&quot;?</h2>
</div>
<p>George, Alice and Harris filed nervously into the room. This was
the first review of &quot;class designs&quot; and none of them knew what to
expect. George had followed my instructions and supplied Javadoc
documentation of the package containing the classes being reviewed.
Everyone had a printed copy.</p>
<p>I recited the prayer customary on these occasions: &quot;we are here
to help George improve his design, to identify issues he needs to
consider more fully; we are not here to score points or to do the
design ourselves.&quot;</p>
<p>The classes to be reviewed related to the manipulation of
measures that have values, units and dimensions (e.g. [100, metres,
length] or [5, kilos, weight]). Much can be (and has been) said
about combining disparate units and commensurate dimensions. It has
been said many times, by many people and in many places. I'm not
going to repeat it here! Instead, I want to focus on the discussion
of these two methods in the Value class:</p>
<pre class="programlisting">
  class Value implements Cloneable {
      public static Value add(Value a, Value b)
      public Value add(Value v)
      ...
  }
</pre>
<p>I have deliberately given the methods in this form - and not the
Javadocs that were reviewed - because I want you, the reader, to
think about the semantics you would expect from these methods
before reading on.</p>
<p>This link between the name and the semantics is where
considering the audience becomes important. A compiler is not
critical of the choice of names - it doesn't care if a method
called 'add' is used to print a sequence of Goldbach prime pairs. A
programmer, however, might express surprise - even if there was
documentation to that effect.</p>
<p>Alice had her opinion of what the add methods should do and,
when we got to the description of one of these methods, asserted
that it &quot;didn't do what one would expect&quot;. I hope you have formed
your own expectation of what these methods do. How does it
correspond to the following (the documented) behaviour?</p>
<pre class="programlisting">
  /**
   * returns a new Value containing a
   * quantity that is the sum of the
   * quantities held in 'a' and 'b'
   */
  public static Value add(Value a, Value b)

  /**
   * returns a new Value containing a
   * quantity that is the sum of those held
   * in the current object and that supplied
   */
  public Value add(Value v)
</pre>
<p>Alice was not in disagreement about the first of these but the
second led to a lot of discussion. (In the end the description, the
signature and the name of the method all came in for criticism -
but we are getting ahead of the story.) To illustrate her point
Alice took possession of the whiteboard and produced the following
code fragment:</p>
<pre class="programlisting">
  Value accumulate(Iterator i) {
      final Value total = new Value();
      while (i.hasNext()) {
          total.add((Value)i.next());
      }
      return total;
  }
</pre>
<p>(Actually, Alice's code used shorter identifiers &quot;V&quot; for
&quot;Value&quot;, &quot;t&quot; for &quot;total&quot;, but she and I are writing in a different
context and code differently as a consequence.)</p>
<p>She claimed that this code has surprising behaviour. George
responded by asserting that &quot;one shouldn't use a method without
referring to the documentation&quot;. Harris was in full agreement &quot;I
don't care what its called I can see what it does&quot;.</p>
<p>George took over the whiteboard and suggested the 'correct'
code:</p>
<pre class="programlisting">
  Value accumulate(Iterator i) {
      Value total = new Value();
      while (i.hasNext()) {
          total = total.add((Value)i.next());
      }
      return total;
  }
</pre>
<p>I think that it is significant that no one queried what Alice
thought the original code should achieve. In claiming that it was
wrong George and Harris are missing the point - the documentation
of a method should support the name given, not invalidate it. Alice
suggested that the description should read &quot;adds the supplied value
to the current value&quot; or &quot;adds the supplied value to the current
value and returns this&quot;.</p>
<p>George's correction leads to code that has the intended effect
but Harris observed that it creates numerous temporary Values. This
leads to the title question:</p>
<p>Should add mutate a Value instance? Or create a new Value? With
Alice's change the user has the option of both behaviours.
Mutate:</p>
<pre class="programlisting">
  total.add((Value)i.next());
</pre>
<p>or create:</p>
<pre class="programlisting">
  total = Value.add(total,
  (Value)i.next());
</pre>
<p>George accepted that the suggested behaviour would be useful,
but didn't think that the mutating behaviour was reflected by the
name add - if you calculate &quot;1 add 2&quot; then neither of the numbers
changes to &quot;3&quot;.</p>
<p>Harris then suggested that the return type of this add method
should be void. His suggestion that the return type should be void
came from the desire to invalidate <tt class="literal">total =
total.add((Value)i.next());</tt>. This seemed reasonable (but we'll
revisit this after hearing what George had been thinking about name
of the method).</p>
<p>At this point the meeting moved on to other matters. George took
the issues raised away to consider and later circulated a proposal
that resolved them.</p>
<p>After drawing an analogy with the <tt class="literal">+</tt> and
<tt class="literal">+=</tt> operators George suggested that the
correct name would be <tt class="literal">plusEquals</tt>.
Accepting this argument implies that the original static method
should also be renamed plus. In the light of this Harris is
concerned about the analogue of &quot;<tt class="literal">i = i +=
1;</tt>&quot;. This is legal (but clearly daft) for the primitive types
and so, by analogy, &quot;<tt class="literal">total =
total.plusEquals((Value)i.next());</tt>&quot; should also be legal.</p>
<p>Consequently, George proposed (and ultimately implemented) the
following:</p>
<pre class="programlisting">
  /**
   * returns a new Value containing a
   * quantity that is the sum of the
   * quantities held in 'a' and 'b'
   */
  public static Value plus(Value a, Value b)

  /**
   * Adds the supplied value to that
   * contained in the current object.
   * @return this
   */
  public Value plusEquals(Value v)
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e101" id=
"d0e101"></a>Afterword</h2>
</div>
<p>The discussion above took about 15 minutes and involved four
developers. It avoided any developer that used Value writing code
like Alice's example and subsequently spending an indeterminate
amount of time looking for the problem.</p>
<p>The fact that the review forced documentation to be written for
the add method also prevented someone writing code like Alice's
finding the problem and then &quot;correcting&quot; the add method to the
detriment of any code that used it as intended.</p>
<p>Are these changes important? Considered from the compiler's
point of view it makes little difference. From the available
functionality it makes little difference. From the point of view of
providing an unsurprising interface that is easy to use effectively
it does make a difference.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
