-1

Assume you have a Writer class that writes obj to fileName. Would you rather do

new Writer(fileName, obj).Write();

or

Writer.Write(fileName, obj);

?

In case Writer is divided into sub methods you don't have to pass obj to every new method in the first case. The second approach is probably faster and doesn't instantiate a new object.

Marcel Bonzelet
  • 238
  • 2
  • 13
  • 1
    I prefer the `Static` approach – Samy S.Rathore Feb 01 '16 at 09:45
  • 2
    @SonerGönül what he has written is valid too :confused: – Yaroslav Feb 01 '16 at 09:47
  • @SonerGönül You only added brackets around the new call which already returns the object, right? – Marcel Bonzelet Feb 01 '16 at 09:55
  • @M.kazemAkhgary I was asking if there are any more details I don't see. Maybe there is a convention I don't know. – Marcel Bonzelet Feb 01 '16 at 09:56
  • @MarcelBonzelet He just thought the parentheses were needed, but they are not. – Dennis_E Feb 01 '16 at 09:57
  • in .Net take a look at StreamWriter vs static methods like File.WriteAllText or File.WriteAllLines etc... you will see that static methods only do single job (although they use streamwriter internally) but you can do many stuff with StreamWriter it self. that can be much more than single command – M.kazem Akhgary Feb 01 '16 at 09:57
  • I would make the `Writer` derive from `IWriter` and instantiate it. This subsequently makes it easy to substitute a different implementation with minimal fuss. Statics are a pain in comparison. – spender Feb 01 '16 at 10:30
  • Why would you say "The second approach is probably faster and doesn't instantiate a new object"? Both of those assumptions are probably wrong. – Enigmativity Feb 01 '16 at 11:05
  • Why would someone disagree without explaining himself? :) – Marcel Bonzelet Feb 01 '16 at 12:14

2 Answers2

2

This will basically come down to whether Writer is stateful or stateless. i.e. when you call Write, if some internal variable changes then ideally you'd want to create an instance of Writer

If the Write method is entirely self-contained then its perfectly valid to have it as a static method of a Writer class.

However, if the target was better defined, i.e. Write(FileStream file, Foo object) then you could choose to make an extension method rather than an entire static Writer class.

Meirion Hughes
  • 24,994
  • 12
  • 71
  • 122
  • Good point mentioning extension methods. In my case `Writer` takes multiple objects. I know basically when to make a method static and I agree that it should be static as it doesn't have a state. Still for more complex setups I do see the point that sub methods don't have to take all parameters over and over again. – Marcel Bonzelet Feb 01 '16 at 10:05
-1

Consider facts as :

  • Testing is important to you ?
  • Would be a problem if the Writer stays in memory (statics variables are not cleaned from memory)
  • You want a software thread-safe? (they will share the same instance, so you could get exceptions/problems)

This question could help you in more depth.

Why are static variables considered evil?

Also to decide if you should make it static :

Java: when to use static methods

Community
  • 1
  • 1
X.Otano
  • 2,079
  • 1
  • 22
  • 40