Code - Reusable (Duplicate) - Program Structure


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

Reusable units are:

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.

Before software can be reusable it first has to be usable.

Ralph Johnson

Even if you aren't developing reusable components, thinking in these terms tends to improve the quality of the software you write.

Code should be reused rather than copied. 1)

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

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.

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.

Only for big function

Powered by ComboStrap