Standards-6
Variables, Parameters and Constants


Major errors

Do not use global or static variables [30-50 points each, 30% max]

These notes have very little to say about global and static variables. A global variable is one that is not created inside a function. A static variable is created inside a function, but marked static.

Do not use global or static variables, with a single exception. You may use a global variable that is used only for debugging. For example, a variable that acts as a switch to turn on or off debugging is normally global.

Global constants are allowed.


Uninitialized variables [10 points each, 5% max]

Do not use any variable before you have put a value in it. But remember that the compiler cannot detect all cases where a variable is used without initialization.

Always ask the compiler to warn you about this.



Minor errors

Only use operators ++, --, +=, -=, etc. to create a statement. [3 points each, 2% max]

You can write statement
 n++;
or a similar statement using other operators such as --. But do not use n++ or ++n or n-- or --n in an expression.

Do not use an assignment as an expression, except in special cases [3 points each, 2% max]

Do not use an assignment (variable = expression) as an expression. It buries the variable change inside the expression, making the code confusing.

You can use the special case of a multiple-assignment, as in

 x = y = 0;

Do not change the value of a call-by-value parameter [3 points each, 2% 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.

Do not change the same variable twice in one statement [3 points each, 2% max]

Do not write a statement that includes two explicit changes of a variable. For example, statement
  x = x++;
tries to change x twice. (This kind of thing has undefined behavior in C++.)

Do not make a change that will surely not be seen [2 points each, 1% max]

Do not use ++x where you really mean x+1. Do not change the value of a local variable when the changed value cannot possibly be looked at again. For example, do not write
  x++;
  return test(y);
where x is a local variable.

Avoid duplicate variables without justification [2 points each, 1% max]

Look at the following function definition.
  int f(int x)
  {
    int m = x;
    return m + x + 1;
  }
Variable m is just another name for x. It is never changed. There is no reason to have two names for the same thing, and it only confuses things. A better definition is
  int f(int x)
  {
    return x + x + 1;
  }
There are some cases where having another variable is sensible.
  1. It can help to have a short name for an expression. For example,

      int v = g.info[i].v1;
    
    allows you to write a much shorter name for an expression. You are allowed to create a reference if you want to treat an awkward variable as a simple one, as in
      int& v = g.info[i].v1;
    
    Now each use of v, whether to get its value or to put a value in it, uses variable g.info[i].v1.

  2. If you intend to change a variable, so that it is not always identical to another variable, then you do not have a redundant variable. For example,

      int f(int x, int n)
      {
        int m = x;
        while(m < n)
        {
          ...
          m = m + 1;
        }
      }
    
    is reasonable. Variable m is not just another name for x since m changes but x does not.


Changing variables [3-4 points each, 4% max]

This is used for assignments where you are not allowed to change the value of any variable. Follow the instructions.

No shadowing [2 points each, 1% max]

Do not create a variable in a place where another variable or constant of the same name exists. See shadowing. Option -Wshadow of g++ asks the compiler to warn you if you are shadowing something.

Do not create a variable whose name is the same as a function's name. Do not create a variable whose name is the same as the name of the function that it is in. For example, function sum should not have a variable called sum.


Pointless constant [2 points each, 1% max]

Do not create a variable that is really just a small constant such as 0, 1 or 2, and is never changed. For example, don't say
  int num = 1;
  x = x + num;

Do not overwork a constant [3 points each, 2% max]

A global constant should have one intended meaning. Don't use it for more than one meaning. For example, instead of having a constant MAX that is used for every array, create separate constants for different purposes. If one array is used to hold a name and another array is used to hold a occupation, you might define the following.
  const int nameMaxSize       = 50;
  const int occupationMaxSize = 100;