Skip to content

Commit cf1598f

Browse files
author
Tomáš Votruba
authored
Merge pull request #39 from peter-gribanov/dry
Don't repeat yourself [DRY]
2 parents 80097dd + 08c8cc7 commit cf1598f

1 file changed

Lines changed: 101 additions & 97 deletions

File tree

‎README.md‎

Lines changed: 101 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
3. [L: Liskov Substitution Principle (LSP)](#liskov-substitution-principle-lsp)
1212
4. [I: Interface Segregation Principle (ISP)](#interface-segregation-principle-isp)
1313
5. [D: Dependency Inversion Principle (DIP)](#dependency-inversion-principle-dip)
14+
6. [Don’t repeat yourself (DRY)](#dont-repeat-yourself-dry)
1415

1516
## Introduction
1617

@@ -465,103 +466,6 @@ Now we can mock the dependencies and test only the work of method `BetterJSAlter
465466

466467
**[⬆ back to top](#table-of-contents)**
467468

468-
### Remove duplicate code
469-
Do your absolute best to avoid duplicate code. Duplicate code is bad because
470-
it means that there's more than one place to alter something if you need to
471-
change some logic.
472-
473-
Imagine if you run a restaurant and you keep track of your inventory: all your
474-
tomatoes, onions, garlic, spices, etc. If you have multiple lists that
475-
you keep this on, then all have to be updated when you serve a dish with
476-
tomatoes in them. If you only have one list, there's only one place to update!
477-
478-
Oftentimes you have duplicate code because you have two or more slightly
479-
different things, that share a lot in common, but their differences force you
480-
to have two or more separate functions that do much of the same things. Removing
481-
duplicate code means creating an abstraction that can handle this set of different
482-
things with just one function/module/class.
483-
484-
Getting the abstraction right is critical, that's why you should follow the
485-
SOLID principles laid out in the *Classes* section. Bad abstractions can be
486-
worse than duplicate code, so be careful! Having said this, if you can make
487-
a good abstraction, do it! Don't repeat yourself, otherwise you'll find yourself
488-
updating multiple places anytime you want to change one thing.
489-
490-
**Bad:**
491-
492-
```php
493-
function showDeveloperList($developers)
494-
{
495-
   foreach ($developers as $developer) {
496-
$expectedSalary = $developer->calculateExpectedSalary();
497-
$experience = $developer->getExperience();
498-
$githubLink = $developer->getGithubLink();
499-
$data = [
500-
$expectedSalary,
501-
$experience,
502-
$githubLink
503-
];
504-
505-
render($data);
506-
}
507-
}
508-
509-
function showManagerList($managers)
510-
{
511-
   foreach ($managers as $manager) {
512-
$expectedSalary = $manager->calculateExpectedSalary();
513-
$experience = $manager->getExperience();
514-
$githubLink = $manager->getGithubLink();
515-
$data = [
516-
$expectedSalary,
517-
$experience,
518-
$githubLink
519-
];
520-
521-
render($data);
522-
}
523-
}
524-
```
525-
526-
**Good:**
527-
528-
```php
529-
function showList($employees)
530-
{
531-
   foreach ($employees as $employe) {
532-
       $expectedSalary = $employe->calculateExpectedSalary();
533-
       $experience = $employe->getExperience();
534-
       $githubLink = $employe->getGithubLink();
535-
$data = [
536-
$expectedSalary,
537-
$experience,
538-
$githubLink
539-
];
540-
541-
render($data);
542-
}
543-
}
544-
```
545-
546-
**Very good:**
547-
548-
It is better to use a compact version of the code.
549-
550-
```php
551-
function showList($employees)
552-
{
553-
   foreach ($employees as $employe) {
554-
render([
555-
$employe->calculateExpectedSalary(),
556-
$employe->getExperience(),
557-
$employe->getGithubLink()
558-
]);
559-
}
560-
}
561-
```
562-
563-
**[⬆ back to top](#table-of-contents)**
564-
565469
### Don't use flags as function parameters
566470
Flags tell your user that this function does more than one thing. Functions should
567471
do one thing. Split out your functions if they are following different code paths
@@ -1702,3 +1606,103 @@ class Employee {
17021606
}
17031607
```
17041608
**[⬆ back to top](#table-of-contents)**
1609+
1610+
## Don’t repeat yourself (DRY)
1611+
1612+
Try to observe the [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) principle.
1613+
1614+
Do your absolute best to avoid duplicate code. Duplicate code is bad because
1615+
it means that there's more than one place to alter something if you need to
1616+
change some logic.
1617+
1618+
Imagine if you run a restaurant and you keep track of your inventory: all your
1619+
tomatoes, onions, garlic, spices, etc. If you have multiple lists that
1620+
you keep this on, then all have to be updated when you serve a dish with
1621+
tomatoes in them. If you only have one list, there's only one place to update!
1622+
1623+
Oftentimes you have duplicate code because you have two or more slightly
1624+
different things, that share a lot in common, but their differences force you
1625+
to have two or more separate functions that do much of the same things. Removing
1626+
duplicate code means creating an abstraction that can handle this set of different
1627+
things with just one function/module/class.
1628+
1629+
Getting the abstraction right is critical, that's why you should follow the
1630+
SOLID principles laid out in the [Classes](#classes) section. Bad abstractions can be
1631+
worse than duplicate code, so be careful! Having said this, if you can make
1632+
a good abstraction, do it! Don't repeat yourself, otherwise you'll find yourself
1633+
updating multiple places anytime you want to change one thing.
1634+
1635+
**Bad:**
1636+
1637+
```php
1638+
function showDeveloperList($developers)
1639+
{
1640+
   foreach ($developers as $developer) {
1641+
$expectedSalary = $developer->calculateExpectedSalary();
1642+
$experience = $developer->getExperience();
1643+
$githubLink = $developer->getGithubLink();
1644+
$data = [
1645+
$expectedSalary,
1646+
$experience,
1647+
$githubLink
1648+
];
1649+
1650+
render($data);
1651+
}
1652+
}
1653+
1654+
function showManagerList($managers)
1655+
{
1656+
   foreach ($managers as $manager) {
1657+
$expectedSalary = $manager->calculateExpectedSalary();
1658+
$experience = $manager->getExperience();
1659+
$githubLink = $manager->getGithubLink();
1660+
$data = [
1661+
$expectedSalary,
1662+
$experience,
1663+
$githubLink
1664+
];
1665+
1666+
render($data);
1667+
}
1668+
}
1669+
```
1670+
1671+
**Good:**
1672+
1673+
```php
1674+
function showList($employees)
1675+
{
1676+
   foreach ($employees as $employee) {
1677+
       $expectedSalary = $employee->calculateExpectedSalary();
1678+
       $experience = $employee->getExperience();
1679+
       $githubLink = $employee->getGithubLink();
1680+
$data = [
1681+
$expectedSalary,
1682+
$experience,
1683+
$githubLink
1684+
];
1685+
1686+
render($data);
1687+
}
1688+
}
1689+
```
1690+
1691+
**Very good:**
1692+
1693+
It is better to use a compact version of the code.
1694+
1695+
```php
1696+
function showList($employees)
1697+
{
1698+
   foreach ($employees as $employee) {
1699+
render([
1700+
$employee->calculateExpectedSalary(),
1701+
$employee->getExperience(),
1702+
$employee->getGithubLink()
1703+
]);
1704+
}
1705+
}
1706+
```
1707+
1708+
**[⬆ back to top](#table-of-contents)**

0 commit comments

Comments
 (0)