    <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  :: Religious Issues</title>
        <link>https://members.accu.org/index.php/articles/527</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">Project Management + Overload Journal #33 - Aug 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/c66/">Management</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/c171/">33</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c66+171/">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;Religious Issues</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 August 1999 17:50:54 +01:00 or Thu, 26 August 1999 17:50:54 +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>My intention in writing this item is to start you thinking about
the many places where programmers have strong opinions and
disagreements. Sometimes these concern actual solutions to problems
but more often than not they focus on details such as layout,
naming conventions and apparently low level detail.</p>
<p>Over the last few weeks several items have directed my attention
to how widespread these issues are even among genuinely expert
programmers. There is an article in the current issue of 'The C/C++
Users Journal' about designing an implementing a template class for
mathematical vectors. Then I attended a presentation by Stan
Lippman on class design at the recent SIGS conference in London.
Several things that Stan was saying were wildly at variance with
what I had heard from such other industry gurus as Scott Meyers and
Kevlin Henney. At the same event I heard an excellent presentation
from Herb Sutter (yes I have booked both him and Stan for future
JACC events)on writing exception safe code. Herb reiterated the
requirement that destructors do not throw exceptions. Now a few
issues ago we had a case being made to the contrary.</p>
<p>In every case the people involved are of at least expert rank
and most are life masters (to borrow a rank from the World of
Contract Bridge - now there is an idea, what about ranking
programmers in a similar way?) of programming.</p>
<p>I am going to address several issues in the rest of this
article. Please feel encouraged to contribute items for future
inclusion (so that they can be co-ordinated, send them direct to
me: editor@accu.org). The object of the exercise is not to
determine winners but to understand why different programmers come
up with opposing views. Sometimes the differences maybe because
someone has not understood all the implications of their preferred
approach, but often it will be because different contexts require
different mechanisms. I hope that contributors will provide
reasonable detailed rationales for their views, even better they
will attempt to provide several different ones.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e28" id="d0e28"></a>Comments</h2>
</div>
<p>I do not want to say very much about these but there are several
points that should be kept in mind. The primary purpose of a
comment is to assist readers of the code to understand it. I
personally hate comment that is invasive of the code. In my days as
a Forth programmer I greatly appreciated a mechanism in that
language that allowed me to separate code and comment into
different units (called screens in Forth). While there is a good
case to be made for recording the development and maintenance
history of source code somewhere I question whether it is correct
to place that in the same file (even worse sprinkled through the
source code). I have never seen this done but perhaps those comment
headers that some programmers are so fond off should be hived of
into included files. I wonder if we could persuade some implementor
to provide a special switch that pulls in and displays comment
files or not as the programmer wishes; sort of like the habit I
have seen some programmers use of making comments white on white
when they want to focus on the source.</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e33" id="d0e33"></a>For Novices</h3>
</div>
<p>Unfortunately the comments that most of us were first exposed to
were intended to help us learn a programming language and had
little to do with the needs of production code. A line such as:</p>
<pre class="programlisting">
i++   // increments i
</pre>
<p>is fine in a chapter that is introducing increment/decrement
operators yet it rapidly becomes silly. Unfortunately many authors
fail to make this kind of special use of comment clear. They tell
you the syntax of commenting but never explain the style/semantics
of comments.</p>
<p>Let me give you a slightly more extended example. At novice
level you might see:</p>
<pre class="programlisting">
Mytype();  // declaration of explicit default constructor
</pre>
<p>However at expert level you might see:</p>
<pre class="programlisting">
Mytype(){};  // required to reinstate a default ctor because 
             // a copy ctor has been declared
