File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes helper functions good design? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "helper functions good design?" Watch "helper functions good design?" New topic

helper functions good design?

Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
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?

comp.lang.javascript FAQ:
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
I don't use them a lot. Sometimes they seem to make sense, but I always worry about the cohesion of my classes...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33130

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

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Peer Reynders

Joined: Aug 19, 2005
Posts: 2933
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).
I agree. Here's the link:
subject: helper functions good design?
It's not a secret anymore!