    <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  :: Abstraction, Syntax, Intent</title>
        <link>https://members.accu.org/index.php/articles/765</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">Design of applications and programs + CVu Journal Vol 11, #2 - 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/c67/">Design</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/c133/">112</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c67+133/">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;Abstraction, Syntax, Intent</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 February 1999 13:15:29 +00:00 or Wed, 03 February 1999 13:15:29 +00: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>Deep
wisdom</h2>
</div>
<p>There is a fairly well known writing guide called The Elements
of Style [<a href="#Strunk">Strunk</a>]. It contains the proverb
&quot;Omit needless words&quot;. This appears to be a good piece of advice
for a novice writer. But is it? Think about it. Does a novice
writer know which words are needless? Probably not. What about an
experienced writer? There's a deeper meaning, good writers know
which words are needless. Many expressions of deep wisdom are like
this, only revealing the deep meaning when experience allows you to
see past the shallow meaning.</p>
<p>One of the most frequently used examples of abstraction is to
separate an interface from an implementation, the type from the
class. Just like &quot;Omit needless words&quot;, it's hard to grasp the
depth of this advice. It's easy to say, and easy to think you
understand it. But do you? Can you clearly separate out in your
mind what's interface and what's not. It can be hard, especially if
you have a long procedural background. I know it was for me.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e30" id="d0e30"></a>What's
interface and what's not?</h2>
</div>
<pre class="programlisting">
// read this fragment and decide
size_t strlen(const char *);
  const char * greeting = &quot;hello&quot;;
  strlen(greeting);
</pre>
<p>Well, you can see it all, so surely it's all interface. But hang
on, lurking in there is a <tt class="type">char *</tt>, which is a
<span class="emphasis"><em>representation</em></span> of the string
concept. If you're a hardened C programmer it can be hard to
<span class="emphasis"><em>really</em></span> appreciate this. The
<tt class="type">char *</tt> has been right in front of your nose
for so long that you haven't noticed. But think, how do you create
and use a <tt class="type">double</tt>? The answer is without
needing to know how a <tt class="type">double</tt> is represented.
You write</p>
<pre class="programlisting">
double sin(double);double tax = 42.24;sin(tax); 
</pre>
<p>you don't need to write<sup>[<a name="d0e59" href="#ftn.d0e59"
id="d0e59">1</a>]</sup></p>
<pre class="programlisting">
  struct double {
    unsigned int sign     :  1;
    unsigned int exponent :  7;
    unsigned int mantissa : 24;
  };
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e64" id="d0e64"></a>The syntax is
different</h2>
</div>
<p>How does the string example look in C and in C++? Essentially
like this</p>
<pre class="programlisting">
/* C */
size_t strlen(const char *);
const char * greeting = &quot;hello&quot;;
strlen(greeting);

// C++
size_t string::length() const;
string greeting(&quot;hello&quot;);
greeting.length();
</pre>
<p>There is a lot of similarity in the C and C++ versions. If the
C++ <tt class="classname">string</tt> representation is a single
character pointer, the immediate memory footprint of <tt class=
"varname">greeting</tt> will be the same in both cases. The
implementation of <tt class="methodname">string::length()</tt>
might well delegate to <tt class="function">strlen()</tt>. In other
words, under the hood they're very similar, the differences seems
syntactic, superficial. If you look at the assembler generated by a
C compiler you'll find memory values, memory value operations, and
jumps. If you look at the assembler generated by a C++ compiler and
you'll find... exactly the same.</p>
<p>But you weren't programming in assembler: that's the point. And
like the <tt class="type">char *</tt> in front of your nose, it's
so obvious it's easy to miss. A vital difference is that the code
just is different. The syntax is different.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e90" id="d0e90"></a>The job of
syntax</h2>
</div>
<p>At a superficial level syntax matters because it's what the
programmer sees. It's what the programmer reads. The syntax is the
interface to the language. But syntax has a deeper purpose, to
express ideas, to support design. The more expressive the language,
the smaller the gap between a concept and the expression of the
concept in the syntax of the language. In other words, the job of
syntax is to directly and explicitly express your intent. I don't
think it's by chance that we use the words statement, declaration,
and expression when we talk about C and C++. The terminology is
deliberate. A statement is a statement of intent; a declaration is
a declaration of intent; an expression is an expression of
intent.</p>
<p>The declarative power of C++ allows you raise the level of
abstraction in exactly the same way that C did over assembler. When
you program at a higher level of abstraction you can capture more
of the concept simply because the syntax allows you to express more
of the concept.</p>
<p>Let's look at the C and C++ fragment again, this time looking
past the superficial difference in syntax. Think about why the
syntax is different. What concept does the difference express?
Here's the C again</p>
<pre class="programlisting">
  size_t strlen(const char *);     
