Code - Reusable (Duplicate) - Program Structure

> Code - (Programming|Computer) Language > ( Information System | Software ) Design - Art of Programming

1 - About

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.

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)
Only for big function

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.

code/design/reusable.txt · Last modified: 2019/03/24 13:49 by gerardnico