Standards-5
Functions and Coding


Major errors

Each function body must have no more than 16 noncomment lines. [30 points each, 25% max]

A noncomment line is defined to be a line that has at least two noncomment, non-white-space characters. For example, a line that only contains a brace does not count as a noncomment line by this definition.

Do not violate other requirements in order to squash a long function definition down. Break the function up into smaller functions and document each function.


Each function body must have no more than 32 noncomment lines. [50 points each, 40% max]

This is the same issue as the preceding item, but more points are taken off for very long function bodies.

A function with a nonvoid return type must return a value. [10-25 points each, 5% max]

If a function whose return type is not void can reach its end without returning a result, you will get a warning (assuming you have requested warnings). Pay heed to those warnings.

Don't make functions do strange compound jobs [40 points each, 3% max]

Make each function have a sensible job. Do not make it do too much. For example, if your intent is to
  1. compute graph g;
  2. print a heading;
  3. print graph g
do not write one function that does the first two steps (compute graph g and print a heading). Those two jobs do not go together. Later, when you modify the software, you might want to compute a graph without printing a heading.

Do not use goto [12 points each, 25% max]

Do not use gotos.

Do not use instance methods or static methods. [25 points each, 5% max]

Do not use classes or create instance methods or static methods within a structure definition. Constructors in structure definitions are allowed.

This course is concerned mainly with physical data structures, although it also covers an introduction to abstract data types. Object-oriented programming is covered in CSCI 2540. Since we cannot take the time in this course to discuss object-oriented programming, and students who have attempted to use it have almost always used it incorrectly, do not try to use object-oriented programming.



Intermediate errors

No duplicated function calls with same parameter [4-10 points each, 5% max]

A function body must not make two calls to the same nondestructive function with the same argument when avoiding that would be a simple matter of remembering the result. For example,
  for(i = 0; i < strlen(s); i++) 
  {
    ...
  }
recomputes strlen(s) each time around the loop. If s is not changed in the loop body, then that duplicate call can be avoided as follows.
  int slen = strlen(s);
  for(i = 0; i < slen; i++) 
  {
    ...
  }

Do not write a function call a second time as a way to get the result from a prior call. For example,

  process(x);
  if(process(x)) 
  {
    ...
  }
computes process(x) twice, ignoring the result the first time and testing the result the second time. If your intent is to compute process(x) once, write
  if(process(x)) 
  {
    ...
  }

This requirement does not mean that a program can never call the same function more than once with the same parameter. It only applies to the body of a single function, and only for cases where avoiding the duplicate function call is a simple matter of remembering its result or avoiding wasted calls.

The number of points that you lose depends on how much the duplicate call affects the efficiency of the function.


Avoid code duplication. [4-8 points each, 5% max]

Do not unnecessarily duplicate sequences of code that are more than two lines long. Use a function.

Do not write a statement in both branches of an if-statement where it would be more sensible to write it either before or after the if-statement.


Do not write crippled functions [15 points each, 4% max]

A crippled function is one that unnecessarily fails to do its entire job, and relies on its caller either to get it started or to finish the job. Examples of things that a crippled function might do are
  • insist that its caller initialize an array for it;
  • insist that its caller perform a final step for it;
  • not work correctly on all sensible parameter values, and insist that its caller work around that.


Minor errors

Components of statements [4 points each, 5% max]

All statements that are parts of other statements must be compound statements (surrounded by braces). For example, the body of a loop must be a compound statement. The parts of an if-statement must be compound statements. For example,
  if(x == 0) return 0;
  else return 2*x + 1;
violates this rule. Use
  if(x == 0) 
  {
    return 0;
  }
  else 
  {
    return 2*x + 1;
  }

Compound statements must be correctly indented.


Do not change the value of a call-by-value parameter [8 points each, 4% max]

If a function parameter is passed by value, then do not change the value of that parameter anywhere in the function body. For example, function next defined by
  int next(int n)
  {
    while(!isgood(n))
    {
      n++;
    }
    return n;
  }
takes parameter n by value. It changes the value of n in the function body. Do not do that.

The main function should return a result. [4 points]

Main returns 0 if all went well and nonzero if there was an error.

To return the value of an expression, return that expression [1-2 points each, 1% max]

Instead of
  int x = 0;
  return x;
write
  return 0;
Instead of
  int length = sequenceLength(nextNum(n)) + 1;
  return length;
just write
  return sequenceLength(nextNum(n)) + 1;

Do not write

   return x = 0;
There is no point in storing a value into a variable that is about to be destroyed.


No extraneous parameters [6 points each, 2% max]

A function must not have extra parameters that either are unused or are unnecessary. A parameter is unnecessary if it provides information that the function could easily get from other parameters, or if the parameter is used solely as a local variable in the function, and neither passes information from the caller to the function nor passes information from the function to the caller.

Do not explicitly return a void value [4 points each, 1% max]

If expression E has a void type, do not write
  return E;

No extraneous returns [3 point each, 1% max]

Do not write statement
  return;
where it is not justified. For example,
  void demo(int& x)
  {
    if(x > 0)
    {
       x = 2*x;
       return;
    }
    else
    {
       x = -x;
       return;
    }
  }
has extraneous returns. It is equivalent to
  void demo(int& x)
  {
    if(x > 0)
    {
       x = 2*x;
    }
    else
    {
       x = -x;
    }
  }

Do not treat a function that is not crippled as if it is crippled. [8 points each, 3% max]

If a function does a job, let it do its job. Do not try to work around a perceived mistake in the function that is not really there. For example,
  if(!isEmpty(x))
  {
    process(x);
  }
assumes that you either do not want to do process(x) if x is empty or that function process does not know that it should do nothing when x is empty. It that is a justified requirement of process (stated in the contract of process) then this is fine. But if process already does nothing on an empty value x, then just write
  process(x);

Do not use default parameters [6 points each, 3% max]

We will not discuss default parameters in this course. Do not use them.

Do not store a value into a local variable just to return it [4 points each, 1% max]

If y is a local variable, do not write
  return y = x + 1;
which stores the value of x + 1 into variable y, and then immediately returns that value. There is no point to variable y. Just write
  return x + 1;

Do not use a statement that has no effect [5 points each, 2% max]

Do not write a statement such as
  x + 1;
that does nothing. If you really want to do nothing, use { }.

In a for-loop, do not write

  for(i; i < n; i++) 
  {
    ...
  }
since the first part of the for-loop heading is
  i;
which is a statement with no effect.


Inaccessible code [4 points each, 2% max]

Do not have code that can never be executed. For example, if you write
  return n;
  printf("%i", n);
the printf statement can never be performed.