2 Code duplication is not always bad: code duplication vs. flexibility
This chapter covers
- Sharing the code common between independent codebases
- Analyzing trade-offs between code duplication, the flexibility of your code, and speed of delivering
- Deciding when code duplication is a sane choice that gives us loose coupling.
The DRY (Don’t repeat yourself) is one of the most well-known software engineering rules. The main idea behind this pattern is removing duplicated code that will lead to fewer bugs and better reusability of software. When DRY was popular, the systems that we were building were a bit different. They were mainly monolithic, meaning that almost the whole codebase was living on one repository.
Today’s systems evolved, and we tend to build distributed systems with many moving parts. In such architectures, the choice of reducing code duplication has more trade-offs like, for example, introducing tight coupling between components or reducing the development speed of the team. If you have one piece of code used in multiple places, changing it may require a lot of coordination. Where is coordination needed, the process of delivering business value slows down. This chapter will delve into patterns and trade-offs involving duplication code. We will try to answer a question: when code duplication is a sane trade-off, but also when it should be avoided. Let’s transition to the first chapter in which we will start from the architecture that involves code duplication.