Skip to content

Commit 8d6cc01

Browse files
design patterns answers p4
1 parent 3aabfc4 commit 8d6cc01

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

README.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ Sooner or later I will complete it with the relative answers. Feel free to contr
5858
* What is an Anti-corruption Layer?
5959
* Singleton is a design pattern that restricts the instantiation of a class to one single object. Writing a Thread-Safe Singleton class is not so obvious. Would you try?
6060
* The ability to change implementation without affecting clients is called Data Abstraction. Produce an example violating this property, then fix it.
61-
* Write a snippet of code violating the Don't Repeat Yourself (DRY) principle. Then, fix it.
62-
* How would you deal with Dependency Hell?
63-
* Is goto evil? You may have heard of the famous paper "Go To Statement Considered Harmful" by Edsger Dijkstra, in which he criticized the use of the `goto` statement and advocated structured programming instead. The use of `goto` has always been controversial, so much that even Dijkstra's letter was criticized with articles such as "'GOTO Considered Harmful' Considered Harmful". What's your opinion on the use of `goto`?
61+
* [Write a snippet of code violating the Don't Repeat Yourself (DRY) principle. Then, fix it.](#write-a-snippet-of-code-violating-the-dont-repeat-yourself-dry-principle-then-fix-it)
62+
* [How would you deal with Dependency Hell?](#how-would-you-deal-with-dependency-hell)
63+
* [Is goto evil? You may have heard of the famous paper "Go To Statement Considered Harmful" by Edsger Dijkstra, in which he criticized the use of the `goto` statement and advocated structured programming instead. The use of `goto` has always been controversial, so much that even Dijkstra's letter was criticized with articles such as "'GOTO Considered Harmful' Considered Harmful". What's your opinion on the use of `goto`?](#is-goto-evil-you-may-have-heard-of-the-famous-paper-go-to-statement-considered-harmful-by-edsger-dijkstra-in-which-he-criticized-the-use-of-the-goto-statement-and-advocated-structured-programming-instead-the-use-of-goto-has-always-been-controversial-so-much-that-even-dijkstras-letter-was-criticized-with-articles-such-as-goto-considered-harmful-considered-harmful-whats-your-opinion-on-the-use-of-goto)
6464
* The robustness principle is a general design guideline for software that recommends "*be conservative in what you send, be liberal in what you accept*". It is often reworded as "*be a tolerant reader and a careful writer*". Would you like to discuss the rationale of this principle?
6565
* Separation of Concerns is a design principle for separating a computer program into distinct areas, each one addressing a separate concern. There are a lot of different mechanisms for achieving Separation of Concerns (use of objects, functions, modules, or patterns such as MVC and the like). Would you discuss this topic?
6666

@@ -559,3 +559,49 @@ class Crew {
559559
}
560560
```
561561
<br>[⬆ Back to top](#table-of-contents)
562+
563+
### Write a snippet of code violating the Don't Repeat Yourself (DRY) principle. Then, fix it.
564+
565+
Code violating the DRY principle:
566+
```javascript
567+
class Employee {
568+
calculateSalaryNet() {
569+
return this.hoursWorked * this.hourlyWage;
570+
}
571+
572+
calculateSalaryGross() {
573+
return this.hoursWorked * this.hourlyWage + TAX;
574+
}
575+
}
576+
```
577+
578+
Fixed code:
579+
```javascript
580+
class Employee {
581+
calculateSalaryNet() {
582+
return this.hoursWorked * this.hourlyWage;
583+
}
584+
585+
calculateSalaryGross() {
586+
return this.calculateSalaryNet() + TAX;
587+
}
588+
}
589+
```
590+
<br>[⬆ Back to top](#table-of-contents)
591+
592+
593+
### How would you deal with Dependency Hell?
594+
595+
1. The most basic approach is to manually update dependencies to satisfy other package versioning or to ask the maintainer to do this.
596+
2. If I trust the semantic versioning libraries I use, I try to do more relaxed versioning.
597+
2. A better solution for this problem is to keep projects in monorepo. This approach implies other challenges though.
598+
3. Another interesting solution is a [system for easier dependencies management](https://www.youtube.com/watch?v=VNqmHJtItCs) introduced by Netflix Engineering.
599+
600+
<br>[⬆ Back to top](#table-of-contents)
601+
602+
### Is goto evil? You may have heard of the famous paper "Go To Statement Considered Harmful" by Edsger Dijkstra, in which he criticized the use of the `goto` statement and advocated structured programming instead. The use of `goto` has always been controversial, so much that even Dijkstra's letter was criticized with articles such as "'GOTO Considered Harmful' Considered Harmful". What's your opinion on the use of `goto`?
603+
604+
I wouldn't be so radical in either direction. I think that structured programming won the debate because it's the right thing to do. It's easier to reason about a program written in this manner.
605+
On the other hand, even writing this kind of programs we often use the `goto` like style (early returns, exception handling) and it doesn't do any harm.
606+
In my opinion the same applies to the `goto` statements - function-scoped `goto` statements here and there might really make our program simpler and easier to understand.
607+
<br>[⬆ Back to top](#table-of-contents)

0 commit comments

Comments
 (0)