    <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  :: Forgetting the ABC</title>
        <link>https://members.accu.org/index.php/articles/796</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 + CVu Journal Vol 17, #2 - Apr 2005</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/c77/">CVu</a>

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

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+97/">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;Forgetting the ABC</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 April 2005 13:16:11 +01:00 or Sun, 03 April 2005 13:16:11 +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>I recently bought and read &quot;C++ Coding Standards&quot; by Sutter and
Alexandrescu [<a href="#Sutter-">Sutter-</a>], without expecting
many surprises. Like most seasoned C++ professionals, I've read
Sutter's &quot;Exceptional&quot; and Meyers' &quot;Effective&quot; series, as well as
numerous articles, which coupled with long experience of using the
language has made me reasonably confident that I don't commit too
many sins.</p>
<p>Indeed, I wasn't surprised by anything I read, although I might
not have agreed completely with everything either. Those are minor
quibbles, though, and not really worth mentioning. What did
surprise me was an omission. Of course, any collection of best
practice guidelines has to tread a line between the terse and
verbose, and on the whole I think the authors have made a good job.
I just wish they hadn't missed what I have always thought of as the
ABC principle - Always Be Conventional.</p>
<p>To be fair, it is covered, albeit in a couple of specific
instances. Items 26 (<span class="emphasis"><em>Preserve natural
semantics for overloaded operators</em></span>), 27 and 28
(<span class="emphasis"><em>Prefer the canonical form[s of +,
+=,++, and the minus equivalents]</em></span>) and 55 (<span class=
"emphasis"><em>Prefer the canonical form of assignment</em></span>)
are all expressions of ABC.</p>
<p>However, while it is important to follow the principle when
dealing with operators, I was surprised that there was no
generalisation of it, since it is a common problem anywhere an
established convention exists.</p>
<p>Here is a simple example I once found in production code:</p>
<pre class="programlisting">
template&lt;typename A_, typename B_,
         typename C_&gt;
struct trio {
  A_ first;
  B_ second;
  C_ extra;
  ...
};
</pre>
<p>This is clearly inspired by <tt class="classname">std::pair</tt>
and while I have omitted all but the essential members, it should
illustrate the point. The struct is well named, implying where the
idea came from, and it can be used as a <tt class=
"classname">pair</tt> would, with a <tt class="varname">first</tt>
and <tt class="varname">second</tt> member. And then there's an
extra member... I can see the logic there, but it's such a silly
thing to do. Why isn't the <tt class="type">C_</tt> member called
<tt class="varname">third</tt>?</p>
<p>Okay, so that's just annoying but not dangerous per se. Here is
another example, again found in production code, which really
boggled me:</p>
<pre class="programlisting">
template&lt;typename A_&gt;
class matrix {
  vector&lt;A_&gt; data_;
  long width_, height_;
public:
  // STL style functions mirroring the
  // std::vector interface

  ...
  void resize(long w, long h) {
    data_.resize(w*h);
    width_ = w;
    height_ = h;
  }
  ...
};
</pre>
<p>This implementation of <tt class="methodname">resize</tt> does
not fulfil my expectations. While the behaviour of the function
does what it says on the tin, it doesn't do enough. By designing
the class interface to resemble that of a standard container, the
programmer has implicitly promised to follow the convention already
established by the STL. If I have data in the matrix and resize it,
I would expect that the elements in the cells that remain in the
matrix are unchanged, which is the canonical behaviour. This is not
the case here, since there is no shuffling around of existing data
if I change the width.</p>
<p>As a user of the matrix class, I will carry with me an
assumption of what the function <tt class="methodname">resize</tt>
will do in a container, based on my knowledge of an established
convention. Any piece of code that draws on such a convention
should adhere to it completely, or clearly document where it does
not. In this example, the offending function should really be
called <tt class="methodname">resize_dirty</tt>, or be rewritten to
conform to the expected behaviour.</p>
<p>These are just two examples - I have seen many, many more.
Neither is it only applicable to operators and STL look-alikes. If
I were to create a new label control using [<a href="#MFC">MFC</a>]
to show the text in rainbow colours, for instance, I would make
sure that the function to provide it with a text to display would
be called <tt class="methodname">SetWindowText</tt> and have the
same form as the canonical one. Sadly, judging from what I've seen
over the years, the common practice would be to call it <tt class=
"methodname">SetText</tt> or <tt class=
"methodname">SetRainbowLabelText</tt>.</p>
<p>ABC is basic wisdom and common sense, just like KISS, and it
bears repeating. While Sutter and Alexandrescu devote four out of
one hundred and one items to special cases, they fail to point out
the underlying general principle. I do not know whether this was
through oversight or because they considered it too obvious, but
personally, I feel it is an important principle that is neglected
too often.</p>
<p>It's just a matter of following conventions where they exist,
fulfilling expectations and avoiding putting in surprises for other
developers (or oneself, in six months time). Sometimes, if all your
friends are jumping off a cliff, you really should do so too.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e101" id="d0e101"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Sutter-" id="Sutter-"></a>
<p class="bibliomixed">[Sutter-] Herb Sutter and Andrei
Alexandrescu, <span class="citetitle"><i class="citetitle">C++
Coding Standards</i></span>, Addison-Wesley, 2004</p>
</div>
<div class="bibliomixed"><a name="MFC" id="MFC"></a>
<p class="bibliomixed">[MFC] Microsoft Foundation Classes, the
class framework created by Microsoft to create GUI applications for
Windows, in which window classes have a <span class=
"bibliomisc"><tt class="methodname">SetWindowText</tt></span>
member.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
