    <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  :: Software As Read</title>
        <link>https://members.accu.org/index.php/articles/339</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 #57 - Oct 2003</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/c155/">57</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+155/">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;Software As Read</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 02 October 2003 22:56:14 +01:00 or Thu, 02 October 2003 22:56:14 +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>Programming is writing, and writing is visual. We should explore
software as read not code as executed. Less code, more
software.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e22" id="d0e22"></a>Iteration</h2>
</div>
<p>In his Overload 45 (October 2001) article, <i class=
"citetitle">minimalism - omit needless code</i>, Kevlin worked on
the simple problem of printing the <tt class=
"classname">std::string</tt>s inside a <tt class=
"classname">std::vector</tt> to <tt class=
"classname">std::cout</tt>. An early version looked like this:</p>
<pre class="programlisting">
typedef vector&lt;string&gt; strings;
typedef strings::iterator iterator;
for (iterator at = items.begin();
at != items.end(); ++at) {
  cout &lt;&lt; *at &lt;&lt; endl;
}
</pre>
<p>A later version looked like this:</p>
<pre class="programlisting">
class print { ... };
for_each(items.begin(), items.end(),
         print(cout));
</pre>
<p>And the final version looked like this:</p>
<pre class="programlisting">
typedef ostream_iterator&lt;string&gt; out;
copy(items.begin(), items.end(),
     out(cout, &quot;\n&quot;));
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e49" id=
"d0e49"></a>Readability</h2>
</div>
<p>The main source of repetition is repetition. When programming in
C++ you often find yourself making a call to a template function
where two of the arguments are created by calling begin and end on
a container. This quickly gets repetitive. The repetition itself
suggests several solutions. Ranges are basic building blocks of the
STL design and it is surprising they are not a visible and explicit
artefact of its type system. For example:</p>
<pre class="programlisting">
template&lt;typename iterator&gt;
class range {
public: // types
  typedef iterator iterator;
public: // c'tor
  range(iterator start, iterator finish)
    : from(start), until(finish) { }
public: // properties
  iterator begin() const {
    return from;
  }
  iterator end() const {
    return until;
  }
private: // state
  iterator from, until;
};
</pre>
<p>This would make containers substitutable for a range over
themselves which would in turn allow STL algorithms to expect a
range argument rather than two iterator arguments. For example:</p>
<pre class="programlisting">
template&lt;typename range,
         typename function&gt;
function for_each(const range &amp; all,
                  function apply) {
  return for_each(all.begin(), all.end(),
                  apply);
}
</pre>
<p>This version of <tt class="function">for_each</tt> is not part
of STL so you have to provide it yourself. Once you've done this
you can rewrite this:</p>
<pre class="programlisting">
for_each(items.begin(), items.end(),
         print(cout));
</pre>
<p>as the impressively readable:</p>
<pre class="programlisting">
for_each(items, print(cout));
</pre>
<p>Understanding this statement is a complete no brainer. It
clearly and concisely expresses its intention. However, it does
require you to create the <tt class="classname">print</tt> class
(which hides away the &quot;\n&quot; detail). Alternatively, you could pull
the same trick by writing a non standard version of <tt class=
"function">copy</tt>:</p>
<pre class="programlisting">
template&lt;typename range, typename output&gt;
output copy(const range &amp; source,
            output sink) {
  return copy(source.begin(),
              source.end(), sink);
}
</pre>
<p>allowing the beautifully readable:</p>
<pre class="programlisting">
copy(items, out(cout, &quot;\n&quot;));
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e85" id="d0e85"></a>Preference</h2>
</div>
<p>Which versions do you prefer? The explicit iteration, the
<tt class="function">for_each</tt> versions, or the <tt class=
"function">copy</tt> versions? Can you explain why?</p>
<p>I prefer the <tt class="function">copy</tt> versions. The name
<tt class="function">for_each</tt> is itself a subtle but strong
hint that iteration is involved. It suggests that each of the items
will be printed to <tt class="classname">std::cout</tt>, one at a
time. The iteration comes first (<tt class=
"function">for_each</tt>, leftmost), followed by the action (print,
rightmost). In contrast, the <tt class="function">copy</tt> is
subtler and simply suggests copying the items to <tt class=
"classname">cout</tt>. It has more of a &quot;single operation&quot; feel to
it. The iteration is not visible (and the &quot;\n&quot; is). This difference
is important, not because you should always try to hide all
iteration, but because the intention was to &quot;write the items to
<tt class="classname">cout</tt>&quot;. In other words, the <tt class=
"function">copy</tt> version is a simpler and more direct
expression of the problem. Lots of code is too solution focused; it
lacks an expression of the problem and hence is hard to understand
and costly to maintain.</p>
<p>Many thanks to Kevlin for an insightful review of a first draft
of this article.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
