什么时候 Java 方法名称太长?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2230871/
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
When is a Java method name too long?
提问by MexicanHacker
In the last weeks I've seen some guys using really long names for a Method or Class (50 characters), this is usually under the premise that it improves readability, my opinion is that a long name like this is an indicator that we are trying to do a lot or too much in a method class if we need such a long name, however I wanted to know what do you guys think about it.
在过去的几周里,我看到一些人为方法或类使用很长的名称(50 个字符),这通常是在提高可读性的前提下,我认为像这样的长名称表明我们是如果我们需要这么长的名字,就试图在方法类中做很多或太多的事情,但是我想知道你们对此有何看法。
An Example is:
一个例子是:
getNumberOfSkinCareEligibleItemsWithinTransaction
采纳答案by JaredPar
A name in Java, or any other language, is too long when a shorter name exists that equally conveys the behavior of the method.
在 Java 或任何其他语言中,如果存在一个较短的名称来同样传达方法的行为,那么名称就太长了。
回答by S.Lott
Context "...WithinTransaction" should be obvious. That's what object-orientation is all about.
上下文“...WithinTransaction”应该是显而易见的。这就是面向对象的全部意义所在。
The method is part of a class. If the class doesn't mean "Transaction" -- and if it doesn't save you from having to say "WithinTransaction" all the time, then you've got problems.
该方法是类的一部分。如果该类的意思不是“事务”——并且它不能使您免于一直说“WithinTransaction”,那么您就遇到了问题。
回答by Rex Kerr
My rule is as follows: if a name is so long that it has to appear on a line of its own, then it is too long. (In practice, this means I'm rarely above 20 characters.)
我的规则如下:如果一个名字太长以至于它必须单独出现在一行中,那么它就太长了。(实际上,这意味着我很少超过 20 个字符。)
This is based upon research showing that the number of visible vertical lines of code positively correlates with coding speed/effectiveness. If class/method names start significantly hurting that, they're too long.
这是基于研究表明,可见的垂直代码行数与编码速度/效率呈正相关。如果类/方法名称开始显着损害它,那么它们就太长了。
Add a comment where the method/class is declared and let the IDE take you there if you want a long description of what it's for.
在声明方法/类的位置添加注释,如果您想要详细描述它的用途,请让 IDE 带您到那里。
回答by Bill the Lizard
The length of the method itself is probably a better indicator of whether it's doing too much, and even that only gives you a rough idea. You should strive for conciseness, but descriptiveness is more important. If you can't convey the same meaning in a shorter name, then the name itself is probably okay.
方法本身的长度可能是它是否做得太多的更好指标,即使这样也只能给你一个粗略的想法。你应该力求简洁,但描述性更重要。如果您不能用较短的名称表达相同的含义,那么名称本身可能没问题。
回答by Kaz Dragon
As with any other language: when it no longer describes the single action the function performs.
与任何其他语言一样:当它不再描述函数执行的单个操作时。
回答by uckelman
An identifier name is too long when it exceeds the length your Java compiler can handle.
当标识符名称超过 Java 编译器可以处理的长度时,它就会太长。
回答by flybywire
Java has a culture of encouraging long names, perhaps because the IDEs come with good autocompletion.
Java 有一种鼓励长名称的文化,这可能是因为 IDE 具有良好的自动完成功能。
This sitesays that the longest class name in the JRE is InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState
which is 92 chars long.
这个站点说 JRE 中最长的类名InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState
是 92 个字符长。
As for longest method name I have found this one supportsDataDefinitionAndDataManipulationTransactions
, which is 52 characters.
至于最长的方法名称,我找到了这个supportsDataDefinitionAndDataManipulationTransactions
,它是 52 个字符。
回答by Mark Byers
Just for a change, a non-subjective answer: 65536 characters.
只是为了改变,一个非主观的答案:65536 个字符。
A.java:1: UTF8 representation for string "xxxxxxxxxxxxxxxxxxxx..." is too long for the constant pool
A.java:1: 字符串“xxxxxxxxxxxxxxxxxxxx...”的UTF8表示对于常量池来说太长了
;-)
;-)
回答by flybywire
Some techniques for reducing the length of method names:
一些减少方法名称长度的技术:
If your whole program, or class, or module is about 'skin care items' you can drop skin care. For example, if your class is called
SkinCareUtils
, that brings you togetNumberOfEligibleItemsWithinTransaction
You can change withinto in,
getNumberOfEligibleItemsInTransaction
You can change Transaction to Tx, which gets you to
getNumberOfEligibleItemsInTx
.Or if the method accepts a param of type
Transaction
you can drop the InTx altogether:getNumberOfEligibleItems
You change numberOf by count:
getEligibleItemsCount
如果您的整个课程、课程或模块都是关于“护肤项目”的,您可以放弃护肤。例如,如果您的班级被称为
SkinCareUtils
,那么您将进入getNumberOfEligibleItemsWithinTransaction
您可以将内更改为in,
getNumberOfEligibleItemsInTransaction
您可以将交易更改为 Tx,这将使您获得
getNumberOfEligibleItemsInTx
.或者,如果该方法接受一个类型的参数,
Transaction
您可以完全删除 InTx:getNumberOfEligibleItems
您通过计数更改 numberOf:
getEligibleItemsCount
Now that is very reasonable. And it is 60% shorter.
现在这是非常合理的。它缩短了 60%。
回答by HackerGil
There are two ways or points of view here: One is that it really doesn't matter how long the method name is, as long as it's as descriptive as possible to describe what the method is doing (Java best practices basic rule). On the other hand, I agree with the flybywire post. We should use our intelligence to try to reduce as much as possible the method name, but without reducing it's descriptiveness. Descriptiveness is more important :)
这里有两种方式或观点:一种是方法名称有多长其实并不重要,只要能描述该方法正在做什么就可以了(Java最佳实践基本规则)。另一方面,我同意 flybywire 的帖子。我们应该用我们的智慧尽量减少方法名称,但不要减少它的描述性。描述性更重要:)