add DRY Principle

- add DRY definition and example
This commit is contained in:
Julio Lozovei
2019-05-17 14:02:12 -03:00
parent 019d1228aa
commit a42732db85

View File

@@ -32,6 +32,7 @@ Laws, Theories, Principles and Patterns that developers will find useful.
* [The Liskov Substitution Principle](#the-liskov-substitution-principle)
* [The Interface Segregation Principle](#the-interface-segregation-principle)
* [The Dependency Inversion Principle](#the-dependency-inversion-principle)
* [The DRY Principle](#the-dry-principle)
* [TODO](#todo)
<!-- vim-markdown-toc -->
@@ -381,6 +382,36 @@ See also:
- [Inversion of Control](#todo)
- [Dependency Injection](#todo)
### The DRY Principle
[The DRY Principle on Wikipedia](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself)
> Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
DRY is an acronym for _Don't Repeat Yourself_. This principle aims to help developers reducing the repetition of code and keep the information in a single place.
> The opposite of DRY would be _WET_ (Write Everything Twice or We Enjoy Typing).
The principle is easy to understand and can be used with any other methodology, design pattern and/or language. It was cited in 1999 by Andrew Hunt and Dave Thomas in the book [The Pragmatic Developer](https://en.wikipedia.org/wiki/The_Pragmatic_Programmer).
In practice, if you have the same piece of code/information in two (or more) different places, you can use the DRY Principle to merge those pieces into a single one and reuse it wherever you want/need. The DRY principle can be used for codes, documentations, schemas, build systems...
Besides avoiding the repetition, DRY help developers to deliver maintainable, readable and reusable code. It also helps developers to keep the testing step easier - in this specific case, DRY keep unit and integration tests simple. Because if the code isn't repeated, you just need to test it a single time.
In a real-world application, we can translate the use of DRY with this code:
```js
// check if user is logged in
function isUserLoggedIn() {
const user = context.getUser();
return typeof user.id !== null;
}
```
In the example above, to check if the user is logged in we can import this function to anywhere and get the result - without DRY, you would write it every time you need to know if the user is logged in.
## TODO
Hi! If you land here, you've clicked on a link to a topic I've not written up yet, sorry about this - this is work in progress!