views:

121

answers:

2

I really like to use static methods (especially for helpers classes). But as static methods are not stubbable, eventually they are a bad practice, aren't they? So I have to choose between static methods usage convenience and testability. Is there any compromise?

+6  A: 

It depends on what the static methods do.

Is it something that really has to be stubable? Like data access, long running operations etc. or is do you mean helper methods like .ToSlug()?

If it's the former case, I'd make them instance methods to increase testability / speed of tests.

If it's the latter, I'd leave them static and just verify their correctness.

Maxem
Agreed. this question is asked many times before by the way.for example you may also want to read: http://stackoverflow.com/questions/731763/should-c-methods-that-can-be-static-be-statichttp://stackoverflow.com/questions/1184701/static-vs-non-static-method
SirLenz0rlot
+1  A: 

If you can guarentee that the static method always returns the same result for a given input and the only reason they are there is helper methods (converting or transforming input -> output without sideeffects) I don't see a problem. For extra kicks, you could make them extension methods (as of c#3.0).

Femaref