NewsFive Ways to Reduce the Size of Classes

Five Ways to Reduce the Size of Classes content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories