Video in progress
How do you tell that a codebase has too many patterns, and how do you remove excess abstraction?
The signs: interfaces with a single implementation, factories that create only one thing, having to open 6–7 files for a simple change, and layers added "because we'll need it in the future". Remove them by inlining in small steps, with tests as a safety net.
📖 Answer
In one project, a simple function "change language" looked like this: LanguageController → LanguageFacade → LanguageServiceFactory → LanguageService (interface) → LanguageServiceImpl → LanguageStrategyResolver → DefaultLanguageStrategy. There was only one strategy and all it did was user.setLang(lang).
Signals:
- an interface has a single implementation and a second isn't in sight (you don't need an interface for mocking: Mockito can mock a class too);
- a factory always returns the same class;
- a Facade just calls one method and returns: no added value;
- a new developer clicks "Go to implementation" for 15 minutes to find a simple bug;
- names describe the pattern rather than the meaning:
AbstractBaseManagerFactoryImpl.
How to clean up:
- First have an integration test that covers the behavior.
- Start inlining from the innermost "empty" layer: the IDE's "Inline" refactoring does most of the work.
- Run the tests at every step and make small commits, so the reviewer follows too.
- If it's a public API (an interface used by another module or team), don't delete it without them.
Which abstractions to keep: real points of variation (several payment providers), the boundary to an external system (an HTTP client you swap in tests), and module boundaries.
My rule: add a pattern when the second or third concrete case shows up, not the first. Adding a needed abstraction later is easy, while getting rid of a wrong abstraction is far more expensive.
Sign up to mark studied questions and save your progress.