@@ -362,13 +362,26 @@ function parseBetterJSAlternative(code) {
|
362 | 362 | **[⬆ back to top](#table-of-contents)**
|
363 | 363 |
|
364 | 364 | ### Remove duplicate code
|
365 |
| -Never ever, ever, under any circumstance, have duplicate code. There's no reason |
366 |
| -for it and it's quite possibly the worst sin you can commit as a professional |
367 |
| -developer. Duplicate code means there's more than one place to alter something |
368 |
| -if you need to change some logic. JavaScript is untyped, so it makes having |
369 |
| -generic functions quite easy. Take advantage of that! Tools like |
370 |
| -[jsinspect](https://.com/danielstjules/jsinspect) can help you find duplicate |
371 |
| -code eligible for refactoring. |
| 365 | +Do your absolute best to avoid duplicate code. Duplicate code is bad because it |
| 366 | +means that there's more than one place to alter something if you need to change |
| 367 | +some logic. |
| 368 | + |
| 369 | +Imagine if you run a restaurant and you keep track of your inventory: all your |
| 370 | +tomatoes, onions, garlic, spices, etc. If you have multiple lists that that |
| 371 | +you keep this on, then all have to be updated when you serve a dish with |
| 372 | +tomatoes in them. If you only have one list, there's only one place to update! |
| 373 | + |
| 374 | +Oftentimes you have duplicate code because you have two or more slightly |
| 375 | +different things, that share a lot in common, but their differences force you |
| 376 | +to have two or more separate functions that do much of the same things. Removing |
| 377 | +duplicate code means creating an abstraction that can handle this set of |
| 378 | +different things with just one function/module/class. |
| 379 | + |
| 380 | +Getting the abstraction right is critical, that's why you should follow the |
| 381 | +SOLID principles laid out in the *Classes* section. Bad abstractions can be |
| 382 | +worse than duplicate code, so be careful! Having said this, if you can make |
| 383 | +a good abstraction, do it! Don't repeat yourself, otherwise you'll find yourself |
| 384 | +updating multiple places anytime you want to change one thing. |
372 | 385 |
|
373 | 386 | **Bad:**
|
374 | 387 | ```javascript
|
|
0 commit comments