</pre>
<p>The comment for a novice is vacuous for an expert, while the
expert example would be deeply confusing for a novice.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e52" id="d0e52"></a>For Experts</h3>
</div>
<p>If code is well written, it needs little in the way of comment.
Programmers who insist on commenting the purpose of a variable are
almost certainly choosing poor names for their variables. The use
of such comments actually serves to perpetuate poorly written code
because it acts as a crutch to minimise the damage done by a poor
coding style. Many commenting styles are akin to the way that
formal mathematical proofs are presented. But any likeness is
purely superficial. The reasons part of a mathematical proof is
essential because the writer is often making large leaps based on
insights that may not be obvious to others; source code is not like
that.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e57" id="d0e57"></a>An Exercise</h3>
</div>
<p>Go and study the commenting styles of several reputable authors.
Why do you think the Scott Meyers (<i class="citetitle">Effective
C++</i> and <i class="citetitle">More Effective C++</i>) uses a lot
of end of line comments while Matt Austern (<i class=
"citetitle">Generic Programming and the STL</i>) hardly ever
does?</p>
<p>Try to avoid adopting some author's commenting style until you
understand it. At that stage you may come to understand that it is
inappropriate for your purposes. Comments are written in a context
that is much wider than just the source code they are attached to.
Almost all the religious wars about comments arise out of a failure
to grasp this fundamental point. Perhaps student code does need
masses of comments to help the instructor follow the sometimes
contorted logic of the novice, but the instructor that is more
concerned with quantity than quality is missing the point (and
doing a grave disservice to his clients). Learn to distinguish
between commenting for study and commenting for maintenance: they
meet totally different objectives.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e73" id="d0e73"></a>Code
Layout</h2>
</div>
<p>No I am not going to waste your time by raking over this well
trodden territory. But it is worth thinking about how the different
popular formats work in different contexts. The traditional K&amp;R
style is reasonably economical when placed on the printed page.
Some of the other currently popular formats seem to emphasise the
pattern of the code at the cost of space. Unfortunately many coders
then hide the code pattern with excessive commenting in the body of
the code. Actually I like formats that provide me with plenty of
whitespace (and blank lines) if I am going to print it out so that
I can work on it with a traditional writing tool (pen or pencil).
On the other hand I feel uncomfortable with layout rules that
result in functions exceeding one screen, or that result in lines
wrapping. Code should be readable and the layout should encourage
reading.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e78" id="d0e78"></a>Data is Always
Private</h2>
</div>
<p>One of the odd features of this guideline (often elevated to a
divinely inspired rule) is that there is no way of applying it in
several of the common OO programming languages. This may explain
why some writers have so much difficulty with it. If you are doing
plain procedural programming it may seem a silly burden to impose.
In addition it may get in the way when you are writing a text on
some particular type of programming. For example, if you are
writing about 2D graphics programming it would seem to be
reasonable to use:</p>
<pre class="programlisting">
struct coordinates {
  double x, y;
};
</pre>
<p>even in a book where the programming language is C++. You want
to focus on algorithms for 2D graphics. However if you are
developing code for a commercial 2D graphics library you might be
better advised to write:</p>
<pre class="programlisting">
class coordinates {
public:
  double x()const;
  coordinates&amp; x(double);
  double y()const;
  coordinates&amp; y(double);
  coordinates(double x =0.0, double y =0.0);
private:
  double x_m, y_m;
};
</pre>
<p>And before you start picking at that code, remember the context
here is one of highlighting style in the context of why the code is
written. While I am writing about this specific topic, note that we
should distinguish between co-ordinates and mathematical vectors.
They have many similarities but there are also differences. Both
rely on ordered pairs, but it makes sense to add two vectors but
not to add two sets of co-ordinates. You can even add a vector to a
set of co-ordinates (and get a set of co-ordinates returned). Does
this mean that both should be derived from a common base class? In
a context where even the slightest element of code bloat hurts the
answer is probably yes but I would argue for no in general. If you
need an ordered pair neither a 2D vector nor a pair of co-ordinates
will do. They may be very similar but they are not, to my mind,
substitutable.</p>
<p>There are some who would argue that the data in this example
should be protected so that derived classes have free access to the
base class data. Indeed if you come from Smalltalk you might wonder
what the problem is.</p>
<p>In the above example there would seem to be little cause to
waste time in debating the issue because you have already granted
complete access via access functions that expose the underlying
data types. How often do you see authors raise this question when
dealing with issues of access and data hiding? If you want to
preserve your rights to change data structures as a class designer
you need to do something like:</p>
<pre class="programlisting">
class coordinates {
public:
  typedef double value_t;
  value_t x()const;
  coordinates&amp; x(value_t);
  value_t y()const;
  coordinates&amp; y(value_t);
  coordinates(value_t x =0.0, value_t y =0.0);
private:
  value_t x_m, y_m;
};
</pre>
<p>Much more important than knowing 'data should always be private'
is understanding the implications of what you write. Sound bites do
not transmit wisdom they only act as convenient tags to recover
wisdom.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e99" id="d0e99"></a>Avoid
inline</h2>
</div>
<p>There is a considerable body of experts who will teach you to
avoid inline (and more specifically defining member functions in
the class interface) as if it was some serious disease. On the
other hand, Stan Lippman would criticise the above <tt class=
"classname">coordinates</tt> class because the member functions had
only been declared and not defined. In the context of the kind of
intensive graphics programming that he is accustomed to (Disney is
a client of the company he works for) I think he is quite correct.
However in the context of many other types of work he is probably
wrong. What is wrong is making such decisions about such
fundamental classes without understanding the implications.</p>
<p>Another issue that must be understood here is the interaction
between <tt class="literal">inline</tt> and <tt class=
"literal">virtual</tt>. This does not mean that it is wrong to
define a virtual function as inline, but you need to understand
that the inline feature will only apply when the compiler can apply
static binding.</p>
<p>For example:</p>
<pre class="programlisting">
class Base 
{
public:
  virtual void printon(ostream &amp; out = cout) const {
    out &lt;&lt; &quot;Base class/n&quot;;
  }
};

