2

I have a helper class that takes some object, processes it and returns back some instance of the other class or even the List of the objects. What would be the best way: to make this helper method static or non-static? The thing is that my app can create lots of the Car objects and I was thinking whether it could have a negative effect when each of them use the static helper?

J.Doe
  • 143
  • 1
  • 2
  • 5
  • Do you have some example code as will help to give the most appropriate answer to your question – Neil Stevens Feb 05 '17 at 14:26
  • 1
    @J.Doe Yup. Your car could have a flat tire. Jokes apart, can you show us some code please. – Chetan Kinger Feb 05 '17 at 14:29
  • 1
    Do you need to mock some functionalities when testing your class (the original, not the helper class)? If so, you shouldn't use static methods. – shay__ Feb 05 '17 at 14:29
  • Possible duplicate of [Java why is using static helper methods bad?](http://stackoverflow.com/questions/14387385/java-why-is-using-static-helper-methods-bad) – shay__ Feb 05 '17 at 14:33

3 Answers3

2

Probably this is something that can be solved without deciding the helper object's life-cycle where you require it.

You should try to leverage dependency injection approach:

public class X
{
   public X(IHelper helper)
   {
        Helper = helper;
   }

   private IHelper Helper { get; }

   public void DoStuff() 
   {
        var result = Helper.DoOtherStuff(input);
   }
}

That is, X don't know whether Helper is always the same instance or if it's a transient object. This makes the code cleaner and more test-friendly, because you can mock the helper with a fake IHelper implementation to be sure that you're just testing X.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • 1
    I'd change the type to `IHelper` in your example to emphasize there's no coupling between `X` and it's helper – shay__ Feb 05 '17 at 14:49
  • I'd agree to this answer. Also, looks like Java community has same discussion over [here](https://stackoverflow.com/questions/14387385/why-is-using-static-helper-methods-in-java-bad) and they've come to the same approach. – vkolesnik Jul 07 '22 at 18:31
1

Most helper or utility classes use static methods. You should only use non-static methods if you want to create multiple instances of your helper class, but since you just need a simple input -> function -> output, I would make the methods static.

Sentinent
  • 351
  • 2
  • 8
  • 1
    This is wrong. You should use static classes / methods only if there is a good reason for them to be static, not the other way around. – shay__ Feb 05 '17 at 14:34
  • 2
    @shay__ like it being a helper method that doesn't relate to any state or need to be overridden? – Jon Hanna Feb 05 '17 at 15:29
  • @shay__ Utility classes are usually static. instantiating an object for a single unrelated method is redundant. his suggestion is not wrong, you misinterpreted it. – Stavm Feb 05 '17 at 15:43
  • @Stavm the fact that people "usually" do it does not mean it's the right thing to do. `HelperClass.GetRuntimeConfigurations()` does not relate to the object's state yet it is wrong to be static, you can't (easily) mock it for tests. – shay__ Feb 06 '17 at 07:52
0

Use static class with static methods, No instance, no derivation and only static methods in the class.

public static class HelperClass
{
   public static void HelperMethod()
   {
       // do something
   } 
}