Recipes

Function definition recipe

  • Choose function and parameter names.
  • Choose the types of the parameters and the type of the output (if any).
  • Write the function header, which specifies the names.
  • Write the function body, the code for the function.

Program recipe

  • Import modules
  • Define constants
  • List tasks as comments
  • Create user-defined functions
    • Choose identifiers and types
    • Create function header
    • Import modules (beginning of program)
    • Define constants (beginning of function)
    • List tasks as comments
    • Fill in details of function tasks
  • Fill in details of program tasks

Branching recipe

  • Identify cases
  • Order cases
  • Choose conditions
  • Simplify

Better program checklist

  • Are types and meanings of all variables and constants clear?
  • Are there any “magic” numbers?
  • Is the organization and use of blank lines clear?
  • Are the steps indicated using comments?
  • Do functions have comments explaining the purpose, preconditions, and postconditions?
  • Are there helper functions for repeated and other tasks?
  • Are there more parameters that could be added to a function?

Testing recipe

  • Order functions by dependency
  • Test in order of dependency
    • Black box testing
    • White box testing
  • To find sources of error:
    • Tracing and visualization
    • Print statements
    • Narrow focus

While loop recipe

  • Express the task as iteration.
  • Write a condition.
  • Ensure initial values of variables checked in the condition.
  • Ensure the condition can change in the body of the loop.

For loop recipe

  • Express the task as iteration.
  • Specify starting and ending values.

Class design recipe

  • Choose meaningful attribute names.
  • Choose attribute types.
  • Record attribute names and types of attributes.

Method recipe

  • Add the method definition to the class definition.
  • Make the object the first parameter, with name self.
  • Use dot notation for attributes.

Recursion recipe

  • Express the problem in terms of a problem on an input closer to a base case.
  • Make sure to have a base case.
  • Make sure to have a recursive case.
  • Make sure all possibilities are covered.