Functions are Friends#

Functions (methods) can be used to do all of the following:

  1. Create descriptive landmarks in the flow of logic

  2. Provide concise summary of inputs for a task

  3. Encapsulate logic into small, testable blocks

  4. Create reusable blocks of application logic

  5. Provide a way to document the purpose of a block of code

  6. Provide a way to document the expected inputs and outputs of a block of code

  7. Provide a way to document the expected behavior of a block of code

Qualities of a good function#

Side Effects

A good function should have the following qualities:

  • Has a descriptive name that clearly communicates its purpose

  • Does one thing and does it well (Single Responsibility Principle)

  • Has a small number of parameters

  • Provides a single level of abstraction

  • Has no side effects

  • Is easy to understand and read (so not too big)