What is a routine? An individual method or procedure with a single purpose
- Descriptive name
- Is documented
- Good layouts with logical organization
- Input variable should not be changed
- No reading or writing to global variables
- Has single purpose
- Routine defends itself from bad data
- Numbers need to be in variables
- All parameters must be used
- No more than 7 parameters
- Parameters ordered thoughtfully
- To reduce complexity
- perfect for hiding information
- pulls blocks out of nested loops or conditionals
- moving code into its own section aids readability
- To avoid duplicating code
- To support subclassing
- You need less new code to override a short, well factored piece of code
- Long and poorly factored code is difficult to manage
- To hide sequences
- forces sequences to happen in the correct order
- To simplify boolean tests
- details of test are out of way
- descriptive function name summarizes purpose
- To improve performance
- can optimize in one place instead of many
- High cohesion =
cosine() - Low cohesion =
cosineAndTan()- b/c doing more than one thing
- When a routine performs one and only one operation
- Strongest and best kind
- Sequential cohesion
- When routine contains operations that must be performed in a specific order
- Share data from step to step
- Dont make up a complete function when done together
- Communicational cohesion
- Operations in a routine make use of the same data but arent related in any other way
- Temporal cohesion
- When operations are combbined into a routine because they are done at the same time
startup()newEmployee()shutDown()
- Procedural cohesion
- When operations in a routine are done in a specific order
- (like reading in auser input orderly?)
- When operations in a routine are done in a specific order
- Logical cohesion
- When several operations are stuffed into the same routine and one of the operations is selected by a control flag
- Coincidental cohesion
- When operations in a routine have no discernable relationship to each other
- Describe everything the routine does
- and side effects
- If the name sounds silly, change the routine functionally
- Avoid meaningless, vague or wishy-washy terms
handleCalculation()performServices()outputUser()processInput()
- Routine interfaces are some of the most error prone areas of a program
- Put input parameters in:
1) input only2) modify and output3) output only
- Use all parameters passed in
- Put status or error variables last
- Dont use routine parameters as working variables
- Copy and assign to local
- Document assumptions about parameters
- Whether param is input only, modified, or output only
- Use of numeric parameters (feet, in, meter)
- Meaning of status cudes and error vals
- Ranges of expected values
- Specific values that should never appear
- Pass variables and objects required to maintain its interface abstraction
Theoretically best max length is one screen, 50-150 lines