    <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 31</title>
        <link>http://accu.org/index.php/journals/712</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>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">CVu Journal Vol 16, #6 - Dec 2004 + Student Code Critiques from CVu journal.</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="http://accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="http://accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="http://accu.org/index.php/journals/c77/">CVu</a>

                     &gt;                         <a href="http://accu.org/index.php/journals/c99/">166</a>
                    (12)
<br />

                                            <a href="http://accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="http://accu.org/index.php/journals/c184/">Journal Columns</a>

                     &gt;                         <a href="http://accu.org/index.php/journals/c183/">Code Critique</a>
                    (39)
<br />

                                            <a href="http://accu.org/index.php/journals/c99-183/">Any of these categories</a>

                    -                        <a href="http://accu.org/index.php/journals/c99+183/">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 31</h1>
<p><strong>Author:</strong>&nbsp;Administrator</p>
<p>
<strong>Date:</strong> 06 December 2004 13:16:09 +00:00 or Mon, 06 December 2004 13:16:09 +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="d0e22" id="d0e22"></a></h2>
</div>
<p><span class="bold"><b>Prizes provided by Blackwells Bookshops
&amp; Addison-Wesley</b></span></p>
<p class="c2"><span class="remark">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.</span></p>
<p class="c2"><span class="remark">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.</span></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e33" id="d0e33"></a>Before We
Start</h2>
</div>
<p>Have you ever come across a tricky bug at work that took you the
whole day to find, or an exercise at school that didn't work the
way you expected to? Those could be good opportunities not only to
share it with other members, but to receive feedback from them.
After all, this belongs to the Dialogue section, so who better than
you to take part?</p>
<p>Remember that you can get the current problem set on 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="d0e43" id="d0e43"></a>Student Code
Critique 30 Entry</h2>
</div>
<p>Here is a program Francis collected which is riddled with poor
design, naming, etc. as well as the actual problem:</p>
<p><span class="emphasis"><em>I'm getting a &quot;parse error before
else&quot; at the line indicated by the arrow</em></span></p>
<pre class="programlisting">
void IS_IT_A_DDR(string&amp; mtgrec,
                 string&amp; temprec,int&amp; ddrrc) {
  string Day2=&quot;SunMonTueWedThuFriSat&quot;;
  string Daytoken=&quot;0123456&quot;;
  int badday=0;
  if (mtgrec.size() &lt; 8) {
    ddrrc=0;
    return;
  }
  for (int i=0; i &lt;= 6; i++) {
    if (mtgrec.substr(0,3)
                == Day2.substr((i+1)*3-3,3)) {
      if ((mtgrec.substr(3,1) == &quot;0&quot;)
             || (mtgrec.substr(3,1) == &quot;1&quot;)) {
        if ((mtgrec.substr(7,1)).
          find_first_of(&quot;BCLMOPSTW*&quot;) != -1) {
      temprec=Daytoken.substr(i,1)
                           + mtgrec.substr(1);
      ddrrc=1;
      return;
      }
        else {
      ddrrc=2;
      return;
      }
    else { &lt;&lt;&lt; compiler diagnostic
      ddrrc=3;
      return;
    }
    }
  }
  else badday++;
  }
  if (badday == 7) {
    ddrrc=4;
    return;
  }
  else ddrrc=5;
  return;
}
</pre>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e53" id="d0e53"></a>From Neil Youngman
<tt class="email">&lt;<a href=
"mailto:ny@youngman.org.uk">ny@youngman.org.uk</a>&gt;</tt></h3>
</div>
<p>This piece of code is a bit of a problem. On a first reading
it's hard to tell what it's trying to achieve. I can see that
there's some sort of date related functionality from the definition
of variable <tt class="varname">Day2</tt>, but it really doesn't
make it obvious what it's doing with those dates.</p>
<p>First things first, I guess, start with the compiler error and
then try to deal with the other issues. This is at some level
obvious, i.e. the <tt class="literal">else</tt> is mismatched, but
which <tt class="literal">if</tt> statement does it go with?</p>
<p>It looks as if it probably matches</p>
<pre class="programlisting">
if((mtgrec.substr(3,1) == &quot;0&quot;)
   || (mtgrec.substr(3,1) == &quot;1&quot;))
