Note: this topic is a bit more advanced as it discusses concepts of software design, if you would like to continue the tutorials, please proceed to the next one.
This article reflexes part of my own opinion on the subject and might not align with your thoughts, if so, please start the discussion!
I have seen quite a few examples of utility classes in the last few years and I must say that I didn’t find any that was worth keeping.
What Are Utility Classes
Utility classes seem to be used to regroup methods and sometimes properties that will be used throughout the program but that don’t, at first glance, fit anywhere in particular. In some cases the programmer will be lazy and dump whatever method that looks global in this particular class.
Also, a utility class is most of the time static because programmers want to have access to it anywhere and, again in most cases, nothing links methods with one another. If you want a more complex definition, you can refer to Wikipedia.
Why They Are Not a Good Practice
I would consider utility classes as a bad practice because they tend, after a while, to contain a lot of junk and are rarely reorganized and maintained. Why? Because people dump every method they don’t know where to put and don’t bother trying to create more classes or reorganize the utility methods after a while.
As an example, I worked on an already started application a few weeks ago in which I found one of these classes. I immediately decided to look for a possibility of reorganization and after a bit of work I managed to create one new class and put the other methods in already created classes, way better!
It may not be that the other programmer was not good or anything, but he might have forgotten to revamp the utility class and just continued adding more and more methods to it.
Last Word on the Subject
In my opinion, there is always a better way to name or organize things than to create a utility class that will soon become a mess or simply a dump where everyone put methods or global variables they don’t know where to put at first glance.
As a last example, I happened to work on a project a few years ago where the utility class was actually the database layer interface! I found that pretty funny!
Feel free to share your opinion on utility classes in the comments.