简体   繁体   English

Java 中实用程序类的命名约定

[英]Naming convention for utility classes in Java

When writing utility classes in Java, what are some good guidelines to follow?在用 Java 编写实用程序类时,要遵循哪些好的指导原则?

Should packges be "util" or "utils"?包应该是“util”还是“utils”? Is it ClassUtil or ClassUtils?是 ClassUtil 还是 ClassUtils? When is a class a "Helper" or a "Utility"?什么时候一个类是“Helper”或“Utility”? Utility or Utilities?公用事业还是公用事业? Or do you use a mixture of them?或者你使用它们的混合物?

The standard Java library uses both Utils and Utilities:标准 Java 库同时使用 Utils 和 Utilities:

  • javax.swing.Utilities javax.swing.Utilities
  • javax.print.attribute.AttributeSetUtilities javax.print.attribute.AttributeSetUtilities
  • javax.swing.plaf.basic.BasicGraphicsUtils javax.swing.plaf.basic.BasicGraphicsUtils

Apache uses a variety of Util and Utils, although mostly Utils: Apache 使用了多种 Util 和 Utils,虽然主要是 Utils:

  • org.apache.commons.modeler.util.DomUtil org.apache.commons.modeler.util.DomUtil
  • org.apache.commons.modeler.util.IntrospectionUtils org.apache.commons.modeler.util.IntrospectionUtils
  • org.apache.commons.io.FileSystemUtils org.apache.commons.io.FileSystemUtils
  • org.apache.lucene.wordnet.AnalyzerUtil org.apache.lucene.wordnet.AnalyzerUtil
  • org.apache.lucene.util.ArrayUtil org.apache.lucene.util.ArrayUtil
  • org.apache.lucene.xmlparser.DOMUtils org.apache.lucene.xmlparser.DOMUtils

Spring uses a lot of Helper and Utils classes: Spring 使用了很多 Helper 和 Utils 类:

  • org.springframework.web.util.UrlPathHelper org.springframework.web.util.UrlPathHelper
  • org.springframework.core.ReflectiveVisitorHelper org.springframework.core.ReflectiveVisitorHelper
  • org.springframework.core.NestedExceptionUtils org.springframework.core.NestedExceptionUtils
  • org.springframework.util.NumberUtils org.springframework.util.NumberUtils

So, how do you name your utility classes?那么,您如何命名您的实用程序类?

Like many such conventions, what's important is not so much what convention you use, as that you use it consistently.像许多这样的约定一样,重要的不是您使用什么约定,而是您始终如一地使用它。 Like, if you have three utility classes and you call them CustomerUtil, ProductUtils, and StoreUtility, other people trying to use your classes are going to constantly get confused and type CustomerUtils by mistake, have to look it up, curse you a few times, etc. (I heard a lecture on consistency once where the speaker put up a slide showing an outline of his speech with three main points, labeled "1", "2nd", and "C".)就像,如果您有三个实用程序类并且您将它们称为 CustomerUtil、ProductUtils 和 StoreUtility,那么其他尝试使用您的类的人会不断感到困惑并错误地键入 CustomerUtils,必须查找它,诅咒您几次,等等(我听过一次关于一致性的讲座,演讲者放了一张幻灯片,显示了他的演讲大纲,其中包含三个要点,标记为“1”、“2nd”和“C”。)

Never ever ever make two names that differ only in some subtlety of spelling, like having a CustomerUtil and a CustomerUtility.永远不要让两个名字只在一些拼写上有所不同,比如有一个 CustomerUtil 和一个 CustomerUtility。 If there was a good reason to make two classes, then there must be something different about them, and the name should at least give us a clue what that difference is.如果有一个很好的理由来创建两个类,那么它们一定有什么不同,名称至少应该让我们知道区别是什么。 If one contains utility functions related to name and address and the other contains utility functions related to orders, then call them CustomerNameAndAddressUtil and CustomerOrderUtil or some such.如果一个包含与名称和地址相关的实用函数,而另一个包含与订单相关的实用函数,则将它们称为 CustomerNameAndAddressUtil 和 CustomerOrderUtil 或诸如此类。 I regularly go nuts when I see meaningless subtle differences in names.当我看到名称中毫无意义的细微差异时,我经常发疯。 Like just yesterday I was working on a program that had three fields for freight costs, named "freight", "freightcost", and "frght".就像就在昨天,我正在开发一个程序,该程序具有三个货运成本字段,名为“freight”、“freightcost”和“frght”。 I had to study the code to figure out what the difference between them was.我必须研究代码才能弄清楚它们之间的区别是什么。

There is no standard rule/convention in Java world for this. Java 世界中没有标准规则/约定。 However, I prefer adding "s" at the end of Class name as @colinD has mentioned.但是,我更喜欢在@colinD 提到的类名末尾添加“s”。

That seems pretty standard to what Master java API Designer Josh Bloch does ( java collection as well as google collection)这对于Master java API Designer Josh Bloch 所做的事情来说似乎非常标准(java 集合以及谷歌集合)

