确定在 Java 中用于方法名称的动词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7151418/
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
determining which verb to use for method names in Java
提问by PengOne
I understand that naming conventions are important for a number of reasons, most having to do with making your code more readable and easier to integrate into larger projects, etc. In Java, most conventions require that method names be in lowerCamelCase
begin with a verb.
我知道命名约定很重要,原因有很多,最重要的是使您的代码更具可读性,更容易集成到更大的项目中等。在 Java 中,大多数约定要求方法名称以lowerCamelCase
动词开头。
My question is: how do I choose the verb to begin the method name?
我的问题是:如何选择动词来开始方法名称?
To make this question less vague, I'm often in the situation where my first choice for a method name is a noun describing the output. In these cases, I'm usually torn between appending generic verbs such as get
, generate
, calculate
, etc in font of the noun to conform to the verb rule. Are there general guidelines for when to use which?
为了让这个问题不那么含糊,我经常遇到这样一种情况,我对方法名称的第一选择是描述输出的名词。在这种情况下,我通常追加通用动词如之间徘徊get
,generate
,calculate
,等在名词的字体,以符合动词规则。是否有关于何时使用哪个的一般准则?
Here's an example. I have a method that takes double[] array
and an int k
and returns double[] newArray
which is the length k
moving average of array
, i.e. newArray[i] = (array[i-k+1]+...+array[i])/k
with some fudging to make newArray
the same length as array
. My inclination is to call this method movingAverage
since that's what it returns, but that's out since it doesn't begin with a verb. Should I call this method getMovingAverage
or generateMovingAverage
or calculateMovingAverage
or does it not really matter?
这是一个例子。我有一个方法,它采用double[] array
和int k
和 返回double[] newArray
,它是 的长度k
移动平均值array
,即newArray[i] = (array[i-k+1]+...+array[i])/k
通过一些捏造使newArray
长度与array
. 我的倾向是调用此方法,movingAverage
因为它返回的是它,但由于它不以动词开头,所以我不打算这样做。我应该调用这个方法,getMovingAverage
或者generateMovingAverage
,calculateMovingAverage
或者它真的不重要吗?
回答by Evan Mulawski
I usually ask myself:
我通常会问自己:
What is this method doing?
这个方法是做什么的?
The answer dictates what the method should be called. It is completely independent of the programmer, of course.
答案决定了应该调用什么方法。当然,它完全独立于程序员。
Note:If you can't succinctlydescribe what the method is doing, it's probably doing too much and should be split up.
注意:如果你不能简洁地描述方法在做什么,它可能做的太多了,应该拆分。
Choosing your method's verb:
选择方法的动词:
- Performing calculation(s): calculate
- Retrieving data: getor retrieve
- Mutating data: setor change
- Deleting data: deleteor remove
- Converting: convert
- Initiating an action: startor initiate
- Stopping an action: stopor cancel
- 执行计算:计算
- 检索数据:获取或检索
- 变异数据:设置或更改
- 删除数据:删除或移除
- 转换:转换
- 启动一个动作:开始或启动
- 停止动作:停止或取消
Now, not all methods begin with a verb; but they really don't need to. If you read:
现在,并非所有方法都以动词开头;但他们真的不需要。如果你阅读:
... myString.length();
or
或者
... myArray.size();
you know exactly what is going on - no verb required. This is true for many class methods higher up in the Java hierarchy; Collections, Math, etc. As long as the name accurately communicates what the method does, it's fine.
你确切地知道发生了什么——不需要动词。对于 Java 层次结构中更高的许多类方法来说,这是正确的。集合、数学等。只要名称准确地传达了方法的作用,就可以了。
回答by Ricardo Montesinos
Do not forget to use this verbs "is, has or can" for booleanmethods, such as: isOn(), isFull(), and so on.
不要忘记将动词“is、has 或 can”用于布尔方法,例如:isOn()、isFull() 等。
回答by Cornel Masson
Regarding the use of get
and set
methods for property accessors only: the whole point of information hidingis that the user of an API (i.e. the calling code) should not need to know or depend on whether the property is stored or calculated on the fly. The implementation should be able to change at any time, as long as the API stays the same.
关于使用get
和set
方法属性访问器只:整点信息隐藏是一个API的用户(即调用代码)不应该需要知道或取决于属性是否存储或实时计算。只要 API 保持不变,实现就应该能够随时更改。
回答by David Andersson
(My opinion) You don't need a verb in the method name if the object has no state, like a math-library. Compare computeSquareRoot(x) and getJoin(listOfStrings) with squareRoot(x) and join(listOfStrings).
(我的意见)如果对象没有状态,例如数学库,则方法名称中不需要动词。将computeSquareRoot(x) 和getJoin(listOfStrings) 与squareRoot(x) 和join(listOfStrings) 进行比较。
回答by suat
As you said and as we can see in the answers, the verbs used at the beginning of method names are almost the same verbs. I think, if same amount of effort is spent for writing related documentation, methods become much more understandable and integrable :)
正如你所说的,正如我们在答案中看到的,方法名称开头使用的动词几乎是相同的动词。我认为,如果花费相同的精力来编写相关文档,方法就会变得更加易于理解和集成:)
I also realized after reading the question, most of the methods I write start with get, retrieve, create. So again it seems verb selection does not matter so much.
阅读问题后我也意识到,我编写的大多数方法都是从get、retrieve、create 开始的。因此,似乎动词选择并没有那么重要。
Best
最好的
回答by outmind
I think to find anything which could be a "solution" for the problem we should first extract criteria which play any role in choosing name, like:
我认为要找到任何可能成为问题“解决方案”的东西,我们应该首先提取在选择名称方面发挥任何作用的标准,例如:
- readableness (trivial: getValue instead of transferValueFromThisObjectToYou)
- convenience (trivial: getCoordValue instead of )
- semantics of the method (get is not the same as calculate)
- context of usage (in IDEs I usually type aaa.get_ and press Ctrl+Space to check what I can get from the object)
- code guides (or other "guides" like Java Bean convention which force you to use some names)
- 可读性(简单:getValue 而不是 transferValueFromThisObjectToYou)
- 方便(简单:getCoordValue 而不是 )
- 方法的语义(get 与计算不同)
- 使用上下文(在 IDE 中,我通常输入 aaa.get_ 并按 Ctrl+Space 来检查我可以从对象中获得什么)
- 代码指南(或其他“指南”,如 Java Bean 约定,强制您使用某些名称)
...
...
However as suat said - better spend some efforts on a documentation for your methods.
但是,正如 suat 所说 - 最好花一些精力为您的方法编写文档。
回答by ILMTitan
I don't think java method names should "begin with a verb", I think they should describe the action. This often requires a verb, as verbs describe actions. Usually, they are important parts of the description (getVar and setVar mean totally different things). Occasionally, they add nothing to the description (can you think of anything that would operate on movingAverage besides get/calculate/generate?) and should be dropped.
我不认为 java 方法名称应该“以动词开头”,我认为它们应该描述动作。这通常需要一个动词,因为动词描述了动作。通常,它们是描述的重要部分(getVar 和 setVar 的意思完全不同)。有时,它们不会在描述中添加任何内容(除了 get/calculate/generate 之外,您还能想到对 moveAverage 进行操作的任何内容吗?)并且应该删除。