Java 中实用程序类的命名约定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2676704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Naming convention for utility classes in Java
提问by JR.
When writing utility classes in Java, what are some good guidelines to follow?
在用 Java 编写实用程序类时,要遵循哪些好的指导原则?
Should packges be "util" or "utils"? Is it ClassUtil or ClassUtils? When is a class a "Helper" or a "Utility"? Utility or Utilities? Or do you use a mixture of them?
包应该是“util”还是“utils”?是 ClassUtil 还是 ClassUtils?什么时候类是“助手”或“实用程序”?公用事业还是公用事业?或者你使用它们的混合物?
The standard Java library uses both Utils and Utilities:
标准 Java 库同时使用 Utils 和 Utilities:
- javax.swing.Utilities
- javax.print.attribute.AttributeSetUtilities
- javax.swing.plaf.basic.BasicGraphicsUtils
- javax.swing.Utilities
- javax.print.attribute.AttributeSetUtilities
- 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.IntrospectionUtils
- org.apache.commons.io.FileSystemUtils
- org.apache.lucene.wordnet.AnalyzerUtil
- org.apache.lucene.util.ArrayUtil
- org.apache.lucene.xmlparser.DOMUtils
- org.apache.commons.modeler.util.DomUtil
- org.apache.commons.modeler.util.IntrospectionUtils
- org.apache.commons.io.FileSystemUtils
- org.apache.lucene.wordnet.AnalyzerUtil
- org.apache.lucene.util.ArrayUtil
- org.apache.lucene.xmlparser.DOMUtils
Spring uses a lot of Helper and Utils classes:
Spring 使用了很多 Helper 和 Utils 类:
- org.springframework.web.util.UrlPathHelper
- org.springframework.core.ReflectiveVisitorHelper
- org.springframework.core.NestedExceptionUtils
- org.springframework.util.NumberUtils
- org.springframework.web.util.UrlPathHelper
- org.springframework.core.ReflectiveVisitorHelper
- org.springframework.core.NestedExceptionUtils
- org.springframework.util.NumberUtils
So, how do you name your utility classes?
那么,您如何命名您的实用程序类?
采纳答案by Jay
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. 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. 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". I had to study the code to figure out what the difference between them was.
永远不要让两个名字只在一些拼写上有所不同,比如有一个 CustomerUtil 和一个 CustomerUtility。如果有一个很好的理由来创建两个类,那么它们一定有什么不同,名称至少应该让我们知道区别是什么。如果一个包含与名称和地址相关的实用函数,而另一个包含与订单相关的实用函数,则将它们称为 CustomerNameAndAddressUtil 和 CustomerOrderUtil 或诸如此类。当我看到名称中毫无意义的细微差异时,我经常发疯。就像就在昨天,我正在开发一个程序,该程序具有三个货运成本字段,名为“freight”、“freightcost”和“frght”。我必须研究代码才能弄清楚它们之间的区别是什么。
回答by Aito
I think that 'utils' should be the package name. The class names should specify the purpose of the logic inside it. Adding the sufix -util(s) is redundant.
我认为“utils”应该是包名。类名应该指定其中逻辑的目的。添加后缀 -util(s) 是多余的。
回答by KSwift87
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). Else if it has a full name instead of an abbreviation, then call it "whatever.WhateverUtilities".
我很确定“助手”和“实用程序”这两个词可以互换使用。无论如何,从您提供的示例来看,我会说如果您的类名是缩写(或其中有缩写,如“DomUtil”),则将您的包称为“whatever.WhateverUtil”(或 Utils,如果您的包中有多个实用程序) )。否则,如果它有全名而不是缩写,则称其为“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. :-)
这真的取决于你,但只要编码人员知道你在说什么,你就可以开始了。但是,如果您是专业地为某人做这项工作,请在接受我的建议之前询问他们的编码标准是什么。无论如何,始终遵循商店标准,因为这将有助于您保住工作。:-)
回答by ColinD
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. Examples of that in the JDK include Collections
and Executors
. It's also the convention used in Google Collections.
当类型是接口或您无法控制的类时,我喜欢在类型名称中添加“s”的约定。JDK 中的示例包括Collections
和Executors
。这也是 Google Collections 中使用的约定。
When you're dealing with a classyou have control over, I'd say utility methods belong on the class itself generally.
当你处理一个你可以控制的类时,我会说实用方法通常属于类本身。
回答by ring bearer
There is no standard rule/convention in Java world for this. However, I prefer adding "s" at the end of Class name as @colinD has mentioned.
Java 世界中没有标准规则/约定。但是,我更喜欢在@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); mean while a Util may be called in any context.
只要 Helper 和 Util 存在,当它具有有助于实现包的特定功能的 API 时,我就会将其称为 Helper(将包视为实现模块);意思是可以在任何上下文中调用 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.
毕竟,这是提供更好的文档和更清晰的代码的问题。