As long as Helper and Util goes, I will call something a Helper when it has APIs that help to achieve a specific functionality of a package ( considering a package as to implement a module);只要 Helper 和 Util 存在,当它具有有助于实现包的特定功能的 API 时,我就会将其称为 Helper(将包视为实现模块); mean while a Util may be called in any context.意思是可以在任何上下文中调用 Util。

For example in an application related to bank accounts, all number specific utility static APIs would go to org.mycompany.util.Numbers例如,在与银行账户相关的应用程序中,所有特定于数字的实用程序静态 API 都将转到org.mycompany.util.Numbers

All "Account" specific business rule helping APIs would go to所有“帐户”特定业务规则帮助 API 都将转到

org.mycompany.account.AccountHelper

After all, it is a matter of providing better documentation and cleaner code.毕竟,这是提供更好的文档和更清晰的代码的问题。

I like the convention of just adding "s" to the type name when the type is an interface or a class you don't control.当类型是接口或您无法控制的类时,我喜欢在类型名称中添加“s”的约定。 Examples of that in the JDK include Collections and Executors . JDK 中的示例包括CollectionsExecutors It's also the convention used in Google Collections.这也是 Google Collections 中使用的约定。

When you're dealing with a class you have control over, I'd say utility methods belong on the class itself generally.当你处理一个你可以控制的类时,我会说实用方法通常属于类本身。

I think that 'utils' should be the package name.我认为“utils”应该是包名。 The class names should specify the purpose of the logic inside it.类名应该指定其中逻辑的目的。 Adding the sufix -util(s) is redundant.添加后缀 -util(s) 是多余的。

I'm pretty sure the words "helpers" and "utilities" are used interchangeably.我很确定“助手”和“实用程序”这两个词可以互换使用。 Anyway judging by the examples you provided, I'd say if your classname is an abbreviation (or has abbreviations in it like "DomUtil") then call your package "whatever.WhateverUtil" (or Utils if there's more than one utility in your package).无论如何,从您提供的示例来看,我会说如果您的类名是缩写(或其中有缩写,如“DomUtil”),则将您的包称为“whatever.WhateverUtil”(或 Utils,如果您的包中有多个实用程序) )。 Else if it has a full name instead of an abbreviation, then call it "whatever.WhateverUtilities".否则,如果它有全名而不是缩写,则称其为“whatever.WhateverUtilities”。

It's really up to you, but so long as coders know what you're talking about, you're good-to-go.这真的取决于你,但只要编码人员知道你在说什么,你就可以开始了。 If you're doing this professionally as a job for somebody though, ask them what their coding standards are before taking my advice.但是,如果您是专业地为某人做这项工作,请在接受我的建议之前询问他们的编码标准是什么。 Always go with the shop standards no matter what as that will help you keep your job.无论如何,始终遵循商店标准,因为这将有助于您保住工作。 :-) :-)

Here is what I do:这是我所做的:

I consider utility classes to be a code smell, but sometimes they are required to reuse methods that do not fit in any other class.我认为实用程序类是一种代码异味,但有时需要它们重用不适合任何其他类的方法。

I prefer filename with Utils suffix as it includes various functions for multiple use-cases, I never create a package named utils;我更喜欢带有Utils后缀的文件名,因为它包含用于多个用例的各种功能,我从不创建名为 utils 的包; instead, I place my utils file within the most suitable existing package for it.相反,我将我的 utils 文件放在最适合它的现有包中。

For example, if I need utils for a feature, then I place the MyFeatureUtils.java file within the com.myapp.myfeature package.例如,如果我需要一个功能的 utils,那么我将MyFeatureUtils.java文件放在com.myapp.myfeature包中。

If I don't have an appropriate package for the utils file or need to access it from multiple modules then I place it in the root or in common package.如果我没有适合 utils 文件的包或需要从多个模块访问它,那么我将它放在rootcommon包中。

If there is an existing utils package in the project, only then I place the utils class in it.如果项目中有一个现有的utils包,那么我才将 utils 类放入其中。

Some more examples:还有一些例子:

  • com.myapp.notification.NotificationUtils.java com.myapp.notification.NotificationUtils.java
  • com.myapp.account.AccountUtils.java com.myapp.account.AccountUtils.java
  • com.myapp.data.DatabaseUtils.java com.myapp.data.DatabaseUtils.java
  • com.myapp.view.ImageUtils.java com.myapp.view.ImageUtils.java
  • com.myapp.analytics.LogUtils.java com.myapp.analytics.LogUtils.java
  • src/routes/route-utils.js src/routes/route-utils.js
  • src/middleware/auth-utils.js src/中间件/auth-utils.js

PS: I do not use Helper instead of Utils, because helpers can be stateful and generally used within an instance for delegating some states and tasks; PS:我不使用Helper代替Utils,因为Helper可以是有状态的,一般在实例中用于委托一些状态和任务; whereas utils are completely stateless and should include only static functions.而 utils 是完全无状态的,应该只包含静态函数。

PS: I do not use Manager instead of Utils, because managers can be stateful and used to provide, manage, and store one or more instances. PS:我不使用 Manager 而不是 Utils,因为管理器可以是有状态的,用于提供、管理和存储一个或多个实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM