I have to admit, I have utility/helper classes in my code. Yes I do. And they are static. Does that make my code smelly? I have a feeling that it does. For a while I was able to convince myself that it was OK, but I really dearly want to be a totally awesome programmer,- so I’ve started questioning everything I do and lay bare my thinking and my code, so I would get input, good and bad, that would lead to better reasoning and better practices.
For this question I have no answer, but I can explain my thinking. Here is how I’ve been thinking untill now:
I use extension methods when it feels like it would be a nice fit, and I know I will need them a fair bit. When I have pure function based methods that can be grouped together, and they just take parameters and spit out a result, and no state is involved, AND an extension method is not suitable, then I’ll make the class and methods static. Please note that I never have a state associate or dependencies associate with them.
So my answer has been: In moderation and if well thought through, no state and no dependencies, and doesnt fit as an extension method, and the methods can be grouped together (not a fix-all utility class), then it would not necessarily be a code smell.
My new thinking: The more I think about it, the more I’m uncomfortable with utility classes. Looking at my cdoe many can be moved to extension methods, but not all. What should I do with my homeless but helpfulls?
Let the discussion begin! 😀