Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good to have "Utils" class in your software project? [closed]

Tags:

java

Usually, during software development, there are all sorts of utility functions I need. Like zipped file, extract zip file, launching Web Browser, get scaled image...

What I did is, I place all this utility functions as static function within a single class named "Utils"

https://github.com/yccheok/jstock/blob/master/src/org/yccheok/jstock/gui/Utils.java

Is it a good practice? Will things grow unmanageable when the number of functions grow larger and larger?

like image 696
Cheok Yan Cheng Avatar asked Jul 22 '10 03:07

Cheok Yan Cheng


People also ask

When should I use util classes?

When to create a utils class? Whenever a common block of code needs to be used from multiple places, we can create utils class. Example: I want to verify whether a text is null or empty.

What is the purpose of utils?

Utility function measures consumers' preferences for a set of goods and services. Utility is measured in units called utils—the Spanish word for useful— but calculating the benefit or satisfaction that consumers receive is abstract and difficult to pinpoint.

Are utility classes bad Java?

Well, utility classes are in fact bad. And conclusion of all discussion is that utility classes are not real objects, and as such they don't have a place in Object Oriented world.

What is software development utils?

In computers, a utility is a small program that provides an addition to the capabilities provided by the operating system. In some usages, a utility is a special and nonessential part of the operating system. The print "utility" that comes with the operating system is an example.


2 Answers

Its absolutely a best practice! you don't want to mix all those utility functions with the rest of your application business logic. However, as your utils files and/or classes grow it is recommended to group them according to the function they provide.

For example, in a web application you could end up with a package structure like this.

org.sample.web.model org.sample.web.utils org.sample.web.validators org.sample.web.validators.utils 
like image 78
Jose Diaz Avatar answered Oct 20 '22 01:10

Jose Diaz


Yes, utility classes are a good idea but, as with all object-oriented programming, you should be aiming for maximum cohesion, minimal coupling.

Maximum cohesion means that everything in a single class should be heavily related to each other. Minimal coupling means there should be no unnecessary dependencies between classes.

In other words, lumping together compression with image manipulation or the launching of external processes in a single class is a bad idea. By all means have a compression utility class and an image manipulation utility class but don't put them together.

Doing so is akin to using the singleton pattern as a god object, a ghetto where you just dump all your rubbish that should be better organised. I would say it's okay to use an uber-utility class during development but make sure your code is better organised before shipping. Maintenance will be a lot easier.

Is it a good practice?

No, not in the long term although it's useful when done temporarily.

Will things grow unmanageable when the number of functions grow larger and larger?

Yes, no question about it.

like image 27
paxdiablo Avatar answered Oct 20 '22 02:10

paxdiablo