Skip to content

Commit 465ca94

Browse files
add DRY principle
1 parent 961e006 commit 465ca94

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

@@ -375,103 +376,6 @@ function parseBetterJSAlternative($code) {
375376
```
376377
**[⬆ back to top](#table-of-contents)**
377378

378-
### Remove duplicate code
379-
Do your absolute best to avoid duplicate code. Duplicate code is bad because
380-
it means that there's more than one place to alter something if you need to
381-
change some logic.
382-
383-
Imagine if you run a restaurant and you keep track of your inventory: all your
384-
tomatoes, onions, garlic, spices, etc. If you have multiple lists that
385-
you keep this on, then all have to be updated when you serve a dish with
386-
tomatoes in them. If you only have one list, there's only one place to update!
387-
388-
Oftentimes you have duplicate code because you have two or more slightly
389-
different things, that share a lot in common, but their differences force you
390-
to have two or more separate functions that do much of the same things. Removing
391-
duplicate code means creating an abstraction that can handle this set of different
392-
things with just one function/module/class.
393-
394-
Getting the abstraction right is critical, that's why you should follow the
395-
SOLID principles laid out in the *Classes* section. Bad abstractions can be
396-
worse than duplicate code, so be careful! Having said this, if you can make
397-
a good abstraction, do it! Don't repeat yourself, otherwise you'll find yourself
398-
updating multiple places anytime you want to change one thing.
399-
400-
**Bad:**
401-
402-
```php
403-
function showDeveloperList($developers)
404-
{
405-
   foreach ($developers as $developer) {
406-
$expectedSalary = $developer->calculateExpectedSalary();
407-
$experience = $developer->getExperience();
408-
$githubLink = $developer->getGithubLink();
409-
$data = [
410-
$expectedSalary,
411-
$experience,
412-
$githubLink
413-
];
414-
415-
render($data);
416-
}
417-
}
418-
419-
function showManagerList($managers)
420-
{
421-
   foreach ($managers as $manager) {
422-
$expectedSalary = $manager->calculateExpectedSalary();
423-
$experience = $manager->getExperience();
424-
$githubLink = $manager->getGithubLink();
425-
$data = [
426-
$expectedSalary,
427-
$experience,
428-
$githubLink
429-
];
430-
431-
render($data);
432-
}
433-
}
434-
```
435-
436-
**Good:**
437-
438-
```php
439-
function showList($employees)
440-
{
441-
   foreach ($employees as $employe) {
442-
       $expectedSalary = $employe->calculateExpectedSalary();
443-
       $experience = $employe->getExperience();
444-
       $githubLink = $employe->getGithubLink();
445-
$data = [
446-
$expectedSalary,
447-
$experience,
448-
$githubLink
449-
];
450-
451-
render($data);
452-
}
453-
}
454-
```
455-
456-
**Very good:**
457-
458-
It is better to use a compact version of the code.
459-
460-
```php
461-
function showList($employees)
462-
{
463-
   foreach ($employees as $employe) {
464-
render([
465-
$employe->calculateExpectedSalary(),
466-
$employe->getExperience(),
467-
$employe->getGithubLink()
468-
]);
469-
}
470-
}
471-
```
472-
473-
**[⬆ back to top](#table-of-contents)**
474-
475379
### Don't use flags as function parameters
476380
Flags tell your user that this function does more than one thing. Functions should
477381
do one thing. Split out your functions if they are following different code paths
@@ -1516,3 +1420,103 @@ class Employee {
15161420
}
15171421
```
15181422
**[⬆ back to top](#table-of-contents)**
1423+
1424+
## Don’t repeat yourself (DRY)
1425+
1426+
Try to observe the [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) principle.
1427+
1428+
Do your absolute best to avoid duplicate code. Duplicate code is bad because
1429+
it means that there's more than one place to alter something if you need to
1430+
change some logic.
1431+
1432+
Imagine if you run a restaurant and you keep track of your inventory: all your
1433+
tomatoes, onions, garlic, spices, etc. If you have multiple lists that
1434+
you keep this on, then all have to be updated when you serve a dish with
1435+
tomatoes in them. If you only have one list, there's only one place to update!
1436+
1437+
Oftentimes you have duplicate code because you have two or more slightly
1438+
different things, that share a lot in common, but their differences force you
1439+
to have two or more separate functions that do much of the same things. Removing
1440+
duplicate code means creating an abstraction that can handle this set of different
1441+
things with just one function/module/class.
1442+
1443+
Getting the abstraction right is critical, that's why you should follow the
1444+
SOLID principles laid out in the [Classes](#classes) section. Bad abstractions can be
1445+
worse than duplicate code, so be careful! Having said this, if you can make
1446+
a good abstraction, do it! Don't repeat yourself, otherwise you'll find yourself
1447+
updating multiple places anytime you want to change one thing.
1448+
1449+
**Bad:**
1450+
1451+
```php
1452+
function showDeveloperList($developers)
1453+
{
1454+
   foreach ($developers as $developer) {
1455+
$expectedSalary = $developer->calculateExpectedSalary();
1456+
$experience = $developer->getExperience();
1457+
$githubLink = $developer->getGithubLink();
1458+
$data = [
1459+
$expectedSalary,
1460+
$experience,
1461+
$githubLink
1462+
];
1463+
1464+
render($data);
1465+
}
1466+
}
1467+
1468+
function showManagerList($managers)
1469+
{
1470+
   foreach ($managers as $manager) {
1471+
$expectedSalary = $manager->calculateExpectedSalary();
1472+
$experience = $manager->getExperience();
1473+
$githubLink = $manager->getGithubLink();
1474+
$data = [
1475+
$expectedSalary,
1476+
$experience,
1477+
$githubLink
1478+
];
1479+
1480+
render($data);
1481+
}
1482+
}
1483+
```
1484+
1485+
**Good:**
1486+
1487+
```php
1488+
function showList($employees)
1489+
{
1490+
   foreach ($employees as $employe) {
1491+
       $expectedSalary = $employe->calculateExpectedSalary();
1492+
       $experience = $employe->getExperience();
1493+
       $githubLink = $employe->getGithubLink();
1494+
$data = [
1495+
$expectedSalary,
1496+
$experience,
1497+
$githubLink
1498+
];
1499+
1500+
render($data);
1501+
}
1502+
}
1503+
```
1504+
1505+
**Very good:**
1506+
1507+
It is better to use a compact version of the code.
1508+
1509+
```php
1510+
function showList($employees)
1511+
{
1512+
   foreach ($employees as $employe) {
1513+
render([
1514+
$employe->calculateExpectedSalary(),
1515+
$employe->getExperience(),
1516+
$employe->getGithubLink()
1517+
]);
1518+
}
1519+
}
1520+
```
1521+
1522+
**[⬆ back to top](#table-of-contents)**

0 commit comments

Comments
 (0)