Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

helper functions good design?

 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually avoid stuff like 'utility' packages or "Helper" classes.

However, I read in Head First OOA&D that It is a good idea to start out by creating a utility package for Helper classes.

What's your take?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't use them a lot. Sometimes they seem to make sense, but I always worry about the cohesion of my classes...
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use helper classes and util packages, but I don't put them all in one util package. I do try to put logic where it belongs. But if multiple classes use the same logic, a helper class often makes sense. Or maybe I should say a helper object. Many of the helper classes don't have static methods.

For example:
mycompany.util - Generic things like StringUtil, DateUtil [this is the kitchen sink, everything goes in it package]
mycompany.component1.util - Helper classes having to do with a certain component
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether or not helper classes are a good idea depends largerly on whether they follow the object-oriented design principles and whether or not they introduce any code smells. The single responsibility principle sometimes drives the creation of helper classes as inclusion of their functionality could leave the core class with multiple responsibilties. Helper classes may also be created to stick with the DRY Principle (Do not Repeat Yourself); i.e. a piece of code that somehow seems to appear in all sorts of places needs to be consolidated in one place but doesn't have a natural home (though one should use this case very sparingly - it's more likely that you don't yet fully comprehend what's going on in your object model).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic