    <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  :: Comments on a Code Review</title>
        <link>https://members.accu.org/index.php/articles/906</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">CVu Journal Vol 11, #5 - 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/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/c130/">115</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;Comments on a Code Review</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 August 1999 13:15:32 +01:00 or Tue, 03 August 1999 13:15:32 +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 class="c2"><span class="remark">responses from the
Harpist</span></p>
<p>Thank you for another thought-provoking article - 'A Big Number
Class'. I have taken the opportunity of adding a few comments and
queries of my own, along with an amended version of your addTo
function (kjm.txt). One point worth noting is that I rarely program
'seriously' in C++ at present (Delphi being more 'suited' to my
current requirements) so some of my comments, from a C++
perspective, may appear rather na&iuml;ve or just
strange&#9786;</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e27" id="d0e27"></a>Constants</h2>
</div>
<p>I assume that you were going to replace the pre-processor
constants; you seem to have got waylaid by the original author's
interesting comments.</p>
<p class="c2"><span class="remark">You are probably
right.</span></p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e35" id="d0e35"></a>Scope:</h3>
</div>
<p>My first reaction was to put them in the class as <tt class=
"literal">static const</tt>s (or <tt class="literal">enum</tt>s for
older compilers) but on closer inspection the constants all seem to
relate to the implementation. I would therefore place them in an
anonymous <tt class="literal">namespace</tt> in the implementation
file, e.g.,</p>
<pre class="programlisting">
// aln.cpp
namespace {
  const int MaxString      = 1050;
  const int MaxAln         = 32700;
  const int WordLength     = 16;
  const int LongWordLength = 4;
  const int MaxNegWord     = 0x8000;
}
</pre>
<p class="c2"><span class="remark">I agree, but I did point out
that the code was written before <tt class="literal">namespace</tt>
was introduced.</span></p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e57" id="d0e57"></a>Class name</h2>
</div>
<p><tt class="classname">aln</tt> is indeed a fairly indifferent
class name; whilst your suggestion of <tt class=
"type">VeryLongInt</tt> seems fine, I would tend towards <tt class=
"type">BigInt</tt> which is quite often used with such classes.</p>
<p class="c2"><span class="remark">Yes, but it gets boring
recycling other peoples names &#9786;</span></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e73" id="d0e73"></a>Private member
functions</h2>
</div>
<p>Again, the names of these functions are rather obscure. I do,
however, feel that this highlights another defect, namely that the
author only comments the obvious. In point of fact he appears to
have misled you with his '// #define' comment. This is, I think,
just a comment and not as one might suspect anything to do with
conditional compilation. It's one of my pet peeves that programmers
rarely comment header files. A simple one-line comment would help
explain most well-written functions; even better if they came with
a statement of pre- and post-conditions.</p>
<p class="c2"><span class="remark">Yes, on re-reading his code I
think he should have written something like:</span></p>
<pre class="programlisting">
// end of #defines
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e84" id="d0e84"></a>Private data
members</h2>
</div>
<p>You queried why <tt class="varname">m_digits</tt> is <tt class=
"type">unsigned int*</tt>; I would suspect that it points to a
dynamically allocated array holding the representation. Whether
it's desirable that the constructors should throw a <tt class=
"exceptionname">bad_alloc</tt> is unclear (without sight of the
class requirements) but it might be simpler to build the array into
the class.</p>
<p class="c2"><span class="remark">I would have made one of two
other choices, either I would have used signed int (it has some
advantages when handling intermediate results) or unsigned char
(and stored a single digit per byte).</span></p>
<p>I'm uneasy about the use of <tt class="literal">static</tt> data
members, esp. <tt class="varname">errnum</tt>. I'm almost certain
that this will cause problems within a multi-threaded en-vironment.
Using error codes instead of exceptions would require a separate
<tt class="varname">errnum</tt> for each thread; doubtless thread
libraries support this sort of thing, but that would render the
solution (unnecessarily) implementation specific. I'm not
particularly au fait with multi-threaded programming, but from my
limited experience I would say that exceptions are safer and
simpler than error codes here.</p>
<p class="c2"><span class="remark">I think you are
right.</span></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e115" id=
"d0e115"></a>Constructors/destructor</h2>
</div>
<p>On a first draft I'd go with the following non-explicit
constructors:</p>
<pre class="programlisting">
  aln(long = 0);      
  aln(unsigned long); 
  aln(string const&amp;);
  aln(aln const&amp;); 
