Java中实用程序类的命名约定 [英] Naming convention for utility classes in Java

查看:118
本文介绍了Java中实用程序类的命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中编写实用程序类时,要遵循哪些好的指导原则?

When writing utility classes in Java, what are some good guidelines to follow?

包装应该是util还是utils?是ClassUtil还是ClassUtils?什么时候是帮助者或实用程序?公用事业或公用事业?或者你是混合使用它们吗?

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?

标准Java库同时使用Utils和Utilities:

The standard Java library uses both Utils and Utilities:


  • javax.swing.Utilities

  • javax.print.attribute.AttributeSetUtilities

  • javax.swing.plaf.basic.BasicGraphicsUtils

Apache使用各种Util和Utils,但主要是Utils:

Apache uses a variety of Util and Utils, although mostly 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使用了大量的Helper和实用工具类:

Spring uses a lot of Helper and Utils classes:


  • org.springframework.web.util.UrlPathHelper

  • org.springframework.core .ReflectiveVisitorHelper

  • org.springframework.core.NestedExceptionUtils

  • org.spring framework.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?

推荐答案

像许多这样的约定一样,重要的不是你使用的约定,而是你一贯使用它。就像,如果你有三个实用程序类并且你称它们为CustomerUtil,ProductUtils和StoreUtility,其他试图使用你的类的人会不断地混淆并错误地输入CustomerUtils,必须查找它,诅咒你几次, (我听过一次演讲的一致性演讲,演讲者提出一张幻灯片,上面显示了他的演讲大纲,标有1,2和C三个要点。)

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和CustomerUtility。如果有充分的理由做两个课程,那么他们必须有一些不同的东西,这个名字至少应该让我们知道这个差异是什么。如果一个包含与名称和地址相关的实用程序函数,另一个包含与订单相关的实用程序函数,则将它们称为CustomerNameAndAddressUtil和CustomerOrderUtil或其他类似函数。当我看到名字中无意义的细微差别时,我经常发疯。就像昨天一样,我正在制定一个有三个运费成本领域的计划,名为货运,货运成本和费用。我不得不研究代码来弄清楚它们之间的区别。

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.

这篇关于Java中实用程序类的命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