    <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 10</title>
        <link>https://members.accu.org/index.php/journals/1123</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 13, #3 - Jun 2001 + 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="https://members.accu.org/index.php/journals/">All</a>

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

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

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c120/">133</a>
                    (12)
<br />

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

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

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

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

                    -                        <a href="https://members.accu.org/index.php/journals/c120+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 10</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 07 June 2001 13:15:46 +01:00 or Thu, 07 June 2001 13:15:46 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a>Critique of
Student Code 9 - C Vu Volume 13- No.2</h2>
</div>
<p>Sadly there was only a single entry this time. Even sadder was
that it came from someone who teaches C++ courses at a community
college. I am exercising my editorial right to protect contributors
by suppressing his name and other details that might identify him.
The reason is that his code exhibits so many flaws and his critique
(in my opinion) is so far off beam that while I will happily award
him the prize (he deserves it because he made the effort to enter,
which puts him ahead of the rest) I cannot in all honesty hold up
his entry as a good example. I only hope he understands. I have
commented his critique. If readers think I should not have done, I
agree that it was a close call. I think if I had had other entries
I would have quietly returned this one. Anyway, here is the
original code slightly cleaned up):</p>
<pre class="programlisting">
// Design Student Class,
#include &lt;iostream.h&gt;
class Student //specify an object
  {
  private:
    int subjcode[10];//array of subject codes
    int mark[10]; // array of marks
    int num_marks;//number of subjects taken
  public:
    Student(){}
//default constructor,no args,does nothing
    Student(int grd, int cd) 
//constructor(two args)
    {  subjcode= cd;
      mark = grd;
    }
    void showstudent()//display data
    {
      cout&lt;&lt;&quot;\nYour Subject Code is:&quot;
                     &lt;&lt;subjcode&quot;;
      cout&lt;&lt;&quot;\nYour mark is:&quot;&lt;&lt;mark;
    }
};