Base &amp; b_g() {
  static Base * b = new Base;
  return *b
}

int main(){
  Base b;
  b.printon();    // OK can place code inline
  Base * b_ptr = new Base;
  b_ptr -&gt; printon();  // OK can place code inline
  b_g().printon();  // will almost certainly require indirection
  return 0;
}
</pre>
<p>Note that in this example you will get code bloat unless the
compiler stores a single copy of the message (string literal) and
uses that via a pointer everywhere it is needed. I would not bet
that compilers will be that smart (remember that the class would
normally be in a header file that was included into several
translation units.</p>
<p>There are a lot of things that compilers could do to help us but
too often they do not cater to the needs of the implementer's
marketing department. An example in the above is that local
<tt class="literal">static</tt>. For it to work reliably in a
multi-threaded environment such static objects must be created
atomically. In other words all other calls to <tt class=
"methodname">b_g()</tt> must be locked out until the <tt class=
"classname">Base</tt> object has been constructed.</p>
<p>Actually, in this case, any subsequent write access to the
reference returned must also lock against other accesses until
completed. In fact just about all kinds of static variable are a
menace in multi-threaded code. Commercial components and libraries
MUST start certifying whether they are thread save in general and
specifically whether they are thread safe for multiple accesses to
the same variable.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e134" id="d0e134"></a>Is It
Religious?</h2>
</div>
<p>I could write for most of a week on items such as the above but
I have not the time and your editor has not got the space. More
than that, I would be taking away the opportunity for more of you
to contribute.</p>
<p>To my mind a religious issue is one where experts entrench
themselves in lines of battle and start talking in terms of
absolutes. You get a great deal of noise (sound bites shouted ever
more forcibly) and hardly any light. What we need is more light. I
have no problem with Stan Lippman advocating a considerable amount
of inlining (and even highlighting that one cost of using the
virtual mechanism is that you will normally lose such potential
efficiency gains.) What I do have a problem with is advocating it
without pointing out the possible costs of doing so.</p>
<p>To some extent I do not care what you do as long as you
understand the costs and benefits. Religious beliefs are held
without reason (even though the originators may have had good
reasons). Those holding them often have closed minds. Unfortunately
those attacking religious beliefs often have equally closed minds
on the other side. What we need is understanding.</p>
<p>Engineers are often pragmatists, which is fine as long as you
have a clear understanding that what works for water pipes may not
be sensible for gas ones. When you use the nearest pipe as an
earth, do you stop to ask what kind of pipe it is? Do you know if
it makes any difference?</p>
<p>Like idioms and patterns, guidelines are a mechanism for
encapsulating the hard won experience of experts. Like the former
we must understand the context and what we are seeking to
achieve.</p>
<p>Now pick your item and write about the rationale behind it. You
might be surprised at how often the most widely accepted statements
lack the universality they claim. On the other hand once you
understand such as 'do not use goto' you may discover that they
have wider applicability.</p>
<p>If you put your minds to it there is sufficient material to run
a column this size for at least the next five years.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