</pre>
<p>There may also be a case for a constructor with <tt class=
"type">char const*</tt> on grounds of efficiency but I'd stick with
<tt class="type">string const&amp;</tt> for the moment. I'd keep
the non-virtual destructor as it appears <tt class=
"classname">aln</tt> is a concrete class.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e133" id=
"d0e133"></a>Exceptions</h2>
</div>
<p>I doubt if the class would be viable today without exceptions.
Without them there is the danger of creating what are quaintly
known as 'zombie objects', i.e., objects which are dead on arrival.
Using error codes, client code needs to query each object after
construction - <tt class="classname">aln</tt> could fail due to a
lack of memory or more likely from incorrect input. Writing generic
code with <tt class="classname">aln</tt> would, as a result, be
problematical.</p>
<p>I would propose a simple exception class to replace error codes,
something like <tt class="exceptionname">aln_error</tt>
perhaps:</p>
<pre class="programlisting">
 struct aln_error {
  errnum n;
  aln_error(errnum err) {n = err;}
};  
</pre>
<p>Public functions setting <tt class="varname">errnum</tt> all
need a <tt class="literal">throw(aln_error)</tt> specifier, all
others should have no throw specifiers. Additionally, the
constructors need a <tt class="literal">throw(bad_alloc)</tt> since
they appear to be allocating dynamic memory.</p>
<p class="c2"><span class="remark">I am less happy with your
exception specifiers unless you are willing to do the work to
capture and process all standard exceptions that maybe thrown from
functions called by the implementation code.</span></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e165" id="d0e165"></a>Debugging
support</h2>
</div>
<p>I'm sure I'm not the only one who would like to hear more of
providing debug support in a separate class. All too many
publications, if they even consider the matter, seem to assume that
assert is the only debug tool available.</p>
<p class="c2"><span class="remark">I think Francis recently wrote a
column on this subject for EXE Magazine.</span></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e173" id="d0e173"></a>abs
function</h2>
</div>
<p>This is the one area I have a couple of qualms with your
solution:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>The member function abs differs from the non-member in that it
is a mutator. This opens up the possibility of a client using it by
mistake. I've committed errors very similar to this and, of course,
worse &lt;sigh&gt;.</p>
</li>
<li>
<p>Where possible, I try to restrict mutators to non-void member
functions. This has improved my class designs; they seem cleaner
and easier to reason about. I would, however, freely concede that
such practice within the C-family is unusual. See B. Meyer's
Object-Oriented Software Construction for a much fuller, if
Eiffel-biased, account (Command-Query Separation principle).</p>
</li>
</ol>
</div>
<p>FWIW my alternative would be the member function:</p>
<pre class="programlisting">
  void changeSignTo(signflag sign) throw() {m_sign = sign;}
</pre>
<p>so <tt class="function">abs</tt> becomes:</p>
<pre class="programlisting">
  aln abs(aln const&amp; item) throw() {
    aln temp(item);
    temp.changeSignTo(plus);
    return temp;
  }
