Chapter 9. Best practices in code design
This chapter covers:
- Understanding publication and wiring
- Designing objects to be thread-safe
- Optimizing for concurrency
- Dealing with type-safety
- Using objects in collections
“The effort to understand the universe is one of the very few things that lifts human life a little above the level of farce and gives it some of the grace of tragedy.”
Steven Weinberg
It’s often a puzzle to programmers who are new to dependency injection about when to apply it. When it wasn’t widely understood, even experienced practitioners applied it too much or too sparingly. The immature state of libraries contributed to this fuzzy application too, giving one the tools but not the discretion of its applicability.
A more fundamental set of confusion arises from the best practices of the language itself. Are these distinct from the patterns relevant to DI? Are they superseded? Or can they coexist (figure 9.1)?
It turns out that such questions are based on the wrong premise entirely. The best practices portended by a language and its engineering fundamentals are the same as those proffered by dependency injection (figure 9.2).
In other words, design well for the language and problem domain, and you will have designed well for the principles of testing, succinctness, and flexibility.