6.6. Methods

Do not change parameters [CHANGE-PARAM: 1-5 points]

A method must not change the value of a parameter. See changing call-by-value parameters.

Only one loop per method [MULTIPLE-LOOPS: 2-8 points]

Each method body can only have one loop.

No duplicated method calls with same parameter [DUP-CALL: 2-6 points]

A method body must not make two calls to the same nondestructive method with the same argument when avoiding that would be a simple matter of remembering the result.

No extraneous parameters [EXTRA-PARAM: 1-3 points]

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

No crippled methods [CRIPPLED: 1-4 points]

A crippled method 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 method 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.

Do not treat a method that is not crippled as if it is crippled. [TRUST: 1-3 points]

If a method does a job, let it do its job. Do not try to work around a perceived mistake in the method 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 method 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);

No strange compound jobs [COMPOUND-JOB: 1-2 pts]

Make each method 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 method that does the first two steps (compute graph g and print a heading). Those two jobs do not go together.

Do not unnecessarily tie jobs together [TIE-TOGETHER: 1-2 points]

This is really a continuation of the previous item. Where sensible, do not write a method that does two jobs so that, if you want to do one of the jobs, you are required to do both. For example, if you want to write the diameter of a graph, it makes sense to break the problem down into two steps: (1) compute the diameter of a graph; and (2) write the result. If you tie the two jobs together, so that the program cannot do one without doing the other, then the program is difficult to modify. Later, you might find that you want to compute the diameter of a graph without writing anything out. Remember that programs are frequently modified.