</pre>
<p class="c2"><span class="remark">I agree but for a slightly
different reason (the return from my version should have been by
value); making this a member function results in inelegant syntax
for calling it. Of course it could be a public static function (I
think I prefer that version as it removes the need for a
changeSignTo member function.)</span></p>
<p>At the risk of appearing ignorant or at least more ignorant than
most, I'd like to solicit your opinion on the use of friends in
cases like this. First let me say I'm perfectly happy forwarding
public virtuals to non-member functions instead of the unnecessary
use of friends. However, I wonder where the gain is when the class
(such as <tt class="classname">aln</tt> appears to be) is concrete?
The alternative I considered was to use a private member function
forwarded by a friend.</p>
<p class="c2"><span class="remark">I would turn the question round.
What is the advantage of using <tt class="literal">friend</tt>
declarations when a class is concrete (does not have a virtual
destructor)? It is simpler to use a single consistent method. If
you later decide to change the design so that the class is no
longer concrete it reduces change. Every time you make a decision
you have a chance of getting it wrong.</span></p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e210" id="d0e210"></a>Rationale</h3>
</div>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>The aln class is almost certainly a concrete class. As a result
we have no need to forward a public virtual function to a
non-member function such as would happen, for example, with i/o
functions in a base class.</p>
</li>
<li>
<p>It minimises the public interface, removing potentially harmful
or at least confusing duplication. I would consider a non-member
function, such as abs, a part of the public interface - others, of
course, might not.</p>
</li>
<li>
<p>The friend only accesses a private function (changeSignTo),
rather than the more pathologically inclined friends that access
data. I think this is a more benign form of friendship but maybe
I'm wrong?</p>
</li>
</ul>
</div>
<p>In general I do not disagree, but I still do not find it
necessary. Whenever the intention is to return a modified copy by
value I think that a public static member should be considered.
Granted that the call will need to explicitly specify the class but
that avoids unwanted and unexpected overloading.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e225" id="d0e225"></a>addTo()
member function</h2>
</div>
<p>Assumptions</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p><tt class="literal">length &gt;= 0</tt></p>
</li>
<li>
<p><tt class="literal">rhs[length-1]</tt>, <tt class=
"literal">lhs[length-1]</tt> exist</p>
</li>
<li>
<p><tt class="literal">length</tt> of <tt class="literal">rhs &gt;=
length</tt> of <tt class="literal">lhs</tt></p>
</li>
<li>
<p>all elements of <tt class="literal">rhs</tt> and <tt class=
"literal">lhs</tt> are <tt class="literal">&gt;= 0</tt></p>
</li>
<li>
<p>there will be no overflow</p>
</li>
<li>
<p><tt class="literal">numeric_limits&lt;signed char&gt;::max() ==
0x7F</tt></p>
</li>
</ul>
</div>
<p>typos</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>there is no <tt class="literal">return</tt></p>
</li>
<li>
<p><tt class="literal">|=</tt> used instead of <tt class=
"literal">&amp;=</tt> to clear the carry bit</p>
</li>
</ul>
</div>
<p>revised function</p>
<pre class="programlisting">
bool addTo(IntElem* lhs, IntElem const* rhs, signed char length) throw(){
  // PURPOSE: add rep rhs[0..length-1] to lhs[0..length-1]
  // REQUIRE: length &gt; 0 AND rhs[length-1], lhs[length-1] to exist AND
  //          all elements of lhs and rhs to be &gt;= 0
  // PROMISE: RESULT = true on lhs[0..length-1] = old lhs[0..length-1] + rhs[0..length-1]
  //          RESULT = false on overflow, lhs[0..length-1] undefined
  assert(length &gt; 0);

  for (int i = 0; i &lt; length - 1; ++i) {
    lhs[i] += rhs[i];
    if (lhs[i] &lt; 0) {
      lhs[i + 1]++;
      lhs[i] &amp;= numeric_limits&lt;IntElem&gt;::max();
    } // if
  } // for

  return (lhs[length - 1] += rhs[length - 1]) &gt;= 0;
} // addTo
</pre>
<p>Notes:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p><tt class="literal">typedef unsigned char IntElem</tt> is used
as we might want to change the representation to suit the
underlying hardware.</p>
</li>
<li>
<p><tt class="literal">numeric_limits&lt;T&gt;::max()</tt> is used
instead of <tt class="literal">0x7F</tt></p>
</li>
<li>
<p>Simple tests (including overflow) have been conducted for 1, 2,
4, and 8 byte reps.</p>
</li>
<li>
<p>One slight drawback of using signed integers is if the user, as
seems possible, wants multiples of 8-bits. This would require
handling the most significant array element differently since its
MSB (most significant bit) may not coincide with our carry bit.</p>
</li>
<li>
<p>I don't see an easy way of supporting aln objects with different
array lengths, since this would be difficult to reconcile with a
<tt class="literal">numeric_limits</tt> class. If we forget about
<tt class="literal">numeric_limits</tt> we would then have
difficulty using <tt class="classname">aln</tt> in place of a
built-in integer type. As I'm short of time, I'll stick with a
<tt class="literal">numeric_limits</tt> class, speaking of
which...</p>
</li>
</ul>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e327" id="d0e327"></a>numeric
limits</h2>
</div>
<p>The class needs an associated numeric_limits class defined,
otherwise it will be difficult to use within generic code,
e.g.,</p>
<p class="c2"><span class="remark">Now spotting that puts you close
to C++ expert.</span></p>
<pre class="programlisting">
template&lt;class T&gt; void doSomething(T&amp; n) throw(Overflow) {
  if (n &lt; numeric_limits&lt;T&gt;::max()) {
    n++;
    // further processing...
  } //end if  
  else
    throw Overflow();
} // end of doSomething
</pre>
<p>A first attempt at such a class based on my underused copy of
Stroustrup 3rd ed.</p>
<pre class="programlisting">
class numeric_limits&lt;aln&gt; {
public
  static const bool is_specialized = true;
  static const int digits          = 64;
  static const bool is_signed      = true;
  static const bool is_integer     = true;
  inline static aln min() throw() {return aln(&quot;-0xFFFFFFFFFFFFFFFF&quot;);}
  inline static aln max() throw() {return aln( &quot;0xFFFFFFFFFFFFFFFF&quot;);}
};   
</pre>
<p>operator functions return by <tt class="literal">const</tt>
value I seem to recall this is to prevent their use as lvalues,
e.g., <tt class="literal">a + b = c</tt> should get caught be the
compiler; but as a quiche-eating Delphi programmer I may well be
wrong :)</p>
<p class="c2"><span class="remark">You are correct.</span></p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
