Functions should be:

All these desirable properties follow from obeying the Cardinal Rule of Functions.

One Function to a Function

That is, the code implementing a function should have one and only one task to do. Example tasks are:

Notice that the latter tasks correspond to classic control structures (composition of function calls, conditional branching, and iteration). A symptom that the rule has been broken is when several of these control structures appear in one function. Another symptom is when you have trouble keeping a function to less than a dozen lines of code, and/or its lines to less than 72 characters.

Another good sign that the rule has been broken is difficulty naming the function clearly, or using names like scan-and-compress-and-print.

Faculty: Chris Riesbeck
Time: Monday, Wednesday, Friday: 1pm - 2pm
Location:Annenberg G15

Contents

Important Links