3

Let's say I have a class that needs some helper methods to do its work. The number of helper methods starts to grow and the size of the class also starts to grow a lot.

How should I handle this problem?

  1. Should I generalize the methods and create a static Helper class even though I know that, for now, only one class needs it?

  2. Should I just let the class size grow? (I don't like this solution)

  3. Any better solution?

closed as too broad by Doc Brown, 17 of 26, Matthieu M., user53019, Ixrec Dec 23 '15 at 1:45

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • The number of methods is not an issue in itself. If they all conceptually belong to the same class, it would be bad design to artificially spilt it up. – JacquesB Dec 21 '15 at 7:56
  • 2
    @JacquesB, I disagree. A class should only have one responsibility. Lots of helper methods suggests more than one responsibility has crept into the class. So it definitely should be broken up and there's absolutely nothing "artificial" about this. – David Arno Dec 21 '15 at 8:18
  • 4
    What you are asking here is no less than how to create a good program design. There have been entire books written about this topic, you cannot learn this from a 100 line answer in a few minutes. So voting to close as "to broad". Best advice I can give you: read some books / articles about refactoring and the SOLID priciples. – Doc Brown Dec 21 '15 at 8:31
  • @DavidArno: IF the class have more than one responsibility then it should obviously be broken up, regardless of the number of methods. My point is that the number of methods itself is not the problem.. – JacquesB Dec 21 '15 at 9:04
  • 1
    @JacquesB: you are correct, the number of methods is not the problem on itself, but it can be IMHO a very strong code smell. The problematic part of your statement is, what "conceptually belongs to the same class" is not a strictly defined concept. It is often a point of view, and sometimes just an excuse for not thinking hard about enough about a better distribution of responsibilities. – Doc Brown Dec 21 '15 at 10:18
5

Speaking broadly, it sounds like your class has taken on too many responsibilities. It's impossible to say exactly how without knowing the details, but the fact that you're concerned about its growth is a smell/tell/canary-in-the-coalmine. It's likely that you simply need a new class.

Remember, "helpers" are problematic. If you think about it long and hard, there's probably a noun with related verbs that works on/with your original class. There's likely some thing that can stand on its own. If not, it might turn out that your original class is actually two or more classes. If it's not that, well, then you haven't had a chance to think about it enough. There's no such thing as something that does one and only one thing, but requires many, many, many methods to do it. That can't be just one thing.


Anticipating questions about the obnoxious HtmlHelper I'll say this: it could have been better named. Sure, its extension methods are all about rendering HTML, but is that really the best name? Also, it probably does more than it should. Is there really some big "thing" that knows about routing, rendering HTML, client-side validation modes, validation summary CSS, etc...? Mmmmm... my guess is there's more than one person on the MVC team who isn't crazy about the name and responsibilities. Sometimes, the best solutions aren't obvious until we've lived with the alternative for a while.

Did I tell you about the time I met Rod Stewart? It could have been one of the best days of my life. Instead, I went straight home and sold all of his vinyl. He smelled like baby wipes, bourbon, and cat pee. I couldn't understand a word he said. He pushed by me (the radio award winner) so he could hit on my girlfriend. We're not sure, but it sounded like he kept calling her "peanut butter" while pawing at her blouse. We left early.

My point is that just because you look up to someone doesn't mean they won't hit on your girlfriend occasionally make mistakes.

(Don't make the equivalent of a HtmlHelper.)

Related from SO: Is the word “Helper” in a class name a code smell?

  • +1 for the Rod Stewart anecdote. I must drop my Tom Jones one into an answer sometime... ;-) – Robbie Dee Dec 21 '15 at 12:21
  • Thanks for this answer! Yes, the growth in size and being harder to change/maintain made me think about refactoring this class. I've been reading a lot about how bad Helper classes are, so I've been avoiding them and try to erradicate them from my project. I do use HtmlHelper. Thanks for clarifying some things about helpers and for the Rod Stewart analogy. – Kevin Amorim Dec 21 '15 at 14:39
  • But, did you think he was sexy? C'mon, let me know... – Eric King Dec 21 '15 at 20:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.