When is a class too long?

Dean J picture Dean J · Feb 8, 2010 · Viewed 16.7k times · Source

When is a function too long? is a subset of this question, I think.

What are a few good metrics for determining that a class is too long?

I'm rerevising a set of code acceptance guidelines for a project with external contractors, and realized I haven't covered this in the past, but should cover this in the future.

Answer

Steven picture Steven · Feb 8, 2010

When it has more than one responsibility.

Let me quote Robert C. Martin's Clean Code here:

The first rule of classes is that they should be small. The second rule of classes is that they should be smaller than that. ... With functions we measured size by counting physical lines. With classes we use a different measure. We count responsibilities. [Chapter 10, page 136]