Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a class too big or too small? [closed]

Tags:

oop

I recently had my code reviewed by a senior developer in my company. He criticized my design for using too many classes. I am interested to hear your reactions.

I was tasked to create a service which produces an xml file as a result of manipulating 3 other xml files. Let's name these aaa.xml, bbb.xml and ccc.xml. The service works in two phases. In phase one it scrubs aaa.xml against bbb.xml. In the second phase, it merges the product of phase one with ccc.xml to produce a final result.

I chose a design with three classes: an XmlService class which used two other classes, a scrubber class and a merger class. I kept the scrubbing and merging classes separate because the both classes were large and featured distinct logic.

I thought my approach was good because it kept my classes small and cohesive. My approach also helped to control the size of my test class.

The senior developer asserted that the scrubbing and merging classes would only be used by the XmlService class, and should therefore be part of it. He felt this would make the XMLService cohesive and this is what being cohesive means according to him. He also feels that breaking up classes this way makes them loose cohesiveness.

The irony is I tried to break these classes to achieve cohesiveness. What do you think? Who is right or wrong? Are we both right? Thank you for your suggestions.

like image 471
Seagull Avatar asked Jul 21 '10 20:07

Seagull


People also ask

How big is too big for a class?

The National Education Association (NEA) recommends an ideal class size of 15, but with dwindling education budgets this is an unattainable ratio for most school boards.

How big should classes?

Researchers generally agree a class size of no larger than 18 students is required to produce the desired benefit. You read that right—the ideal class size is 18 kids.

How long is too long for a function?

The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that. Functions should not be 100 lines long.


2 Answers

If you follow the single responsibility principle (and based on the tone of your question, I think you do follow it), then the answer is clear:

  1. A class is too big when it does more than one thing; and
  2. A class is too small when it fails to fulfill its purpose.

That's very broad and indeed subjective -- hence the struggle with your colleague. On your side, you can argue:

  • There's absolutely no problem in creating additional classes -- It's a non-issue, compile-wise and runtime-wise.
  • The current implementation of the service may suggest that these classes "belong" to it, but that may change.
  • You can test each functionality separately.
  • You can apply dependency injection.
  • You ease the cognitive load of understanding the inner working of the service, because its code is smaller and better organized.

Furthermore, I think your boss has a misguided understanding of cohesion. Think of it as focus: the narrower the focus of your program, the higher the cohesion. If the code on your satellite classes is merged within the service class, the latter becomes less focused (less cohesive). It's generally accepted that higher cohesion is preferred over lower cohesion. Try to enlighten his/her view about it.

like image 134
Humberto Avatar answered Oct 28 '22 13:10

Humberto


Cohesion is a measure of how strongly related is the functionality within a body of code. That said, if merging and scrubbing aren't strongly related, including them both in the same class reduces cohesion.

The Single Responsibility Principle is also worth mentioning here. Creating a class for the sole purpose of scrubbing and another for the sole purpose of merging follows this principle.

I'd say your approach is the better of the two.

like image 30
tQuarella Avatar answered Oct 28 '22 14:10

tQuarella