Code - Reusable (Duplicate) - Program Structure

1 - About

A powerful concept in most modern programming languages is the ability to group code into reusable units.

Reusable units are programs or block of codes that can be called.

Reusability of code can happen on different levels:

By wrapping code, you can call it wherever you want, without resorting to copy-and-paste.

Wrapping code introduce the notion of dependency.

Even if you aren't developing reusable components, thinking in these terms tends to improve the quality of the software you write. Effective Java (2nd Edition) by Joshua Bloch

Code should be reused rather than copied. 1) Effective Java (2nd Edition) by Joshua Bloch

With reusability, a language needs to implement an import functionnalities in order to locate the reusable.

3 - Rule of thumb

A good rule of thumb is that if a component:

  • is used several times,
  • or is complex enough on its own

it's a good candidate to be a reusable component. See Code - Refactoring

Minimal set of operations that your app needs. DRY: Don't Repeat Yourself.

4 - How to decide if you should create a new function or object ?

Single responsibility principle: a component should ideally only do one thing. If it ends up growing, it should be decomposed into smaller subcomponents.

1)
Only for big function
code/design/reusable.txt ยท Last modified: 2018/10/23 15:28 by gerardnico