Five Ways to Reduce the Size of Classes

by Cynthia Harvey

These class size reduction techniques can add clarity to your code.

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:

  1. Diminish the workload -- Each class should just one thing.
  2. Avoid primitive obsession -- Don't use collections in their raw form.
  3. 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.
  4. Subclass special-case logic -- If you have logic that isn't used very often, move it to a sub-class or a separate class.
  5. Don't repeat yourself (DRY) -- Discipline yourself to avoid duplicate code in both the writing and the maintenance phases.

View article

This article was originally published on Thursday Jun 16th 2011
Mobile Site | Full Site