• Post Reply Bookmark Topic Watch Topic
  • New Topic

Helper functions  RSS feed

 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any preference (or performance issue) whether "Helper functions" should be static or not?
For example:

As you can see, the helper function does not access any instance variables.
Should methods like this be static?
Thanks in advance,
Leslie
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Helper or Utility functions should probably always be static. There are performance issues involved because static members cannot be polymorphic so a virtual call does not have to be made on a static method nor does there have to be an instance of the owner class to call the method, everthing is resolved at compile time.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, making the method static reduces the flexibility of your design as static methods cannot be overriden. So there's a trade-off.
D.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Don Kiddick:
Also, making the method static reduces the flexibility of your design as static methods cannot be overriden. So there's a trade-off.
D.

That's agreed but the question was involving utility methods. My own personal preference is to never use static methods in a class with instance methods but place them in a utility class with a private constructor like the Math class. If you have a utility method that is not dependent on a particular instance you may as well place it in a utility class that you may be able to reuse later.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must ask yourself why you didn't write the method as static in the first place. Do you have a design issue ? Were you assuming the method was going to read/write the object's state, but changed half way through ?
If the method does not access any of the object's data, it probably should be static, but I would be asking myself several questions first. To naively add the static modifier to a method, because it looks more correct, seems very odd.
I will be proposing a feature request for the 1.6 compiler to fail if the developer has not "designed before implemented", since I see it all too often.
X.java:1: implementation performed before design - 'thinking' expected.

Also, passing java.util.ArrayList is poor form (in fact, having a reference of that type is poor form). Use at least (class hierarchy abstraction) a java.util.List instead.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Morris:

My own personal preference is to never use static methods in a class with instance methods but place them in a utility class with a private constructor like the Math class.

There are lots of instances where you would want to have static methods in a class with instance methods. e.g. a factory method, typesafe enumeration.
If you have a utility method that is not dependent on a particular instance you may as well place it in a utility class that you may be able to reuse later.

I don't see how making the method static makes the class any more reusable though ( as opposed to a utility class with instance methods). I think it makes it less reuable as it's less flexible because the method cannot be overriden.
D.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:
If the method does not access any of the object's data, it probably should be static

I used to think this way - if a method does not access any state, make the method static. Then I worked on a project where I really wanted to override a method in this 3rd party library, but the writer had made the method static because of the above reasons. He gained nothing from doing that, he wasn't accessing the method from a static context and he lost the flexibility I required.
I now only make methods static if there is a good reason too.
D.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you're right.
Rephrase to:
"If the method does not access any of the object's data and the enclosing class is not designed for inheritance (and thus should be declared final), it probably should be static"
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!