Utility classes are evil? [closed]

2019-01-07 04:13发布

I saw this thread

If a "Utilities" class is evil, where do I put my generic code?

and thought why are utility classes evil?

Lets say I have a domain model that is dozens of classes deep. I need to be able to xml-ify instances. Do I make a toXml method on the parent? Do I make a MyDomainXmlUtility.toXml helper class? This is a case where the business need spans the entire domain model -- does it really belong as an instance method? What about if there are a bunch of auxiliary methods on the xml functionality of the application?

14条回答
疯言疯语
2楼-- · 2019-01-07 04:33

Utility classes aren't exactly evil, but they can violate the principles that compose a good object-oriented design. In a good object-oriented design, most classes should represent a single thing and all of its attributes and operations. If you are operating on a thing, that method should probably be a member of that thing.

However, there are times when you can use utility classes to group a number of methods together — an example being the java.util.Collections class which provides a number of utilities that can be used on any Java Collection. These aren't specific to one particular type of Collection, but instead implement algorithms that can be used on any Collection.

Really, what you need to do is think about your design and determine where it makes the most sense to put the methods. Usually, it's as operations inside of a class. However, sometimes, it is indeed as a utility class. When you do use a utility class, however, don't just throw random methods into it, instead, organize the methods by purpose and functionality.

查看更多
放我归山
3楼-- · 2019-01-07 04:34

With Java 8 you can use static methods in interfaces ... problem solved.

查看更多
Emotional °昔
4楼-- · 2019-01-07 04:38

Utility classes containing stateless static methods can be useful. These are often very easy to unit test.

查看更多
beautiful°
5楼-- · 2019-01-07 04:38

Looking back at this question now, I'd say that C# extension methods completely destroy the need for utility classes. But not all languages have such an in-genius construct.

You also have JavaScript, where you can just add a new function right to the existing object.

But I'm not sure there really is an elegant way to solve this problem in an older language like C++...

Good OO code is kinda hard to write, and is hard to find since writing Good OO requires a lot more time/knowledge than writing decent functional code.

And when you're on a budget, your boss isn't always happy to see you've spent the whole day writing a bunch of classes...

查看更多
女痞
6楼-- · 2019-01-07 04:41

Utility classes are problematic because they fail to group responsibilities with the data that supports them.

They are however extremely useful and I build them all the time as either permanent structures or as stepping stones during a more thorough refactor.

From a Clean Code perspective utility classes violate the Single Responsibility and the Open-Closed Principle. They have lots of reasons to change and are by design not extensible. They really should only exist during refactoring as intermediate cruft.

查看更多
别忘想泡老子
7楼-- · 2019-01-07 04:43

Most Util classes are bad because:

  1. They broaden the scope of methods. They make code public that would otherwise be private. If the util method is stable (i.e. doesn't need updating) it's better in my opinion to copy and paste private helper methods than to expose it as an API and make it harder to understand what the public entry point to a component is (you maintain a tree structured called hierarchy with one parent per method that is easier to mentally segregate into components which is harder when you have methods called from multiple parent methods).
  2. They result in dead code. Util methods over time become unused as your app evolves and you end up with unused code polluting your code base. If it had remained private your compiler would tell you the method is unused and you could just remove it (the best code is no code at all).

There are some analogies to static vs dynamic libraries.

查看更多
登录 后发表回答