Five Ways to Reduce the Size of Classes
Andrew Binstock writes that the best developers keep their classes to 50-60 lines of code. How can you do that? Here are five techniques:
- Diminish the workload -- Each class should just one thing.
- Avoid primitive obsession -- Don't use collections in their raw form.
- Reduce the number of class and instance variables -- If you have a lot of variables, you can probably move a group of them to a separate class.
- Subclass special-case logic -- If you have logic that isn't used very often, move it to a sub-class or a separate class.
- Don't repeat yourself (DRY) -- Discipline yourself to avoid duplicate code in both the writing and the maintenance phases.
Originally published on https://www.developer.com.
This article was originally published on June 16, 2011