Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Utils classes: code smell / antipattern?

 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would anybody like to share their experiences with classes named ???Utils that were really not "utility" classes. The "util" classes that I'm looking at right now have a lot of business type logic and are smelling a lot like "I don't really know where to assign this responsibility so I'm just going to let the Utils class handle it."
[ June 04, 2004: Message edited by: Junilu Lacar ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, if there's business logic in there it's probably smelly. Assigning responsiblity is what OO coder/designers get paid for. Shirking the job is smelly, too.

Some people reserve the name "utility class" for a class with all static methods, like Math. Every project probably has a StringUtils class. (What does that tell us?) Other people say classes should be responsibility plus state and if your static util class holds no state it's a smelly class. I'm in the first camp, by the way.

I recently made a couple stateless classes with all static methods that simplify kinda complex remote calls. I just got sick of typing the same four or five lines all the time and made a method that does them for me. We call them "helper" or "convenience" classes rather than "utility".
[ June 06, 2004: Message edited by: Stan James ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic