- **Epistemic status:** #seedlings
![[Pasted image 20220303122553.png]]
As you implement [[Modularity]] in a system, it is important to organize these modules into layers, providing a level of abstraction. This design pattern is powerful because each layer uses only the abstractions it was provided by the layers below it. You will have the flexibility to change the underlying implementation without affecting other areas and reducing the risk of runaway dependencies between modules.
When implementing this design pattern, ask yourself if requirements changed would the module be supported, or would it run into issues? Does the change require parts that are unrelated to the new requirement? The point of this design pattern is to rely on the properties of things you can control, and not on external data that might change at any point. One such example is when bringing an external library. Adding a level of abstraction between your codebase and the library makes it easy to change the library at any given time without needing to change the underlying structure of the code. [[Atomic Design]] is a complementary design pattern that should make it easy to implement a layered design.
You can use the following techniques to increase [[Modularity]] in your system:
- **Avoid coupled modules:** Write modules that don't reveal anything unnecessary to other module's implementation. If you need to manipulate an object's state, make the object do it for you, isolating the other code's implementation.
- **Avoid global data:** When you reference global data, it ties the module to the other components that do the same. If you need to change the global data, it would carry risks across all those modules.
- **Avoid similar functions:** Sometimes functions have the same start and end, but a different [[Algorithm]] in the middle. Spotting these patterns you can modularize those sections, reducing the amount of maintenance (See [[Code maintenance is a routine part of the development process]]).
---
## References
- Thomas, David, and Andrew Hunt. _The Pragmatic Programmer, 20th Anniversary Edition: Journey to Mastery_. Second edition. Boston: Addison-Wesley, 2019.