</pre>
<p>but with erratic indentation and without any clear idea of
intent, that's not really certain.</p>
<p>To go much deeper I need some idea of meaning. The function is
called <tt class="function">IS_IT_A_DDR</tt>. DDR to me is a type
of memory, which doesn't help. The parameter names don't offer much
of a clue either. The first parameter is called <i class=
"parameter"><tt>mtgrec</tt></i>. I guess that would be 2 components
<i class="parameter"><tt>mtg</tt></i> and <i class=
"parameter"><tt>rec</tt></i>. <i class="parameter"><tt>mtg</tt></i>
could be mortgage or meeting and <i class=
"parameter"><tt>rec</tt></i> could be record. Given the date
related details I would guess that meeting record is the most
likely meaning.</p>
<p>The first <tt class="literal">if</tt> statement inside the
<tt class="literal">for</tt> loop looks for a 3 letter day of the
week at the start, the second for a '0' or a '1' and the 3rd for
any of the characters in &quot;BCLMOPSTW*&quot; anywhere from the 8th
character on. the value of <tt class="varname">ddrrc</tt> will be
set according to which of these it finds. As I seem to be no closer
to deducing the purpose of this code, so I guess I'd better
consider the many stylistic abominations.</p>
<p>First off naming. As pointed out in the question the naming is
poor. I have been unable to determine what the code is intended to
achieve. There should of course be comments to assist with
maintenance, but there aren't. Even with comments, clear use of
names is invaluable in understanding the detail of a program. Here
I have neither.</p>
<p>Looking at the function definition <tt class=
"function">ALL_CAPS</tt> is a common stylistic convention to denote
a macro or a constant. I can't see why it is used here. The choice
of names we have already criticised. The first parameter seems to
be read only and should therefore be const. The last parameter
seems to be a return code, indicating the result of the function.
It seems to me that this should be the function's return value
instead of the function returning void.</p>
<p>Looking at the variables both <tt class="varname">Day2</tt> and
<tt class="varname">Daytoken</tt> should both be constants and some
sort of collection structure, e.g. an array or a vector, seems more
appropriate than a string. This makes clear that they are a group
of separate, if related values, not a single value. The variable
<tt class="varname">badday</tt> seems entirely redundant, as I
can't see that it's value can be anything other than 7 if the loop
runs to completion. Of course that makes the last <tt class=
"literal">else</tt> clause entirely redundant.</p>
<p>Next we come to a size check. This is fairly straightforward,
but involves a magic number &quot;8&quot;. Generally hard coded constants
should be declared somewhere central with a name, both to minimise
the number of places where you they have to be changed, should a
change be needed and to make the code more readable.</p>
<p>That brings us to the values assigned to the variable <tt class=
"varname">ddrrc</tt>. The 8 in the size check could be related to
the code we see, which clearly expects at least 8 characters. The
numbers going into <tt class="varname">ddrrc</tt> carry no meaning
whatsoever. These should definitely be defined as constants
somewhere. I would probably declare an enumeration and make the
function return a value of the enumeration type.</p>
<p>There are also some efficiency concerns. The first 3 characters
of <tt class="varname">mtgrec</tt> are potentially extracted up to
7 times as we iterate through the loop. The obvious solution to
this is to extract them just once, with a statement like</p>
<pre class="programlisting">
const string mtgDay = mtgrec.substr(0, 3);
</pre>
<p>but I suspect that this would be missing an opportunity to
improve the design further, by introducing a proper structure to be
used in place of a string. The string appears to be a collection of
structured data and using a string for the data hides that
structure. Defining a proper class (or struct) for that data would
bring that structure out as well as being more efficient than using
<tt class="methodname">string::substr()</tt> to extract the
components.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e151" id="d0e151"></a>The Winner of
SCC 30</h2>
</div>
<p>The editor's choice is:</p>
<p><span class="bold"><b>Neil Youngman</b></span></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="d0e164" id="d0e164"></a>Francis'
Commentary</h2>
</div>
<p>My first reaction to the student's question is 'Are you
surprised that the code has an error?' I would rapidly follow it up
with 'If the corrected code passes the compiler, would you trust
it?' I think that the only acceptable answer to both these
questions is 'no'.</p>
<p>My next question is 'What should you do about it?' I would try
to guide the student into 'Redesign the code and factor the
separate actions into their own informatively named functions.' If
performance becomes an issue after doing that, it is time to
consider helping the compiler with the <tt class=
"literal">inline</tt> keyword.</p>
<p>Most of the reorganisation I want to do is concerned with the
implementation so I will move that out into the unnamed
namespace.</p>
<p>Before I do any of that, I take strong objection to both the
function name (spelt in all uppercase) and the function return
type. Any function whose name asks a question should return a
<tt class="type">bool</tt>. However it seems to me that the
function does not answer a simple binary question but asks
something else for which there answer is a choice of five things. I
have no idea what DDR means in this context (I am pretty sure it
does not refer to 'Dance Dance Revolution', 'Developers Diversified
Reality' nor to some form of SDRAM), nor what the classification
stored in an out-parameter (<tt class="varname">ddrrc</tt>) means
so I will have to use some placeholders. These placeholders should
be replaced with meaningful names. <tt class="literal">enum</tt>s
are designed to deal with this kind of issue. As the enumeration
constants need to be available in multiple translation units, it
needs to be declared in a header file. Let me start with that:</p>
<pre class="programlisting">
#ifndef DDR_DECLARATIONS_H
#define DDR_DECLARATIONS_H
#include &lt;string&gt;

enum ddr_classification{
  ok, too_short, bad_day, bad_flag,
  bad_symbol, err5
};
ddr_classification classify(
      std::string const &amp; mtgrec,
      std::string &amp; outrec);
inline 
void IS_IT_A_DDR(std::string const &amp; mtrec,
                 std::string &amp; temprec,
                 int &amp; ddrrc) {
  ddrrc = classify(mtrec, temprec)+1;
}
#endif
</pre>
<p>I have provided a simple forwarding function to provide
temporary support for the ill-named function so that no immediate
changes need to be made to code elsewhere. I would expect that to
be rapidly replaced. I have made ok take a zero value so that in
future it will be possible to use the return value of <tt class=
"function">classify()</tt> for a rapid check of validity. Note that
I use fully qualified names in the header file and that I have
added a <tt class="literal">const</tt> qualifier to the first
parameter.</p>
<p>There is also the question of those two string variables; they
aren't variable nor are the local (though they could be static).
Such items belong in the associated unnamed namespace.</p>
<p>Here is the start for the implementation file:</p>
<pre class="programlisting">
#include &quot;ddr_declarations.h&quot;
using std::string;
namespace {
  string const daynames[] = {&quot;Sun&quot;, &quot;Mon&quot;,
                          &quot;Tue&quot;, &quot;Wed&quot;, &quot;Thu&quot;,
                          &quot;Fri&quot;, &quot;Sat&quot;};
  char * const daynumber = &quot;0123456&quot;;
  char * const symbols = &quot;BCLMOPSTW*&quot;;
}
</pre>
<p>I would favour a better name for symbols but without knowing the
context that is the best I can do. Now let me try to write a
halfway sensible definition for <tt class=
"function">classify()</tt>.</p>
<pre class="programlisting">
ddr_classification classify(
      std::string const &amp; mtgrec,
      std::string &amp; outrec){
  if(mtgrec.size() &lt; 8) return too_short;
  int daynum(dayname_to_int(mtgetrec));
  if(daynum &gt; 6) return bad_day;
  if(not valid_flag(mtgrec.substr(3, 1))) 
            return bad_flag;
  if(valid_symbol(mtgrec.substr(7, 1))){
    outrec = daynumber[daynum] 
                  + mtgrec.substr(1);
    return ok;
  }
  else return bad_symbol;
}
</pre>
<p>Now notice that if the original code's <tt class=
"literal">for</tt>-loop ever exited that the student's <tt class=
"varname">badday</tt> variable must be equal to 7. There just isn't
any other way through that nest of <tt class="literal">if</tt>'s.
That was far from obvious in the original. Separating out the
various conditions and only continuing if everything is still
checking out leads to much clearer code. It also much better models
the way would handle the problem ourselves. First check that the
first three characters represent an abbreviation for a day, next
check that the fourth symbol is acceptable then check that the
eighth one is OK. Human beings might only notice that there were
too few symbols at that last stage, though it is easier to check it
first from a program perspective.</p>
<p>Now let me go back and add the requisite helper functions (which
will go in the unnamed namespace).</p>
<pre class="programlisting">
int dayname_to_int(string const &amp; mtgetrec){
  for(int i(0); i != 7; ++i){
    if(mtgrec.substr(0, 3) == daynames[i]{
      return i;
    }
  }
  return 7;
}
</pre>
<p>Yes, I know there is a magic number lurking in there, but I am
running short of time if David is to get this in time to use
it.</p>
<pre class="programlisting">
bool valid_flag(char flag){
  if(flag == '0') return true;
  if(flag == '1') return true;
  return false;
}
bool valid_symbol(char symbol){
  for(int i(0); i != strlen(symbols); ++i){
    if(symbol == symbols[i])
      return true;
  }
  return false;
}
</pre>
<p>Now I think that this code represents the intention of that
provided by the student. It would have been much easier had the
student specified what the code was intended to do. Notice that the
coding error was a direct consequence of an inappropriate view of
how to code the problem. The student was willing to use all kinds
of tools he had found in the Standard C++ Library but the tool he
really needed was his own brain. Sadly once the code was working
too many instructors would accept it.</p>
<p>Remember that this is part of the Dialogue section of C Vu so
you have an implicit invitation to critique my solution as well as
add any other useful information you have about the actual
problem.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e233" id="d0e233"></a>Student Code
Critique 31</h2>
</div>
<p>(Submissions to <tt class="email">&lt;<a href=
"mailto:scc@accu.org">scc@accu.org</a>&gt;</tt> by January
10th)</p>
<p><span class="emphasis"><em>Here is the code I have using the
equation to drop the lowest number from the grades. The problem is,
if I change up number 3 and number 4, I get a different answer. I
used the numbers 80, 84, 60, 100 and 90. Putting the numbers in
like that, I get 88 but, if I mix up the 100 and 60 then I get a
grade of 81. Can anyone tell me why it is not finding the lowest
number and just dropping it when I tell it to (-
lowest)?</em></span></p>
<pre class="programlisting">
#include &lt;iostream&gt;
#include &lt;iomanip&gt;

using namespace std;

int main() {
int test1, test2, test3, test4,test5,average,
                          averagescore,divide;

cout &lt;&lt;&quot;This program will gather five test
                                scores and\n&quot;;
cout &lt;&lt;&quot;drop the lowest score, giving you the
                                   average\n&quot;;
cout &lt;&lt;&quot;\n&quot;;
cout &lt;&lt;&quot;Please enter Five Test scores\n&quot;;
cin &gt;&gt; test1&gt;&gt;test2&gt;&gt;test3&gt;&gt;test4&gt;&gt;test5;

int lowest = test1;
       // test 1 is the lowest number unless
if (test2 &lt; test1)lowest = test2;
       // test 2 is smaller than test 1 unless
if (test3 &lt; test2)lowest = test3;
       // test 3 is smaller than test 2 unless
if (test4 &lt; test3)lowest = test4;
       // test 4 is smaller than test 3 unless
if (test5 &lt; test4)lowest = test5;
       // test 5 is smaller than test 4.

average = (test1+test2+test3+test4+test5);
       // all test scores averaged together
averagescore = average - lowest;
       // average score minus the lowest grade
divide = averagescore /4;
       // average grade is then divided by 4
cout &lt;&lt; divide&lt;&lt; endl;
       // final grade after division
return 0;
}
</pre>
<p>Besides the question asked by the student, this code gives you a
chance to discuss topics such as extensibility, design and style.
Please address as many issues as you consider necessary, without
omitting the answer to the original question.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
