Chapter 2, SOLID Design Principles for Go

1. How does the single responsibility principle improve Go code?

By applying the single responsibility principle, the complexity of our code is reduced as it is decomposed code into smaller, more concise pieces.

With the smaller, more concise pieces, we gain increases in the potential usability of that same code. These smaller pieces are easier to compose into larger systems, due to their lighter requirements and more generic nature.

The single responsibility principle also makes tests simpler to write and maintain because when a piece of code has only one purpose, there is only much less scope (and therefore complexity) required to test.

 

2. How does the open/closed principle improve Go code?

The open/closed principle helps reduce the risk of additions and extensions by encouraging us not to change existing code, particularly exported APIs.

The open/closed principle also helps reduce the number of changes needed to add or remove a feature. This is particularly prevalent when moving away from certain code patterns, such as switch statements. Switch statements are great, but they tend to exist in more than one place, and it's easy to miss one instance when adding new features.

Additionally, when problems do arise, they are easier to find, given that they are either in the newly added code or the interactions between it and its usages.

 

3. How does the Liskov substitution principle improve Go code?

By following the Liskov substitution principle, our code performs consistently regardless of the dependencies we are injecting. Violating the Liskov substitution principle, on the other hand, leads us to violate the open/closed principle. These violations cause our code to have too much knowledge of the implementations, which in turn breaks the abstraction of the injected dependencies.

When implementing interfaces, we can use the Liskov substitution principle's focus on consistent behavior as a way of detecting code smells related to incorrect abstractions.

 

4. How does the interface segregation principle improve Go code?

The interface segregation principle requires us to define thin interfaces and explicit inputs. These features allow us to decouple our code from the implementations that are fulfilling our dependencies.

All of this leads to dependency definitions that are concise, easy to understand, and convenient to use, particularly when using them with mocks and stubs during testing.

 

5. How does the dependency inversion principle improve Go code?

The dependency inversion principle forces us to focus on the ownership of the abstractions and change their focus from uses to requires.

It also further decouples our dependency definitions from their implementations. As with the interface segregation principle, the result is code that is more straightforward and separate, particularly from its users.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.86.18