</pre>
<p>Let's break the code into individual symbols.</p>
<p>The <tt class="type">char *</tt> is the representation of the
concept string, but is not the name of the concept.</p>
<p>The <tt class="literal">const</tt> represents the function
semantics on the target string we're finding the length of. Notice
that for functions with more than one parameter, there is nothing
to distinguish the parameter representing the target from other
parameters.</p>
<p>The <tt class="literal">str</tt> of <tt class=
"function">strlen</tt> refers to the name of the <tt class=
"classname">string</tt> concept, while the <tt class=
"literal">len</tt> of <tt class="function">strlen</tt> refers to
the semantics of the function. Only a human can make this simple
analysis. There is nothing in the <span class=
"emphasis"><em>syntax</em></span> of the <span class=
"emphasis"><em>single</em></span> token strlen that the compiler
can use to separate str from len, or st from rlen, etc. There is
nothing in the syntax of C to group <tt class=
"function">strlen</tt> and <tt class="function">strchr</tt> more
strongly than, say, <tt class="function">strlen</tt> and <tt class=
"function">strerror</tt>. A single token has no declarative power,
even if it is a keyword<sup>[<a name="d0e148" href="#ftn.d0e148"
id="d0e148">2</a>]</sup>. To make your code more declarative you
first have to break it down into separate elements. The declarative
power arises through the very act of explicitly combining the
separate elements together. C++ is very expressive primarily
because it's syntax offers lots of ways to combine elements.</p>
<pre class="programlisting">
  size_t string::length() const;   
</pre>
<p>The token <tt class="function">strlen</tt> has been replaced by
the tokens <tt class="classname">string</tt> and <tt class=
"methodname">length</tt> . There is now a clear separation of
concerns: <tt class="classname">string</tt> names the type,
<tt class="methodname">length</tt> names the function, and vitally,
the <tt class="literal">::</tt> glues them together. The <tt class=
"literal">::</tt> syntactically groups <tt class=
"classname">string</tt> and <tt class="methodname">length</tt>
together. We have said it in code. The <tt class=
"methodname">length</tt> of <tt class=
"methodname">string::length</tt> and the <tt class=
"methodname">find</tt> of <tt class="methodname">string::find</tt>
are strongly bound together because both are bound to <tt class=
"classname">string</tt> using the <tt class="literal">::</tt>
binding. Notice also that there is nothing called <tt class=
"literal">error</tt> bound to <tt class=
"classname">string</tt>.</p>
<p>The <tt class="literal">const</tt> represents the semantics of
the target string we're finding the length of. Notice that for
functions with more than one parameter, there is now a syntactic
difference between the target and other parameters. Again, we've
said it in code.</p>
<p>The char * has vanished. It's nicely hidden inside the
definition of class <tt class="classname">string</tt>.</p>
<pre class="programlisting">
class string {
  ... ... ...
private: // representation
  char * text;
};
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e219" id="d0e219"></a>Summary</h2>
</div>
<p>Here's my definition of design: consciously joining separate
elements to create structure. Note that the elements need to be
separate to be able to be joined together. This relates to
simplicity. If an element is too large it tends to be hard to
understand, hard to maintain, it loses structure, it rusts. Break a
large element down into separate pieces and each piece becomes
simpler, easier to understand, easier to maintain, more cohesive.
Then put the pieces back together in different ways to create
different structures with different strengths and weaknesses. This
is easy to say, but it's harder to do. Most human beings seem to
have great difficulty really embracing simplicity. The best
designers seem to have a knack for seeing the underlying three or
four concepts when you just see one.</p>
<p>That's all for now</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e226" id="d0e226"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Strunk" id="Strunk"></a>
<p class="bibliomixed">[Strunk] <span class="citetitle"><i class=
"citetitle">The Elements of Style</i></span>, William Strunk Jr,
E.B.White, MacMillan, 0-02-418200-1</p>
</div>
</div>
<div class="footnotes"><br>
<hr class="c2" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e59" href="#d0e59" id=
"ftn.d0e59">1</a>]</sup> This is a guess. I really don't know that
much about floating point representation.</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e148" href="#d0e148" id=
"ftn.d0e148">2</a>]</sup> Try it. Go on. What's the fewest number
of tokens that form a legal statement?</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
