Code - Reusable (Duplicate) - Program Structure
Table of Contents
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:
- 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.
With reusability, a language needs to implement an import functionnalities in order to locate the reusable.
2 - Articles Related
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.