A powerful concept in most modern programming languages is the ability to group code into reusable units.
Reusable units are:
- or block of codes that can be called (ie have a name)
Reusability of code can happen on different levels:
- Application Level
- Application (system)
- Block of code Level: See Reusable Block (ie Function or class)
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.
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.
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.