    <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  :: Student Code Critique Competition 37</title>
        <link>https://members.accu.org/index.php/articles/855</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">Student Code Critiques from CVu journal. + CVu Journal Vol 17, #6 - Dec 2005</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/c184/">Journal Columns</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c183/">Code Critique</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/c93/">176</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c183+93/">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;Student Code Critique Competition 37</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 06 December 2005 05:00:00 +00:00 or Tue, 06 December 2005 05:00:00 +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="d0e18" id="d0e18"></a></h2>
</div>
<p><span class="bold"><b>Prizes provided by Blackwells Bookshops
&amp; Addison-Wesley</b></span></p>
<p><span class="emphasis"><em>Please note that participation in
this competition is open to all members. The title reflects the
fact that the code used is normally provided by a student as part
of their course work.</em></span></p>
<p><span class="emphasis"><em>This item is part of the Dialogue
section of C Vu, which is intended to designate it as an item where
reader interaction is particularly important. Readers' comments and
criticisms of published entries are always welcome, as are possible
samples.</em></span></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e29" id="d0e29"></a>Before We
Start</h2>
</div>
<p>Remember that you can get the current problem set in the ACCU
website (<a href="http://www.accu.org/journals/" target=
"_top">http://www.accu.org/journals/</a>). This is aimed at people
living overseas who get the magazine much later than members in the
UK and Europe.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e37" id="d0e37"></a>Student Code
Critique 35 - Further Entry</h2>
</div>
<p>Following on from the entry for SCC 35 in the last issue, Jim
Hyslop &lt;<tt class="email">&lt;<a href=
"mailto:jhyslop@dreampossible.ca">jhyslop@dreampossible.ca</a>&gt;</tt>&gt;
wrote:</p>
<p>I'd like to congratulate Simon Farnsworth on a well thought out
critique. I agree with his contention that '&quot;<tt class=
"function">GetReport()</tt> <span class="emphasis"><em>is a
mess.</em></span>&quot; He is also correct in his final statement that
the function should throw an exception. To me, that is the first
and most important point I&amp;rsquo;d discuss, not the last one.
Null references are not allowed, period. They invoke the dreaded
Undefined Behaviour. Do your best not to accidentally create them,
and never, ever deliberately create them.</p>
<p>My take on the class is the opposite of Simon's: it seems to me
that Report is intended to add vector-like behaviour to a map, i.e.
given an index <span class="emphasis"><em>n</em></span>, find the
<span class="emphasis"><em>n</em></span>th entry in the map. Report
<tt class="literal">IS-A</tt> map, with additional features. So,
given our two different interpretations of the intent of the class,
the next important improvement is to beef up the class's comments
to explain in more details the &quot;why&quot; of the class, so that we don't
have to guess at the class's purpose.</p>
<p>One important objective to keep in mind when critiquing other
people's code, whether you are an instructor, mentor, colleague, or
whatever, is not only to say what needs to be done, but why. For
example, Simon recommends using <tt class="literal">iter-&gt;</tt>
rather than <tt class="literal">(*iter)</tt>. Knowing why you
should do this is important. Similarly, why should one use
pre-increment over post-increment? I agree with the recommendation,
and with his reasoning, but his reasoning is a little shallow: in
what way is the code simpler? (I leave answering these questions,
as well as the question: &quot;why is preferring pre-increment over
post-increment not a premature optimization?&quot;, as an exercise to
the reader :-).</p>
<p>I do, however, disagree with his statement that &quot;the optimizer
should fix it for you&quot;. The optimizer can only fix this for builtin
types. The exact type of a container's iterator is
implementation-defined, so an implementation is not required to
actually use a pointer: it could use a class or struct.
<span class="application">gcc</span>, for example, uses a struct as
its iterator. In that case, the compiler cannot arbitrarily
substitute pre-increment for post-increment.</p>
<p>Now, back to the original submission.</p>
<p>I'm assuming that the member function definitions are shown
inline as an SCC convention, in order to conserve space. Member
functions should not, of course, be written as inline until and
unless profiling shows them to be a bottleneck.</p>
<p>Retrieving the <span class="emphasis"><em>n</em></span>th
element in a map can be simplified in user code by taking advantage
of the distance properties of iterators:</p>
<pre class="programlisting">
Report &amp; Reports::GetReport( int index )
{
  if ( index &lt;0 || index &gt;=size() )
    throw out_of_range;
  return (begin() + index)-&gt;second;
}
</pre>
<p>- From 17 lines down to 3. Not bad.</p>
<p>Note that the iterator's <tt class="literal">op+</tt> does not
do any range checking. If <i class="parameter"><tt>index</tt></i>
is negative, or exceeds the size, you will invoke undefined
behaviour (a crash, if you're lucky) so we need to do strict
checking on <i class="parameter"><tt>index</tt></i> before we do
anything else.</p>
<p>This is not necessarily more efficient than the original code,
since iterator's <tt class="literal">operator +(int distance)</tt>
(which is implemented in terms of <tt class="literal">operator
+=</tt>) basically boils down to:</p>
<pre class="programlisting">
  if ( distance &gt;= 0 )
    while ( distance-- ) this-&gt;operator++();
  else
    while ( distance++ ) this-&gt;operator--();
</pre>
<p>but it is easier to read and follow in user code.</p>
<p>If profiling indicates we're spending a lot of time in the
increment, then this can be optimized by determining whether index
is closer to <tt class="methodname">begin()</tt> or to <tt class=
"methodname">end()</tt>:</p>
<pre class="programlisting">
Report &amp; Reports::GetReport( int index )
{
  if ( index &lt;0 || index &gt;=size() )
     throw out_of_range;
  if ( index &gt; size()/2 )
     return (end() - (size() - index))-&gt;second;
  return (begin() + index)-&gt;second;
}
</pre>
<p>On variable naming: What is <tt class="varname">nIndex</tt>?
Well, clearly it's an index, but what is its purpose? If one is
going to use Hungarian Notation then that can be clearly indicated
by using the proper prefix <tt class="literal">i</tt> rather than
the generic <tt class="literal">n</tt> (remember, in true, Simonyi
notation<sup>[<a name="d0e134" href="#ftn.d0e134" id=
"d0e134">1</a>]</sup>, <tt class="literal">i</tt> does not mean
<tt class="type">integer</tt>, it means <tt class=
"literal">index</tt>). <tt class="varname">iter</tt> is, similarly,
clearly an iterator, but again: what is its purpose? The variable
names do not convey any meaning to the reader. The author is also
using a mix of Hungarian Notation, and non-HN. Pick one or the
other, and stick to it (as an aside, the <tt class=
"varname">iter</tt> highlights the problems with using Hungarian
Notation in C++ programming: each time you create a new type, you
need to invent a new prefix. Personal preference: avoid HN, as it
adds very little value.).</p>
<p>So, without good, descriptive variable names, we resort to
studying the code to determine what the variables are. As Simon
pointed out, the member variables seem to be an optimization (side
note: in addition to the robustness concerns he noted, the
optimization is neither thread-safe nor reentrant, although I
suppose that could all be lumped under 'susceptible to changes
between calls'. Side note 2: the author of the code seems to have
recognized at least some of the problems Simon pointed out, since
there is a test to see if <tt class="varname">nIndex</tt> exceeds
the size of the map, implying that the author knows some elements
could be removed between calls to <tt class=
"function">GetReport</tt>). So, um... where was I? Oh, yeah. The
next thing to do is give the member variables meaningful names,
make them private,</p>
<pre class="programlisting">
private:
  int lastIndexRetrieved;
  iterator lastElementRetrieved;
and, of course, initialize them in the ctor:
Reports() : lastIndexRetrieved( 0 ),
   lastElementRetrieved( begin() ) {}
</pre>
<p>Notice that I did <span class="bold"><b>not</b></span> recommend
adding a comment to explain their purpose, I specifically
recommended changing the variable names.</p>
<p>If the robustness concerns Simon noted are addressed (not
likely, but possible if <tt class="function">Reports</tt> is
populated once and not modified), and profiling indicates we are
spending a lot of time incrementing/decrementing iterators, then
<tt class="function">GetReport</tt> can be optimized as
follows:</p>
<pre class="programlisting">
Report &amp; Reports::GetReport( int index )
{
  if ( index &lt; 0 || index &gt;= size() )
     throw out_of_range;
  int distance = index - lastIndexRetrieved;
  lastIndexRetrieved = index;
  lastElementRetrieved += distance;
  return lastElementRetrieved-&gt;second;
}
</pre>
<p>A variation on the <tt class=
"methodname">begin()</tt>/<tt class="methodname">end()</tt>
optimization shown above can be applied, by determining which is
the closest iterator to the desired index: <tt class=
"methodname">begin()</tt>, <tt class=
"varname">lastElementRetrieved</tt>, or <tt class=
"methodname">end()</tt>. If, for example, there are 1000 elements
in the map, the last element retrieved was 14, and we want element
990, then it is faster to count back from <tt class=
"methodname">end()</tt> rather than forward from <tt class=
"varname">lastElementRetrieved</tt>. But again, only if profiling
indicates we're still spending a lot of time iterating through the
map. You may end up spending more time figuring out which element
is closest, than actually spinning through the list!</p>
<p><tt class="function">ClearAll()</tt> appears to me to be a
convenience function, to allow the user to reset all data in the
reports with a single function call, for subsequent re-population.
Given the ownership issues Simon pointed out, we must assume that
some external mechanism is responsible for adding and removing
<tt class="classname">Report</tt> objects to and from the map, and
for cleaning up the data on program termination. There are similar
ownership issues around the key, <tt class="type">Data *</tt>,
which we again must assume some other object will take care of.
Examining these lifetime issues would be part of a general code
review.</p>
<p>In <tt class="function">ClearAll()</tt>, I would suggest the
programmer consider get in the habit of using <tt class=
"function">std::transform()</tt> and <tt class=
"function">std::for_each()</tt> instead of manually iterating
through loops.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e224" id="d0e224"></a>Student Code
Critique 36 Entries</h2>
</div>
<p>Here is a C program generating a couple of prime numbers as part
of an exercise on encoding/decoding with public and private keys.
There are two bugs with the program: it produces the same output
each time it is run with one compiler (<span class=
"application">msvc</span>) and it loops forever with another
(<span class="application">gcc</span>). Please critique the code to
help the student resolve both these problems with the algorithm.
Additionally suggest any improvements to the coding style and point
out any other issues with the algorithms used. You can also broaden
the critique to include a C++ solution if this may assist the
student with their original task.</p>
<pre class="programlisting">
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
int main()
{
//need to generate number, then find out
//whether it is a prime, twice. Then need to
//generate e and see if it is a factor of n.
  int i1, rem1, i2, rem2, i3, rem3, rem4;
  int p, q;
  int n, phi, e;
  //These are the two prime numbers output
  int m, d;
  i1 = 0;
  i2 = 0;
  i3 = 0;
  while(i1!=1)
  {
    p = 100 + 99*rand()/((double)RAND_MAX+1);
    //p is random number between 100 and 200.
    i1=p-1;
    rem1 = p%i1;
    //find out whether the number is prime
    while(rem1!=0)
    {
      i1--;
      rem1 = p%i1;
    }
  }
  while(i2!=1)
  {
    q = 100 + 99*rand()/((double)RAND_MAX+1);
    i2=q-1;
    rem2 = q%i2;
    while(rem2!=0)
    {
      i2--;
      rem2 = q%i2;
    }
  }
  n = p*q;
  phi = (p-1)*(q-1);
  // phi is the number of primes less than n!
  // e picked such that gcd(e, phi) = 1
  while(i3!=1)
  {
    e = phi*rand()/((double)RAND_MAX+1);
    //e is a random number between 0 and phi. 
    i3=e;
    rem3 = phi%i3;
    rem4 = 1;

    //this loop finds the highest value of i3 
    //which divides both numbers. It needs to
    // be 1, so they are relatively prime
    while(rem4!=0 )
    { 
      i3--;
      rem3 = phi%i3;
      
      if(rem3==0) 
        rem4 = e%i3;
    }
  }

  //the loop will find the value of m such 
  //that e*d mod phi = 1.
  m = 0;        
  while((e*d)%phi !=1)
  {
    d = (m*phi+1)/e;
    m++;
  };
  printf( &quot;(m,d) is (%i,%i)\n&quot;, m,d );
  return 0; 
}
</pre>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e237" id="d0e237"></a>From Seyed H.
Haeri (Hossein) <tt class="email">&lt;<a href=
"mailto:shhaeri@math.sharif.edu">shhaeri@math.sharif.edu</a>&gt;</tt></h3>
</div>
<p>When I start scanning this code, the first thing which meets my
eyes is its appropriate commenting. So, one positive point for the
student!</p>
<p>Next, I see the variable declarations at the beginning of the
main() function. As mentioned in the problem specification, this is
a C code, so the student has probably not had any other option. In
case he/she is about to move to C++, which the spec lets us to
assume so, the programmer should postpone them as late as
possible.</p>
<p>Another point which arises whilst moving from C to C++, is that
because there is no more any need to use <tt class=
"function">printf()</tt>, and we use <tt class="varname">cout</tt>
in return, it suffices to replace the top <tt class=
"literal">#include</tt>s with a mere <tt class="literal">#include
&lt;iostream&gt;</tt>. (Note that I've dismissed the tailing
<tt class="literal">.h</tt> on purpose.)</p>
<p>A negative point which I give to the student is because of the
lack of good interaction with the user (of the programmer); the
program outputs &quot;<span class="quote">(m,d) is &hellip;</span>&quot;
without saying what <tt class="literal">m</tt> and <tt class=
"literal">d</tt> are. The program is supposed to say that
beforehand. More explanation of what the program is about to do is
not that bad. Furthermore, <span class=
"emphasis"><em>(m,d)</em></span> is not a good representation of
the purpose. If we suppose that somebody may some day use this
program, that's mathematicians. Mathematicians are very likely to
misunderstand (,) with GCD - as is usual in Number Theory.</p>
<p>I'm not sure whether this is an assignment in which the student
is supposed to implement the algorithm for producing two random
prime numbers. Assuming it is not, it suffices to use the Boost
stuff to do that within much less lines. I don't know for when this
code is, but if it is for recent years, the program is, therefore,
overkill.</p>
<p>But what has caused the bug reported by the student? Well, a
known problem: Random Number Generation. It turns out that built-in
random generators such as <tt class="function">rand()</tt>,
although really produce random numbers, don't produce what we
human-beings may consider so. What has caused the students report
is also the same. He/she has encountered the problem because
<tt class="function">rand()</tt> produces the same number in
consecutive invocations. That is, <tt class="function">rand()</tt>
of MSVC is always producing the same prime number, and that of GCC
is always producing the same non-prime number. Therefore, MSVC
always outputs the same number, and GCC goes in an infinite loop.
(Note that this is a consequence of program's logic.)</p>
<p>Another important point about this code is the lack of good
modularisation in it; none of those loops should be more than a
function call. Assuming that the programmer knows how to implement
each of below functions, (and he/she adds appropriates comments as
well) he/she should have written something like this:</p>
<pre class="programlisting">
#include &lt;iostream&gt;
&hellip;
int main()
{
  int p = gimmmeARandom(100, 200);
  int q = gimmmeARandom(100, 200);
  int n = p * q;
  int phi = (p - 1) * (q - 1);
  int e;
  do
  {
    e = gimmmeARandom();
  }
  while(gcd(e, phi) != 1);
  int m = 0, d = 0;
  while((e * d) % phi != 1)
  {
    d = (m * phi + 1) / e;
    ++m;
  }
  cout &lt;&lt; &quot;Two random prime numbers
     : &quot; &lt;&lt; m &lt;&lt; d;
  return 0;
}
</pre>
<p>Finally here are a few minor points:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>The programmer has always used the postfix ++ operator, whilst
in all of them, the prefix version does the job. So, he/she should
replace all of them.</p>
</li>
<li>
<p>The variable <tt class="varname">d</tt> is not initialised. I
don't know about C, but in C++, local variables are not
automatically initialised. Therefore, the student should manually
initialise that.</p>
</li>
</ol>
</div>
<p>I suggest the student should read about the Boost Random Number
Generation library.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e308" id="d0e308"></a>From Ken Duffill
<tt class="email">&lt;<a href=
"mailto:ken@kendee.co.uk">ken@kendee.co.uk</a>&gt;</tt></h3>
</div>
<p>On first look this code is quite busy and difficult to follow.
There is a bunch of declarations, all with pretty meaningless and
very similar names. Then there are four loops, obviously this is
where the work goes on, but neither the original description, nor
the comments, nor the code itself make clear what is meant to be
going on.</p>
<p>This kind of code is very common with beginners, and even
experienced developers like myself (I have been programming for
nearly thirty years) may produce code like this during some quick
prototyping excercise. The next phase of development, though, MUST
be to get the code into some sort of order.</p>
<p>I will go through the process step by step so as to try to show
what changes I would like to make and why I think they are
important. Once we have done this it is possible that at least some
of the bugs will have become obvious and been resolved along the
way and the code is definitely going to be more easy to understand
so if there is a fundamental flaw in the design we should be able
to pick it up.</p>
<p>Right from the start we see that the code is one long function
called <tt class="function">main</tt>. Even though it isn't that
long; one long function always leads to trouble. It is difficult to
tell where the variables have any meaning. They are all declared in
the same place and then some are used and forgotten, others are
used to carry data from one part of the program to another, but it
is not easy to tell which is which.</p>
<p>So, the first step is to put each of the four loops into
separate functions, and only leave those variables declared in the
<tt class="function">main</tt> function that are needed to carry
data between the functions.</p>
<p>Looking at the first loop, it seems that the variable <tt class=
"varname">p</tt> is the only data that is needed once the loop has
finished. So we create a function (we will call it <tt class=
"function">function1</tt> for now, once we understand its real
purpose later on we will think of a more descriptive name) that
returns an <tt class="type">int</tt>. We cut the loop from
<tt class="function">main</tt> and paste it into the new function,
replacing the original with the assignment <tt class="literal">p =
function1();</tt></p>
<p>Now we remove <tt class="varname">i1</tt> and <tt class=
"varname">rem1</tt> from the declarations in <tt class=
"function">main</tt> and put them into <tt class=
"function">function1</tt> and add a new int <tt class=
"varname">p</tt> declaration in <tt class=
"function">function1</tt>, and return <tt class="varname">p</tt>
when the function is complete.</p>
<p>Compile the code using both compilers, to make sure we haven't
broken anything.</p>
<p>Oops! The compilers baulk at a statement in <tt class=
"function">main</tt> <tt class="literal">i1 = 0;</tt> So we cut
that from <tt class="function">main</tt> and paste it into
<tt class="function">function1</tt>. At this point one of my
personal preferences takes over for a moment and instead of having
the first few lines of <tt class="function">function1</tt> look
like this:</p>
<pre class="programlisting">
int i1, rem1;
int p;
i1 = 0;
</pre>
<p>I change it to:</p>
<pre class="programlisting">
int i1 = 0;
int rem1;
int p;
</pre>
<p>That is to say one declaration per line and if a variable needs
initialising, do it in the declaration so you don't forget.</p>
<p>Compile the code using both compilers, to make sure we haven't
broken anything. That's better!</p>
<p>Run both versions, to make sure we haven't changed its
behaviour.</p>
<p>Next we do the same for the second loop, calling this <tt class=
"function">function2</tt>.</p>
<p>Now we can see that functions one and two are only different in
that one has variables <tt class="varname">i1</tt>, <tt class=
"varname">rem1</tt> and <tt class="varname">p</tt> and the other
variables <tt class="varname">i2</tt>, <tt class=
"varname">rem2</tt> and <tt class="varname">q</tt>. If we change
<tt class="varname">i1</tt> and <tt class="varname">i2</tt> to
<tt class="varname">i</tt> and <tt class="varname">rem1</tt> and
<tt class="varname">rem2</tt> to <tt class="varname">rem</tt> in
both functions and then change <tt class="varname">q</tt> to
<tt class="varname">p</tt> in the second function, the functions
become identical, so <tt class="function">function2</tt> can be
deleted and main can make two calls to <tt class=
"function">function1</tt>, one assigning the result to <tt class=
"varname">p</tt>, the other assigning the result to <tt class=
"varname">q</tt>, instead.</p>
<p>In examining the next loop in <tt class="function">main</tt> to
decide how to turn it into a function we notice that the two lines
of code before the loop include one that assigns <tt class=
"varname">n</tt> to <tt class="literal">p*q</tt>, and yet
<tt class="varname">n</tt> is never used in the code. It is only
referred to in the comment that states, &quot;<span class="quote">phi is
the number of primes less than n!</span>&quot;</p>
<p>Just to make sure that we haven't missed something we delete
this line, and the declaration of <tt class="varname">n</tt> and
recompile. Sure enough, the compiler is happy. Now, if we have any
&quot;domain knowledge&quot; we will understand that the value <tt class=
"literal">n = p * q</tt> is a very important part of the
cryptographic algorithm so we may choose to leave it in for future
use. If we do then we should place a comment to this affect. If we
decide to remove it we need to change the comment to &quot;<span class=
"quote">phi is the number of primes less than n!. Where n was the
product of p and q.</span>&quot;</p>
<p>We now create a function (<tt class="function">function3</tt>,
for now) that takes an integer parameter and returns an integer
result. Cut the loop from <tt class="function">main</tt> and paste
it into the new function, replace the cut code in <tt class=
"function">main</tt> with a call to <tt class=
"function">function3</tt> passing in <tt class="varname">phi</tt>
as calculated just before the loop, move all appropriate
declarations from <tt class="function">main</tt> to <tt class=
"function">function3</tt>. Exactly as we did for <tt class=
"function">function1</tt> and <tt class="function">function2</tt>.
We then compile and run to show we haven't broken anything.</p>
<p>Ooops, exactly as happened for <tt class=
"function">function1</tt> and <tt class="function">function2</tt>
the compiler baulks because there is an assignment of <tt class=
"literal">i3 = 0</tt> left in <tt class="function">main</tt>. Move
this assignment into <tt class="function">function3</tt>, taking
this opportunity to implement my preferences (one declaration per
line and if a variable needs initialising, do it in the declaration
so you don't forget).</p>
<p>Compile again, and run.</p>
<p>Don't despair that we haven't fixed the bug yet, <span class=
"application">gcc</span> still produces an exe that loops forever,
and <span class="application">MSVC</span> still gives us the same
answer every time we run it. That's OK we haven't tried to fix
anything yet we are just getting the code 'in shape'.</p>
<p>We repeat the refactoring process one last time, for the last
loop. This time <tt class="function">function4</tt> needs to accept
two integer parameters (<i class="parameter"><tt>e</tt></i> and
<i class="parameter"><tt>phi</tt></i>) and doesn't return anything,
it just prints the results. Note the initialisation of <tt class=
"literal">m = 0</tt> just before the loop, let's do that in the
declaration of <tt class="varname">m</tt> in the function this
time.</p>
<p>Now <tt class="function">function4</tt> is very small, and we
notice straight away that there is an unnecessary semicolon at the
end of this <tt class="literal">while</tt> loop, which we remove.
Isn't it much easier to see these things in small functions? Also
we see that there is an <tt class="literal">int d</tt> that is
declared and then used, in the conditional of the <tt class=
"literal">while</tt> loop, before it has been initialised. Could
this be our bug?</p>
<p>So following my preferences we want to initialise <tt class=
"varname">d</tt> in its declaration, but what do we initialise it
to? Well in the body of the loop <tt class="varname">d</tt> is set
to <tt class="literal">(m*phi+1)/e</tt> so lets use that. We know
that <tt class="varname">m</tt> is initialised to zero so the
initialisation of <tt class="varname">d</tt> becomes <tt class=
"literal">1/e</tt> because <tt class="literal">m*phi</tt> is zero.
Now we see that in the loop, once <tt class="varname">d</tt> has
been set <tt class="varname">m</tt> is incremented so maybe,
because we have set <tt class="varname">d</tt> in the
initialisation, <tt class="varname">m</tt> should be initialised to
1 rather than zero. Essentially we have done the first pass through
the loop in our initialisation, at the same time as making sure we
don't get any surprises if the runtime environment doesn't
initialize in the way we expected.</p>
<p>Was this our bug? Well, no. Having compiled and run the code we
get the same behaviour as before. But the code quality has
improved.</p>
<p>Now our <tt class="function">main</tt> is looking much smaller
and more understandable. If we adopt my preferences for
declarations we have a <tt class="function">main</tt> that looks
like this:</p>
<pre class="programlisting">
int p = function1();
int q = function1();
int phi = (p-1) * (q-1);
int e = function3(phi);
function4(e, phi);
</pre>
<p>with a few blank lines and comments in between these lines of
code.</p>
<p>Now we can see that if the variable and function names made some
more sense we might actually be able to see what is going on!</p>
<p>Just by looking at the comments we can get some better variable
names in <tt class="function">main</tt>.</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p><tt class="varname">p</tt> and <tt class="varname">q</tt> are
random numbers that are prime.</p>
</li>
<li>
<p><tt class="varname">phi</tt> is the number of primes less than
<tt class="literal">n!</tt> Where <tt class="varname">n</tt> was
the product of <tt class="varname">p</tt> and <tt class=
"varname">q</tt>. I could change this name to <tt class=
"varname">xPrimes</tt> or something similar, but it may well be
that the algorithm being implemented is well known and EVERYBODY
who knows it expects to use <tt class="varname">phi</tt>, in which
case it would be better to leave it.</p>
</li>
<li>
<p>I am still not sure what <tt class="varname">e</tt> is so we
will leave that.</p>
</li>
<li>
<p><tt class="function">function1</tt> clearly calculates a random
number that is prime, cos that is what <tt class="varname">p</tt>
and <tt class="varname">q</tt> are.</p>
</li>
</ul>
</div>
<p>Also, it seems as if e and <tt class="varname">phi</tt> are only
used as transports between <tt class="function">function3</tt> and
<tt class="function">function4</tt>, so by moving the call of
<tt class="function">function3</tt> into the beginning of
<tt class="function">function4</tt> we can get main to look like
this:</p>
<pre class="programlisting">
int p = FindRandomPrime();
int q = FindRandomPrime();
function4(( p - 1 ) * ( q - 1 ));
</pre>
<p>We could go one stage further now and lose <tt class=
"varname">p</tt> and <tt class="varname">q</tt> altogether the
whole of <tt class="function">main</tt> just becomes:</p>
<pre class="programlisting">
function4(( FindRandomPrime() - 1 ) *
 ( FindRandomPrime() - 1 ));
</pre>
<p>But that may be a step too far. Again, with some &quot;domain
knowledge&quot; we will find that <tt class="varname">p</tt> and q are
important numbers in the whole cryptographic process, so we will
leave them be.</p>
<p>Our bug is still present though, and we need to look at the
functions more closely.</p>
<p>The <tt class="function">FindRandomPrime</tt> function and
<tt class="function">function3</tt> are very similar. Inside a loop
we get a random number in a given range. Let's see if we can factor
this bit of code out.</p>
<p>We create a function that returns an <tt class="type">int</tt>
and takes two <tt class="type">int</tt> parameters. It is quite
obvious now what this function is going to do, so we will give it a
useful name straight away. The prototype will be something like
this:</p>
<pre class="programlisting">
int GetRandomInRange( int from, int to );
</pre>
<p>We cut the line from <tt class="function">FindRandomPrime</tt>
and paste it into <tt class="function">GetRandomInRange</tt>, and
refactor it to account for the parameterisation so it becomes:</p>
<pre class="programlisting">
return from + (to-from-1)
*rand()/((double)RAND_MAX+1);
</pre>
<p>replacing that line in <tt class="function">FindRandomPrime</tt>
with a call to <tt class="function">GetRandomInRange</tt> thus:</p>
<pre class="programlisting">
p = GetRandomInRange(100, 200);
</pre>
<p>We almost don't need the comment anymore. We replace the line in
<tt class="function">function3</tt> similarly:</p>
<pre class="programlisting">
e = GetRandomInRange(0, phi);
</pre>
<p>Comple and run... The bug is still there.</p>
<p>Now we ask ourselves does <tt class=
"function">GetRandomInRange</tt> work? It is a one line function,
so we should be able to understand it now. <tt class=
"function">rand()</tt> returns a random integer between 0 and
<tt class="constant">RAND_MAX</tt>, and this function appears to
try to convert this to a random integer between from and to.</p>
<p>With this implementation there is a mixture of implicit and
explicit casting in this expression. Now, as I have said earlier, I
have been developing software for nearly thirty years, more than
fifteen of which have been in C. I have the C standard on my desk
and I still can't tell for sure how this expression will evaluate.
Some integers get cast to doubles, and then the doubles get cast
back to integers, but which, where and why? I dunno.</p>
<p>It is my belief that, even if I knew that this expression could
be relied upon in all implementations to give me the correct
answer, I cannot rely on everybody who may need to look at this
code in the future to be able to, and if they can't they are going
to break it someday (if it isn't already broken, of course). So as
a matter of principal, I don't like expressions that mix explicit
and implicit casts.</p>
<p>If we rewrite this expression as two expressions so as to avoid
implicit casts altogether we get:</p>
<pre class="programlisting">
double range = ( double )rand()
  /((double)RAND_MAX + (double)1.0 );
return from + (int)(
  (double)(to-from-1)*range);
</pre>
<p>When we compile and run we now do not loop indefinitely in the
<span class="application">gcc</span> version. Hey, success! Of
course, it gives us the same answer every time we run the
application, but at least our two compilers now produce an
application with the same behaviour.</p>
<p>I am still not entirely sure that this function actually
delivers what it seems to want to in extreme cases, and there may
be a hidden bug in the application as a result.</p>
<p>I would prefer to see:</p>
<pre class="programlisting">
double range = (double)rand() /
  (double)RAND_MAX;
return from + (int)((double)( to - from )
  * range );
</pre>
<p>Now that this code is contained in one simple function, some
unit tests should be written to prove the answer is correct in all
cases (or fix the code until it does). I will leave that as an
exercise for the reader.</p>
<p>So, why are our random numbers not random. A quick look at the
standard (or any C standard library reference) tells us the answer
lies with <tt class="function">rand</tt>. <tt class=
"function">rand</tt> doesn't actually produce random numbers;
rather, it selects numbers from a sequence of pseudo-random
numbers. The starting point of this sequence will always be the
same unless the <tt class="function">rand</tt> library is properly
initialised. <tt class="function">srand</tt> should be called
somewhere in the application before the first call to <tt class=
"function">rand</tt>. The parameter to <tt class=
"function">srand</tt> should be some value that is different every
time the application runs. It is quite usual to use a value from
the system timer for this purpose. So I will use a call to
<tt class="function">time</tt> thus:</p>
<pre class="programlisting">
srand((int)time(NULL));
</pre>
<p>This, of course will only work so long as the application is not
run more frequently than once per second. Whilst <tt class=
"function">srand</tt> is called at the application level this is
reasonable, because we humans are slow beasts, but care must be
taken if this code is refactored into libraries that may be called
rapidly from other code.</p>
<p>We now have tidied our code up considerably, found the two
causes of the problems observed and two other problems that had not
yet produced observable errors. But there is still more that can be
done. With a little more &quot;domain knowledge&quot; we can find better
names for <tt class="function">function3</tt> and <tt class=
"function">function4</tt>.</p>
<p>I did some internet searches based upon the comments like
&quot;<span class="quote">e picked such that gcd(e, phi) = 1</span>&quot; and
&quot;<span class="quote">this loop finds the highest value of i3 which
divides both numbers. It needs to be 1, so they are relatively
prime</span>&quot; and &quot;<span class="quote">the loop will find the value
of m such that e*d mod phi = 1</span>&quot;. From information gleaned, I
refactored <tt class="function">function3</tt> into <tt class=
"function">FindRandomRelativePrime</tt> that calls functions called
<tt class="function">Euclid</tt> and <tt class=
"function">FindLargestFactor</tt>. I am not a Crypto expert and I
still can't find a better name for <tt class=
"function">function4</tt>. I have attached the code so maybe
someone else can finish the job.</p>
<p>Now that all the functionality is refactored into small, well
named, functions whose purpose is clear and unambiguous then each
function can be tested with unit tests, preferably automatically
built and run as part of the development environment every time the
code is changed.</p>
<p>Who knows what other problems will have been identified and
fixed once this process is complete.</p>
<p>You will notice if you look at my code that I have changed the
post-increments and post-decrements to pre-increments and
pre-decrements. There are cases when the code produced can be more
efficient if pre- rather than post- operations are used. It is true
that in C these cases are rather rare and insignificant, but in C++
when the objects being pre- or post- operated are not native types
but large objects then the saving of the creation of one temporary
can be significant, and it is therefore a good habit to get into.
One final comment. The original problem stated that the application
behaved differently when built with <span class=
"application">MSVC</span> than with <span class=
"application">gcc</span>. It is good practice to build your
applications with more than one compiler whenever you can. Some
interesting differences can come up.</p>
<p>When I was refactoring the code for <tt class=
"function">FindLargestFactor</tt>. I decided that the original
algorithm was marginally wasteful as the largest factor must be
less than or equal to half the candidate. Because of this the
candidate must be greater than 2 in order to have any factors. I
put a conditional in right at the beginning of the function, but
being lazy I only compiled with the <span class=
"application">gcc</span> compiler. There was no problem. It was
some time later when I compiled with MSVC again and the compiler
complained because it didn't like declarations that follow code.
Although the C99 standard allows this, for maximum portability it
is not good practice. If you really MUST have new declarations
after code then they should be put in a new block by the
appropriate use of curly braces. In this case there is very little
overhead to just moving the conditional to just after the
declarations, but that isn't always the case.</p>
<p>Another incident was when I began using time to seed the random
sequence. Again <span class="application">gcc</span> was happy, but
<span class="application">MSVC</span> complained that I hadn't
<tt class="literal">#include</tt>d <tt class=
"filename">time.h</tt></p>
<p>One final final comment. It is also a good idea to pass your
code through a static code checker such as <span class=
"application">Lint</span> (or <span class=
"application">PcLint</span>), this can also pick up some problems.
Probably the problem with implicit casting that was the first bug
we cured in this project would have been caught by a static
checker.</p>
<pre class="programlisting">
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;time.h&gt;
int GetRandomInRange(int from, int to)
{
  double range = (double)rand() /
    (double)RAND_MAX;
  return from + (int)((double)( to - from )
    * range );
}
int FindLargestFactor(int candidate)
{
  // The largest factor of a candidate cannot
  // be bigger than half of the candidate,
  // so lets start there.
  int potentialFactor = (candidate &gt;&gt; 1);
  // The concept of factors only applies to
  // numbers greater than or equal to 2.
  if (candidate &lt; 2)
  {
    return candidate;
  }
  // If there is no remainder when candidate
  // is divided by potentialFactor
  // then potentialFactor is a real factor.
  while(candidate % potentialFactor != 0)
  {
     // There was a remainder so lets try
     // another potentialFactor.
     --potentialFactor;
  }
  // We will always end up with a factor even
  // if it is one.
  return potentialFactor;
}
int FindRandomPrime()
{
  int p;
  do
  {
     p = GetRandomInRange( 100, 200 );
  // Find out whether p is prime.
  // If the largest factor of p is one then p
  // is prime, so we have finished.
  } while ( FindLargestFactor( p ) != 1 );
  return p;
}
int Euclid( int larger, int smaller )
{
    // Find whether the greatest common
    // denominator (gcd) of the two
    // parameters is one or not.
    int divisor = smaller;
    int dividend = larger;
    int remainder;
    while (( remainder = dividend % divisor )
           &gt; 1 )
    {
        dividend = divisor;
        divisor = remainder;
    }
    //If the result is one then it is the gcd,
    //otherwise the result is zero. We are not
    //interested what it is, but if we were it
    //would be dividend at this point.
    return remainder;
}
int FindRandomRelativePrime( int phi )
{
  int e;
  do
  {
    e = GetRandomInRange( 1, phi );
  } while ( Euclid( phi, e ) != 1 );
  return e;
}
void function4( int phi )
{
  // phi is the number of primes less than
  // n!.  Where n was the product of p and q.
  int e = FindRandomRelativePrime( phi );
  int m = 1;
  int d = 1 / e;
  //the loop will find the value of m such
  //that e*d mod phi = 1
  while(( e * d ) % phi != 1 )
  {
     d = ( m * phi + 1 ) / e;
     ++m;
  }
  printf(&quot;(m,d) is (%i,%i)\n&quot;, m,d );
}
int main()
{
  srand((int)time(NULL));
  {
    int p = FindRandomPrime();
    int q = FindRandomPrime();
    function4(( p - 1 ) * ( q - 1 ));
  }
  return 0;
}
</pre></div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e903" id="d0e903"></a>From Jim Hyslop
<tt class="email">&lt;<a href=
"mailto:jhyslop@dreampossible.ca">jhyslop@dreampossible.ca</a>&gt;</tt></h3>
</div>
<p>Let's deal with gcc's behaviour first. In debugging, <tt class=
"function">printf</tt> can be your best friend, especially if you
don't have an IDE to step through the code. So, after putting in
various <tt class="function">printf</tt>s to find the checkpoints
(i.e. &quot;About to find p&quot;, &quot;About to find q&quot;, and so on), we find
that it's never exiting the first loop:</p>
<pre class="programlisting">
while(i1!=1)
{
  p = 100 + 99*rand()/((double)RAND_MAX+1);
  //p is random number between 100 and 200.
  i1=p-1;
  rem1 = p%i1;
  //find out whether the number is prime
  while(rem1!=0)
  {
    i1--;
    rem1 = p%i1;
  }
}
</pre>
<p>Oops - what's this: <tt class="literal">99 * rand()</tt>. If
<tt class="function">rand()</tt> is greater than approx. 21 million
(which it will be about 1 time out of 100) then it will overflow.
We don't want that, since overflows are undefined behaviour (which,
by the way, is probably why <span class="application">MSVC</span>
&quot;works&quot;). So, to fix it, we can just force the numerator to a
<tt class="type">double</tt> by using the constant 99.0 instead of
99:</p>
<pre class="programlisting">
p = 100 + 99.0*rand() / ((double)RAND_MAX+1);
</pre>
<p>We don't need to worry about the denominator overflowing, since
<tt class="constant">RAND_MAX</tt> is first cast to a <tt class=
"varname">double</tt>, and then incremented.</p>
<p>Looking further down the code, we see that the random number
generation is repeated. This needs to be refactored into a function
which generates a random number in a given range:</p>
<pre class="programlisting">
int GenRand( int min, int max )
{
  return min + (double)max *
    rand()/((double)RAND_MAX+1);
}
</pre>
<p>Doing this by the way, also eliminates the need for the comments
<tt class="literal">//[varname] is a random value between x and
y.</tt></p>
<p>Now, when you run it, <span class="application">gcc</span> and
<span class="application">MSVC</span> behave the same: you always
get the same results. That's because <tt class=
"function">rand()</tt> is not a true random number generator: it
generates pseudo-random numbers in a sequence determined by the
algorithm used. Unless you provide it with a unique seed each time
you start the program, it will generate the same sequence of
numbers. So the first thing to do is seed the random number
generator. For purposes of this exercise, <tt class=
"literal">srand( time(NULL) );</tt> before the first call to
<tt class="function">rand()</tt> will suffice. Now, the program
will generate different numbers each time you run it (unless you
run it twice within one clock second).</p>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e967" id="d0e967"></a>Style
Comments</h4>
</div>
<p>Even C has the concept of local block variables. Take advantage
of them, and declare variables in the most restrictive scope
possible. The variables <tt class="varname">i</tt>? (e.g.
<tt class="varname">i1</tt>, <tt class="varname">i2</tt>, etc.) and
<tt class="varname">rem</tt>? are each only used within one
<tt class="literal">while</tt> loop, so they can be declared
there.</p>
<p>None of the variables are initialized. Uninitialized variables
account for a large number of programming errors, so variables
should be initialized whenever possible. It also makes the meat of
your functions more compact, since you don't occupy space within
the logic simply initializing variables.</p>
<p>The variable names are not very useful. <tt class=
"varname">i1</tt>, <tt class="varname">i2</tt>, <tt class=
"varname">i3</tt> are meaningless. I seem to recall that <tt class=
"varname">p</tt>, <tt class="varname">q</tt>, <tt class=
"varname">m</tt>, <tt class="varname">d</tt>, <tt class=
"varname">n</tt>, <tt class="varname">e</tt>, and <tt class=
"varname">phi</tt> are specific terms used in the cryptographic
equations so that is reasonable - but there should be a comment to
that effect.</p>
<p>Comment placement is inconsistent. Some comments are placed
before the line in question, some after, e.g.:</p>
<pre class="programlisting">
p = 100 + 99*rand()/((double)RAND_MAX+1);
//p is random number between 100 and 200.
[...]
//find out whether the number is prime
while(rem1!=0)
</pre>
<p>Consistency is important in allowing other programmers to easily
read and understand your program. The comment for <tt class=
"varname">p</tt> should be placed before the statement.</p>
<p>Avoid repetition. There are two loops that perform identical
work - they should be refactored (yes, you can refactor even in C)
into a function. Applying meaningful names for <tt class=
"varname">i1</tt> and <tt class="varname">rem1</tt>, we get:</p>
<pre class="programlisting">
int FindRandomPrime( void )
{
  int primeCandidate, factor=0;
  while(factor!=1)
  {
    int remainder;
    primeCandidate = GenRand( 100, 200 );
    factor=primeCandidate-1;
    remainder = primeCandidate%factor;
    //find out whether the number is prime
    while(remainder!=0)
    {
      factor--;
      remainder = primeCandidate%factor;
    }
  }
  return primeCandidate;
}
</pre>
<p>This is a horribly inefficient way of finding primes. It tries
all numbers from 1..primeCandidate. You don't need to try any even
numbers except 2, and you don't need to try any numbers below
<tt class="literal">sqrt( primeCandidate )</tt>. A much more
efficient function might be:</p>
<pre class="programlisting">
int IsPrime( int value )
{
  int factor1 = 3,
      factor2 = sqrt( value ) + 1,
      divisorFound= value%2 == 0;
  while ( !divisorFound &amp;&amp; factor1&lt;factor2 )
  {
    divisorFound = value % factor1 == 0;
    factor1 += 2;
  }
  return divisorFound;
}
int FindRandomPrime()
{
  int primeNumber;
  do {
    primeNumber = GenRand( 100, 200 );
  } while ( !IsPrime( primeNumber ) );
  return primeNumber;
}
</pre>
<p>Note, by the way, that I've split <tt class=
"function">IsPrime</tt> into a separate function: this allows you
to hook in unit tests to validate that your <tt class=
"function">IsPrime</tt> function works correctly.</p>
<p>I don't have my copy of C99 handy, but I'd double check to see
if // comments are allowed in C99. I know they are not allowed in
C90.</p>
<p>A little more liberal use of whitespace would help readability
(although that could be a restriction imposed by the printing
requirements in the magazine).</p>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e1059" id=
"d0e1059"></a>Commentary</h3>
</div>
<p>With three such varied entrants I don't think much additional
commentary is needed - the main items I wanted addressing are all
covered: the amount of code duplication, the oddly predictable
behaviour of <tt class="function">rand()</tt>, and the problems
caused by the mix of <tt class="type">int</tt> and <tt class=
"type">double</tt> variables.</p>
<p>However no one correctly identified the reason for the
difference in runtime behaviour between <span class=
"application">MSVC</span> and <span class="application">gcc</span>.
It is in fact caused by the different values for the
(implementation defined) value <tt class="constant">RAND_MAX</tt>.
This is defined as <tt class="literal">0x7fff</tt> with
<span class="application">msvc</span> and <tt class=
"literal">0x7fffffff</tt> with <span class=
"application">gcc</span>. With <span class=
"application">msvc</span> the expression <tt class=
"literal">99*rand()/((double)RAND_MAX+1)</tt> will evaluate to a
number from 0 to 98 but with <span class="application">gcc</span>
on the platform being used it can only be 0 or -1 (caused by signed
integer overflow).</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e1105" id="d0e1105"></a>The Winner
of SCC 36</h2>
</div>
<p>The editor's choice is:</p>
<p>Jim Hyslop with a strong commendation to Ken Duffill</p>
<p>Please email <tt class="email">&lt;<a href=
"mailto:francis@robinton.demon.co.uk">francis@robinton.demon.co.uk</a>&gt;</tt>
to arrange for your prize.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e1117" id="d0e1117"></a>Student
Code Critique 37</h2>
</div>
<p>(Submissions to <tt class="email">&lt;<a href=
"mailto:scc@accu.org">scc@accu.org</a>&gt;</tt> by Jan 3rd
2006)</p>
<p>For a change I thought I'd set a Java problem. Here is a
student's attempt at a simple class to provide access to a single
database table, ADDRESS. Please critique the code and suggest what
problems there may be with this class when using it in a larger
application, and any issues with the simple test harness.</p>
<pre class="programlisting">
import java.sql.*;
class Scc37 {
  String[] drivers = {
    &quot;sun.jdbc.odbc.JdbcOdbcDriver&quot; };
  String database = &quot;jdbc:odbc:ADDRESS&quot;;
  void setDrivers( String[] drivers ) {
    drivers = drivers;
  }
  void setDatabase( String database ) {
    database = database;
  }
  String selectAddress( String query ) {
    try {
      for ( int idx = 0;
            idx != drivers.length; ++idx )
        Class.forName( drivers[ idx ] );
    } catch (Exception e) {
      System.out.println(e);
    }
    String userName=&quot;&quot;;
    String password=&quot;&quot;;
    // Get connection
    Connection con = null;
    try {
      con = DriverManager.getConnection(
        database,userName,password);
    } catch(Exception e) {
      System.out.println(e);
    }

    // Execute query
    ResultSet results = null;
    try {
      results =  
        con.createStatement().executeQuery(
        &quot;SELECT * FROM ADDRESS WHERE &quot;
        + query );
    } catch (Exception e) {
      System.out.println(e);
    }
    // Get all results
    String retVal = &quot;&quot;;
    try {
      ResultSetMetaData rsmd = 
        results.getMetaData();
      int numCols = rsmd.getColumnCount();
      int i, rowcount = 0;
      // break it off at 50 rows max
      while (results.next() &amp;&amp; rowcount&lt;40) {
        // Loop through each column, getting
        // the data and displaying
        for (i=1; i &lt;= numCols; i++) {
          if (i &gt; 1) retVal = retVal + &quot;,&quot;;
          retVal = retVal +
            results.getString(i);
        }
        retVal = retVal + &quot;\n&quot;;
        rowcount++;
      }
    } catch (Exception e) {
      System.out.println(e);
    }
    return retVal;
  }

  /** Test harness - 'args' list drivers */
  public static void main( String[] args ) {
    Scc37 scc37 = new Scc37();
    scc37.setDrivers( args );
    System.out.println( &quot;Found:&quot; +
      scc37.selectAddress(
        &quot;name LIKE '%Hardy'&quot; ) );
  }
}
</pre></div>
<div class="footnotes"><br>
<hr class="c3" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e134" href="#d0e134" id=
"ftn.d0e134">1</a>]</sup> Charles Simonyi, 'Hungarian Notation',
<a href=
"http://msdn.microsoft.com/library/en-us/dnvsgen/html/hunganotat.asp"
target=
"_top">http://msdn.microsoft.com/library/en-us/dnvsgen/html/hunganotat.asp</a></p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
