Rules to Write Reusable Code

  • Cohesion in Methods: Do just one thing or a few but closely related
  • Short Methods: Keep the right size so it can be easily understood and modifiable
  • Method Coherency: Similar to others, similar size, error conditions, etc
  • Split high-level from low-level methods: high-level are those which control, check status and low-level those which get the job done
  • Uniform Coverage: Methods must be able to handle all the possible combination of inputs
  • Avoid Coupling: Global objects, etc
  • State Dependant methods: Behaviour shouldn’t change based on external condition, write a new method