How to stop naming Java classes with the “Manager” suffix

Everywhere you look in a Java application, there are classes named SomethingManager. The account cache has an AccountCacheManager. The application properties have an ApplicationPropertiesManager. The user transaction is completed by a UserTransactionManager.

You probably named a class this way at some point.

Please go change it.

What’s wrong with “Manager?”

Manager is a meaningless suffix. Sure, you might argue that we need a thing to manage another thing, but what do you mean when you say that? You’re giving code a manager? Is this a workplace? Do objects in your application need a boss?

How to change it?

Whenever I see “Manager” in a class name it’s almost always redundant. I saw a class that managed the life cycle of another component recently. It was predictably named with the “LifeCycleManager” suffix. For sure this is a case where we need to name the class manager, right? After all, it manages the life cycle and that’s what we actually say in the real world?

No! Wrong!

This class had initialize() and stop() methods. Let’s assume the class it “managed” was called MyComponent. It’s actually clearer and more readable to just call the “manager” class, MyComponentLifeCycle. The code where it’s “managed” becomes MyComponentLifeCycle.initialize() and MyComponentLifeCycle.stop(). I can initialize or stop the life cycle directly, and when I do, I say what I mean and I mean what I say. I don’t ask a manager to do it, I just do it.

This simple idea applies across most use cases where I’ve seen the Manager suffix on a class. Just now, I opened up a code base and found a ConnectionManager class. Some of the methods it exposes are getConnection(), configure(), and close(). Setting aside the fact that this class probably could be refactored into some other object that makes more sense, all its really doing is holding a javax.sql.DataSource and providing some utility code around it, with the ultimate goal of providing a valid java.sql.Connection to the application.

Can’t this class just be called DbConnection and change that getConnection() method (which returns a java.sql.Connection by the way) to just get() or maybe asSqlConnection()? Now instead of having a ConnectionManager.getConnection() (which is totally redundant too by the way) you instead have DbConnection.get(). Later you call DbConnection.close(). No need for a “manager.”

No Managers!

As you can see, clean code should have no managers. It’s redundant and unnecessary. It tells you nothing about what the class actually does.

My goal is manager extinction. Who’s with me?

原文链接:How to stop naming Java classes with the “Manager” suffix

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容