A method must not change the value of a parameter. See changing call-by-value parameters. |
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. |
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. |
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
|
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); |
Make each method have a sensible job. Do not make it do too much.
For example, if your intent is to
|
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. |