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?Should I generalize the methods and create a static Helper class even though I know that, for now, only one class needs it?Should I just let the class size grow? (I don't like this solution)Any better solution?
Helper methods of a particular class
c#;class design;methods;single responsibility
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?