void main()
{
  int count=2;//count controls for loop
  class Student[count];//array for 2 students
    for (int n=0; n&lt;2; n++)
    {
      student[n].showstudent();
//prints array
    }
  Student[0]();
//no marks so use default constructor
//First element of array is subject code,
//2nd element is grade
  Student[1](0,87);
  Student[0](0,78);
  Student[0](1,83);
  Student[1](2,66);
  Student[1](3,92);
}
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e25" id="d0e25"></a>Entry from
anon</h2>
</div>
<p>The Student Code Critique Competition 9 column in C Vu Issue
Volume 13 - No. 9 starts off, &quot;Something short and confused
(confusing?) this time, and in C++&quot;. &quot;Confused&quot; is certainly
appropriate for the author of this code. It is obviously from a
rank amateur at C++. But it looks familiar to me since I teach ...
The errors spotted in this code are pretty common mistakes for
beginners.</p>
<p>The problem given to us by Francis Glassborow is to write what
you think was the spec to which the student was responding. Here's
what I think it was:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>&quot;Write a class that defines a student. Data members should
include a subject code and one to hold the student's grade. Add a
function to print out the results.&quot;</p>
</blockquote>
</div>
<p>Short - but I think adequate.</p>
<p class="editorial"><span class="emphasis"><em>FG: Maybe, but I
wonder if the task was:</em></span></p>
<div class="blockquote">
<blockquote class="blockquote">
<p><span class="emphasis"><em>Implement a class to record an
individual students marks. The student may be taking up to 10
subjects. Subjects are identified by a subject number. Write a
short program to test your class.</em></span></p>
</blockquote>
</div>
<p>Now. et us see what the student did wrong in designing this
class. First of all, the braces do not line up. The student put the
closing brace for each block of code in line with the opening brace
most of the time, but the opening and closing braces are not lined
up where I think they should be. For instance, the very first
opening brace for the class should line up under the 'c' of class
as below: I know, some programmers put the opening brace on the
same line as the class header, but I prefer the opening brace on
the next line under the first letter of the header.</p>
<p class="editor"><span class="editorial">FG: Sorry, but this a
typical remark from some teachers. The layout of the program can
help with avoiding some types of errors but is a red-herring here.
Indeed the student has obviously tried to use a sensible and
consistent layout and criticism of small failings are unhelpful and
distract from the real problems.</span></p>
<p>Then the keyword <tt class="literal">private:</tt> should be
indented two or three spaces on the next line. Thus, we have:</p>
<pre class="programlisting">
class Student
{
  private: 
</pre>
<p>The next really big error, in my opinion, is making the
<span class="property">subjcode</span> data member an array. I
think it should be</p>
<p class="editorial"><span class="emphasis"><em>FG: And I think the
writer should have assumed this was not an error and have asked why
there should be an array of subject codes. Instead he should have
been thinking about ways to couple codes and marks (modern C++
instruction might suggest the use of the <tt class=
"classname">pair&lt;int, int&gt;</tt>, or even <tt class=
"classname">pair&lt;Subject, Mark&gt;</tt> because even if subject
codes look like numbers they are not integers (you cannot do
arithmetic with them). On the same theme, some container such as a
<tt class="classname">vector</tt> would have advantages. (You could
have <tt class="methodname">add_subject(int, int)</tt> that would
simply use <tt class="methodname">push_back()</tt> to allow the
vector to grow naturally, another possibility would be a <tt class=
"classname">map</tt> - the instructional potential is
considerable)</em></span></p>
<pre class="programlisting">
int subjcode; // no array.
</pre>
<p>Ditto for</p>
<pre class="programlisting">
int mark;   // it should not be an array.
</pre>
<p class="editorial"><span class="emphasis"><em>FG: I think the
above are good examples of how not to use comments even in
instructional code. They mean nothing without the context of the
supposedly erroneous code.</em></span></p>
<p>Just instantiate an object as an array in <tt class=
"function">main()</tt>.</p>
<p class="editorial"><span class="emphasis"><em>FG: Given the
assumption as to the problem, I do not see how this is
relevant.</em></span></p>
<p>The next error is defining the data member <span class=
"property">num_marks</span>. It is declared but not used anywhere
in the program. Why have it if it is not used? Actually, it's not
even needed. If you want to know how many real grades you have in
the array that you eventually instantiate, just assign the number
zero to <span class="property">subjcode</span>, and walk through
the array (instantiated in <tt class="function">main()</tt>) while
<tt class="literal">subjcode != 0</tt>. Then you can print out the
structures as long as the <span class="property">subjcode</span> is
not equal to zero.</p>
<p class="editorial"><span class="emphasis"><em>FG: And all the
while there is the 'better' option of using an STL container that
will know its own size. But even if you were using an array,
caching the size instead of repeatedly walking it makes perfectly
good sense and is not an error, or even, in my opinion, a design
error. The only mistake here is that the student has not tested the
code. But given the very real problems that are to come, that is
not surprising.</em></span></p>
<p>The default constructor has comments of &quot;Default constructor, no
args, does nothing&quot;</p>
<p>In my opinion, if it does nothing, its useless, don't bother
typing it in! Just because the default constructor takes no
arguments doesn't mean it should do nothing. In fact, it should at
least initialize the data members to some default value (zero). The
default constructor is called to instantiate an array of 10 objects
of type <tt class="classname">Student</tt>. My default constructor
looks like this:</p>
<pre class="programlisting">
Student()
{
  subjcode = 0;
  mark     = 0;
}
</pre>
<p class="editorial"><span class="emphasis"><em>FG: Now this is
particularly interesting. The student could not do anything (well
apart from zero <tt class="prompt">num_marks</tt>) in the
constructor because you can only default initialise an array. The
student's constructor should have read:</em></span></p>
<pre class="programlisting">
<span class="emphasis"><em>Student(): num_marks(0) {}</em></span>
</pre>
<p class="editorial"><span class="emphasis"><em>However the writer
of this critique could, and should have done
better:</em></span></p>
<pre class="programlisting">
<span class=
"emphasis"><em>Student():subjcode(0), mark(0) {}</em></span>
</pre>
<p>Another big mistake in this program is that there is no
assignment function other than the constructor! The class needs a
way of changing the data after the objects are created, so I added
the following method to the class:</p>
<pre class="programlisting">
void assigndata (int cd, int grd)
{
  subjcode = cd;
  mark     = grd;
}
</pre>
<p class="editorial"><span class="emphasis"><em>FG: It maybe poor
design, but a perfectly good assignment already exists once a
constructor with two parameters is provided:</em></span></p>
<pre class="programlisting">
<span class="emphasis"><em>void example(){
  Student s;
  S = Student(1234, 57);
}</em></span>
</pre>
<p class="editorial"><span class="emphasis"><em>so it is hardly
fair to classify this as a big mistake. And is still just burdening
the student with minor errors that do nothing to tackle the root of
the problem. In fact the student is likely to have lost interest
before the real problems are addressed.</em></span></p>
<p>There is also an error in the constructor that takes two
arguments, Student::Student(int grd, int cd); This constructor is
ok except the data members are reversed in order from the class
definition. Be consistent and keep the same order. It's a lot
easier on anyone reading the code, even the author. Write:</p>
<pre class="programlisting">
Student(int cd, int grd)
</pre>
<p>The constructor works well otherwise.</p>
<p class="editorial"><span class="emphasis"><em>FG: Again, this is
not an error. And it emphasises the wrong thing. Consistency has
nothing to do with it (the internal order is private and irrelevant
to the public interface.) The reason for putting the subject code
first then the mark is exactly because that is the way we would
normally think about it subject:mark not
mark:subject</em></span></p>
<p>The student who wrote this code has made the classic beginner's
mistake of not declaring an object of the type class <tt class=
"classname">Student</tt>. The reason his code does not work is
because he has no objects to work on. What should go in main() is
something like the following:</p>
<pre class="programlisting">
Student JoeStudent[10];
</pre>
<p>The above code gives you an array of ten objects of type
<tt class="classname">Student</tt>. It is an easy exercise to fill
the array using a while loop or fill the array by &quot;hard coding&quot;.
Here's my final program:</p>
<p class="editorial"><span class="emphasis"><em>FG: At last we come
to the fundamental problem, all the rest is fog until this issue
has been tackled. But not that the writer still has an 'error' in
his code because he is using the pre-standard headers. A small
issue, unless you are teaching and choose to pick on so many things
that are purely matters of style. Note that I have changed the
layout for compactness.</em></span></p>
<pre class="programlisting">
#include &lt;iostream.h&gt;
class Student {// define the class
  private:
    int subjcode;  //subject codes array
    int mark;  
// marks for each subject code array
  public:
    Student(){
      subjcode = 0; mark    = 0;
    } 
// default constructor, no args, does nothing
    Student (int cd, int grd){
// constructor  (two args)
      subjcode= cd;   
      mark = grd;
    }
void Student::assigndata(int code, int grade){
      Student::subjcode = code;
      Student::mark =  grade;
  }
    void showstudent() { //display data
      cout&lt;&lt;&quot;\nYour Subject Code is:&quot; &lt;&lt;subjcode;
      cout&lt;&lt;&quot;\nYour mark is:&quot;&lt;&lt;mark&lt;&lt;endl;
     }
};   // end of class Student

int main() // should be int to conform to ANSI standard
{
    Student JoeStudent[10];  
//declare an array of ten Student objects
// assign data to part of the array for
// testing
    JoeStudent[0].assigndata(1234,100);
//assign data to one object in the array
    JoeStudent[1].assigndata(7562, 90); 
    // print out the data
    for (int n=0;n&lt;10;++n)    {
        if(JoeStudent{n].subjcode != 0)
          JoeStudent[n].showstudent();
//prints array subscript if subjcode &gt; 0
    }
    return 0;
}  // end of main
</pre>
<p><span class="emphasis"><em>And here is how that code could have
been written. Note that inline definitions are far more
questionable than any matters of code layout</em></span></p>
<p><span class="emphasis"><em>In <tt class=
"filename">Student.h</tt></em></span></p>
<pre class="programlisting">
#ifndef STUDENT_H
#define STUDENT_H
#include &lt;iostream&gt;
class Student {
  public:
    Student(int subject_code=0, int mark=0)
    void showstudentdata(std::ostream&amp; =std::cout) 
// copy ctor, copy assignment and dtor work 
// correctly
  private:
    int subjcode, mark;  
};
#endif
</pre>
<p><span class="emphasis"><em>In <tt class=
"filename">test.cpp</tt></em></span></p>
<pre class="programlisting">
int main() {
  Student studentrecord[10];  
  studentrecord[0] = Student(1234, 100);
  studentrecord[1] = Student(7562, 90);
  for (int n=0;n&lt;10;++n){
    studentrecord[n].showstudentdata();
    std::cout &lt;&lt; '\n'
  }
  return 0;
}
</pre>
<p><span class="emphasis"><em>In <tt class=
"filename">student.cpp</tt></em></span></p>
<pre class="programlisting">
#include &quot;student.h&quot;
Student::Student(int s, int m): subjcode(s),mark(m) {}
void Student::showstudentdata(std::ostream &amp; out){
  if(subjcode)
    out&lt;&lt; &quot;Subject number &quot; &lt;&lt; subjcode 
       &lt;&lt; &quot; Mark: &quot; &lt;&lt; mark;
}
</pre>
<p><span class="emphasis"><em>Now please study the above two
programs. Notice that the contestant's code is cluttered with
comments that add little if anything. Mine is comment free (does it
need comments?, well there is one place I might have added a
comment for a student) Note also that the contestants code tries to
access private data because the test for 'real data' is at the
wrong place. He rushed his solution to meet a deadline, but had he
instinctively tested where it mattered, he would not have made the
mistake.</em></span></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e209" id="d0e209"></a>Student Code
Critique 10</h2>
</div>
<p>OK let us try something slightly different this time. Go back to
my interpretation of the student's task (a class to manage the
subjects taken and marks attained by a single student) and write a
model answer. All types you need should be properly implemented
(note that subjects and marks are types) in an appropriate fashion.
Now explain clearly your reasons for the different design decisions
you made.</p>
<p>Entires by July 15 please.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
