    <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  :: Write to Learn</title>
        <link>https://members.accu.org/index.php/articles/552</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 #30 - Feb 1999</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/c174/">30</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+174/">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;Write to Learn</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 February 1999 16:50:52 +00:00 or Fri, 26 February 1999 16:50:52 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="article" lang="en">
<div class="titlepage">
<div>
<div>
<h2><a name="d0e1" id="d0e1"></a>Write to Learn</h2>
</div>
<div class="author">
<h3><span class="firstname">Jon</span> <span class=
"surname">Jagger</span></h3>
<tt class="email">&lt;<a href=
"mailto:jjagger@qatraining.com">jjagger@qatraining.com</a>&gt;</tt></div>
</div>
<hr></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>Why do I write articles for ACCU you might ask? Well, if truth
be told, it's not as altruistic as it appears: I write to learn!
Writing forces me to order my thoughts (which generally end up
slightly less disorganised :-). Readers send me feedback which
usually sparks new thoughts. This ties in with John's last
editorial &quot;We are all continually learning and re-learning, and
that process isn't just listening and reading, it's speaking and
writing&quot;. To try and convince you of this I'm going to write about
what I learned from my own Overload 29 article. I hope this will
convince some of you out there that writing articles is in your own
self interest. Now, where to start? Something trivial</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e22" id="d0e22"></a>How to say
nothing?</h2>
</div>
<pre class="programlisting">
date::date(int dd, int mm, int ccyy) 
  : day(dd), month(mm), year(ccyy)
{
  // empty
}
</pre>
<p>Constructors often do all they need to in their member
initialisation list. Not unreasonable, since constructors
<span class="emphasis"><em>do</em></span> initialisation. The
comment tries to emphasise that the null body is not accidental.
While reading this empty comment I recalled a classically bad
comment (don't laugh now, wait till you see it real code)</p>
<pre class="programlisting">
    value++;   // increment value
</pre>
<p>As I thought about it I realised my empty comment was almost
exactly the same! It wasn't conveying the message I wanted it to:
that the body is <span class=
"emphasis"><em>deliberately</em></span> empty. How to say this
succinctly? For now I've settled on</p>
<pre class="programlisting">
    // all done
</pre>
<p>Here's some more ado about nothing. I noticed my previous
article contained code fragments that used ... in place of large
chunks of code. This is visually confusing as catch-all handlers
also use ellipses<sup>[<a name="d0e43" href="#ftn.d0e43" id=
"d0e43">1</a>]</sup>. So I'm trying something different.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e47" id="d0e47"></a>reference or
value ?</h2>
</div>
<pre class="programlisting">
namespace accu
{
class string
{
public:
  char &amp; operator[](size_t index);
  const char &amp;
         operator[](size_t index) const;
  ... ... ...
  };
}
</pre>
<p>Just after this fragment (again from my previous article) I
wrote &quot;An alternative version of the const array-subscript operator
could return a plain char (by value). There is not much to choose
between the two, ...&quot; While reading this paragraph I decided to it
would be useful to list just what differences there are. There is a
difference worth highlighting. Consider</p>
<pre class="programlisting">
const char &amp; example()
{
  const string greeting(&quot;Hello&quot;);
  return greeting[0];
}
</pre>
<p>If the const subscript operator returns a char reference then
<tt class="function">example</tt> will return a dangling const
reference to the initial char of <tt class="varname">greeting</tt>
which will go out of scope when example returns. Ooops. However, if
the const subscript operator returns a value</p>
<pre class="programlisting">
char string::operator[](size_t index) const;
</pre>
<p>the <tt class="function">example</tt> function will return a
const reference to a <span class="emphasis"><em>copy</em></span> of
the initial char of <tt class="varname">greeting</tt>, and all is
well. As I write <span class="emphasis"><em>this</em></span>
article I've noticed the previous article said &quot;array-subscript
operator&quot;. What have arrays to do with this? Nothing. It's the
string subscript operator. Terminology matters.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e80" id="d0e80"></a>size_t or int
?</h2>
</div>
<p>A reader asked whether the use of <tt class="type">size_t</tt>
as the index type for the subscript operator might be slower than
using a plain <tt class="type">(signed) int</tt>. I can see the
thinking behind this. <tt class="type">size_t</tt> could be
<tt class="literal">typedef</tt>'d to be an <tt class=
"type">unsigned long</tt>, leading to the question of whether
<tt class="type">long</tt> arithmetic is slower than <tt class=
"type">int</tt> arithmetic. Does it matter? It might; it depends on
the context. However my primary concern when writing code is that
my code mirrors my intent. As Dan Saks put it so eloquently at the
ACCU conference &quot;Say it in Code&quot;. <tt class="type">size_t</tt> is
sensible for a string subscript parameter. It says a negative value
doesn't make sense in this context. But suppose the application
needs to be speeded up, profiling shows the string subscript
operator to be a prime candidate for optimisation, and a test
reveals the plain <tt class="type">int</tt> version is indeed
quicker. Would I change the <tt class="type">size_t</tt> to an
<tt class="type">int</tt> in the subscript operators? Well, yes and
no. I'd be tempted to try</p>
<pre class="programlisting">
namespace accu
{
  class string
  {
  public: // types
    class position { ... ... ... };
  public:
    char &amp; operator[](position index);
    char operator[](position index) const;
    ... ... ...
  };
}
</pre>
<p>and give string::position lots of checking. This would force
clients to write</p>
<pre class="programlisting">
for (
  string::position index = 0; 
  index != limit; 
  ++index)
{
  ... ... ... 
}
</pre>
<p>but would allow me to redeclare position if I wanted to</p>
<pre class="programlisting">
typedef int position; 
// OR   
// typedef size_t position;
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e128" id=
"d0e128"></a>string::reference or string::char_reference ?</h2>
</div>
<p>In my code the smart-reference class nested inside <tt class=
"classname">string</tt> was called <tt class=
"classname">char_reference</tt>. A sharp eyed reader asked why I'd
used the name <tt class="classname">char_reference</tt> and not
just <tt class="classname">reference</tt>? After all, the standard
STL containers have a nested type called reference.</p>
<pre class="programlisting">
namespace std
{
  template&lt;typename type&gt; // simplified
  class vector
  {
  public:
    typedef ... ... ... reference;
    ... ... ...
 };
}
</pre>
<p>With a moment's reflection you will quickly see that in a
template container you cannot name the type that <tt class=
"classname">reference</tt> is a reference to because that is the
name of the template parameter type, which of course will vary. My
string class is not a template class, and is not constrained in
this way. I can chose any name for the smart-reference class and
still be &quot;conforming&quot; with a simple typedef</p>
<pre class="programlisting">
namespace accu
{
class string
{
public:
  class char_reference;
  typedef char_reference reference; 
  // idiomatic

public:

  char_reference operator[](size_t index);
  char operator[](size_t index) const;
  ... ... ...
};

class string::char_reference
{
  public:
  ... ... ...
};
}
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e154" id="d0e154"></a>reference or
pointer ?</h2>
</div>
<p>In the article my <tt class=
"classname">string::char_reference</tt> class looked like
this...</p>
<pre class="programlisting">
namespace accu
{
  ... ... ..
  class string::char_reference
  {
    ... ... ...
  private:
    string &amp; s;
    size_t index;
  };
}
</pre>
<p>Ugh, <tt class="varname">s</tt> is an awful variable name.
Something like target is much more expressive. But should it be a
reference? Why not a pointer? I think it's perfectly reasonable for
the string client to write</p>
<pre class="programlisting">
string::reference marker = greeting[0];
</pre>
<p>and I can see the wisdom of using a pointer data member to
emphasise the association between two separate objects with
separate (but related) lifetimes. On the other hand a reference has
to be initialised and cannot be re-bound. But using a reference
might confuse the reader: they might think the smart reference
class is making the raw reference data member smart and the size_t
data member is just some extra unrelated gubbins. Of course I could
make the pointer const. On balance I think I prefer the pointer
version.</p>
<pre class="programlisting">
namespace accu
{
  ... ... ..
  class string::char_reference
  {
public:
  char_reference(string *target,size_t index);
  // default copy constructor OK
public: 
  char_reference operator=(char new_value);
  operator char () const;

private:
  string * const target;
  size_t index;
};
}
</pre>
<p>As I write this I wonder why index is not also const.</p>
<pre class="programlisting">
string * const target;
const size_t index;
</pre>
<p>So, why not this?</p>
<pre class="programlisting">
string * const target;
size_t const index;
</pre>
<p>That somehow seems clearer.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e185" id="d0e185"></a>primitive or
idiomatic ?</h2>
</div>
<p>Some string behaviour was not covered in the previous article.
Obvious examples are comparison and input/output. Here's how I
would do output</p>
<pre class="programlisting">
namespace accu
{
  class string
  {
    ... ... ...
  public: // primitive output

    void write(ostream &amp; out) const;
  };

  // idiomatic output

  ostream &amp; operator&lt;&lt;
       (ostream &amp;out,const string &amp; to_write);
};
</pre>
<p>and the implementation would be</p>
<pre class="programlisting">
namespace accu // string : input/output
{
  // primitive output

  void string::write(ostream &amp; out) const   
  {
    ... ... ... 
  }

  // idiomatic output

  ostream &amp; operator&lt;&lt;
            (ostream &amp; out, const string &amp; s) 
  {
    s.write(out);
    return out;
 }
}
</pre>
<p>The use of &lt;&lt; and &gt;&gt; as streaming operators is very
specific to C++. It's easy to forget this. The difference between
primitives and idioms is important. Primitives seem right during
early design, idioms during late design, as a refinement. It also
seems right that the idioms do nothing except forward to the
primitive (just like &lt;&lt; forwards to <tt class=
"methodname">write</tt>). I mention this in nauseous detail because
it relates strongly to the last section of the article where I
discussed the pro's and con's of making <tt class=
"methodname">string::assign</tt> public or private. Looking at this
again I realise this is really the same primitive/idiom idea.</p>
<pre class="programlisting">
void example(accu::string &amp; s)
{
  // this is the primitive use
  s.assign(0, 'J');
  s[0] = 'J'; // this is idiomatic use
}
</pre>
<p>This has helped me make up my mind. I've settled on making
<tt class="methodname">string::assign</tt> public, and removing the
friendship. I've just noticed a tiny bit of hungarian notation in
my previous article</p>
<pre class="programlisting">
void string::assign(size_t index,char new_ch);
</pre>
<p>Slap. In my defence I plead that I wrote the article tight to
the copy deadline. Here's a fragment of the &quot;final&quot; version. The
primitive and idiomatic access methods are declared together in
their own section. The implementation code chunks at the same
section level.</p>
<pre class="programlisting">
namespace accu
{
  class string
  {
  public:
  
    class char_reference;
    typedef char_reference reference;
    ... ... ...

  public: // access, idiomatic and primitive

    char_reference operator[](size_t index);
    char operator[](size_t index) const;

    void assign(size_t index, char new_value);

  private:
    ... ... ...
  };

  class string::char_reference
  {
  public:
    char_reference
                (string *target,size_t index);
    ... ... ...
  private:
    string * const target;
    size_t const index;
  };
}

// string : access, primitive and idiomatic
namespace accu 
{
  // primitive

  void string::assign
               (size_t index, char new_value) 
  {
    bounds_check(index);
    unshare_state();
    text[index] = new_value;
  }

  // idiomatic

  string::char_reference
  string::operator[](size_t index)
  {
    return char_reference(this, index);
  }

  char string::operator[](size_t index) const
  {
    bounds_check(index);
    return text[index];
  }
}

// string::char_reference - assignment
namespace accu 
{
  string::char_reference 
  string::char_reference::operator=
  (char new_value)
  {
    target-&gt;assign(index, new_value);
    return *this;
  }

  string::char_reference::
  operator char() const
  {
    // this was ro in the previous article &#61516;
    // a needless abbreviation
    const string &amp; readonly = *target;
    return readonly[index];
  
  }
}
</pre>
<p>I'm constantly amazed just how often apparently simple code
benefits from further simplification. The above is simpler than the
previous article in two ways. The unfettered use of primitives is
one. The other is the visual separation of the two class
definitions. In other words, I don't write</p>
<pre class="programlisting">
namespace accu
{
  class string
  {
  public:
  
    class char_reference
    {
    ... ... ...
    };
    ... ... ...
  };
}
</pre>
<p>Here's a thought. Should the <tt class=
"classname">char_reference</tt> conversion operator be const?
Suppose someone writes</p>
<pre class="programlisting">
const string::char_reference eh = s[0]; 
cout &lt;&lt; eh &lt;&lt; endl;
</pre>
<p>In an expression a reference will automatically &quot;decay&quot; into the
thing it is a reference to: a reference is implicitly const, so the
explicit const is meaningless. However, if the conversion operator
was non-const the second line would no longer compile. This is a
step too far. Don't forget code such as</p>
<pre class="programlisting">
void parameter
       (const string::char_reference &amp; ah)
{
  cout &lt;&lt; ah &lt;&lt; endl;
}
</pre>
<p>or (and this is a clincher), code like this</p>
<pre class="programlisting">
template&lt;typename type&gt;
void parameter(const type &amp; ah)
{
  cout &lt;&lt; ah &lt;&lt; endl;
}
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e236" id="d0e236"></a>expressions
or types ?</h2>
</div>
<p>In the article I wrote the subscript operator with the
<tt class="literal">&amp;</tt> token and the <tt class=
"literal">operator</tt> token together. A comment from Sean
Corfield made me re-look at this. He pointed out that Stroustrup
consistently uses a different style, one where the <tt class=
"type">char</tt> token and the <tt class="literal">&amp;</tt> token
have no intervening whitespace. It's easier to see it than read
it</p>
<pre class="programlisting">
// Previous article version
char  &amp;operator[](size_t index);

// Stroustrup version
char&amp;  operator[](size_t index);

// This article. I'm trying it out
char &amp; operator[](size_t index);
</pre>
<p>To take the simplest example, lets look at this bit of C</p>
<pre class="programlisting">
int answer = 42;
int  *ptr = &amp;answer
*ptr = answer;
</pre>
<p>This is how Kernighan and Ritchie declare pointers in their
white book<sup>[<a name="d0e261" href="#ftn.d0e261" id=
"d0e261">2</a>]</sup>. They decided to make the syntax of a
declaration mirror the syntax of an expression. Hence the
<tt class="literal">*</tt> and the <tt class="varname">ptr</tt> are
together in both. The effect is the declaration emphasises how to
use the identifier in an expression. Which is exactly the point I
think they intended. But in C++ I think Stroustrup would write</p>
<pre class="programlisting">
int answer = 42;
int*  ptr = &amp;answer;
*ptr = answer;
</pre>
<p>Why the difference? Well, Stroustrup emphasises the <span class=
"emphasis"><em>type</em></span> of <tt class="varname">ptr</tt> in
the declaration of <tt class="varname">ptr</tt>. Which again is
exactly the point I think he intends. In other words in C the focus
is on expressions, whereas in C++ the focus is on types. A natural
consequence of this is that Stroustrup never declares more than one
pointer in a declaration. If he did he would have to write
something like</p>
<pre class="programlisting">
int*  ptr, *another; / version 1
</pre>
<p>Note however that Stroustrup is quite happy to declare more than
one <span class="emphasis"><em>value</em></span> in a single
declaration<sup>[<a name="d0e291" href="#ftn.d0e291" id=
"d0e291">3</a>]</sup>. How would Stroustrup declare two pointers of
the same type? Perhaps he'd write this</p>
<pre class="programlisting">
int*  ptr;           // version 2
int*  another;
</pre>
<p>Is this different to version 1? In a sense it is. In version 1
if I change <tt class="type">int</tt> to <tt class=
"type">double</tt> I'm changing the type of <tt class=
"varname">ptr</tt> <span class="emphasis"><em>and</em></span> the
type of <tt class="varname">another</tt>. To change them
<span class="emphasis"><em>both</em></span> in version 2 I have to
edit <span class="emphasis"><em>twice</em></span>. In effect
version 1 is saying <tt class="varname">ptr</tt> and <tt class=
"varname">another</tt> are <span class=
"emphasis"><em>deliberately</em></span> the same type, whereas
version 2 is saying <tt class="varname">ptr</tt> and <tt class=
"varname">another</tt> are coincidentally the same type. Of course
you could write</p>
<pre class="programlisting">
typedef int*  int_pointer;
int_pointer   ptr, another;
</pre>
<p>Another point of interest is that all the introductory QA C++
courses use a third style using two spaces...</p>
<pre class="programlisting">
int  *  ptr = ...;
int  &amp;  ref = ...;
</pre>
<p>The rationale is simple. We want each token to be clearly
visible: we don't really want to lexically bind the asterisk to the
type name &quot;more&quot; than to the identifier name. After all, C++
newcomers have enough to cope with as it is!</p>
<p>That's all for now.</p>
</div>
<div class="footnotes"><br>
<hr class="c2" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e43" href="#d0e43" id=
"ftn.d0e43">1</a>]</sup> And in a variadic function but you're not
using those in C++, right?</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e261" href="#d0e261" id=
"ftn.d0e261">2</a>]</sup> The C Programming Language</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e291" href="#d0e291" id=
"ftn.d0e291">3</a>]</sup> Thanks to Kevlin Henney for pointing this
out</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
