java中变量名或类名前的_(下划线)是否有标准?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1899683/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:39:52  来源:igfitidea点击:

Is there a standard in java for _ (underscore) in front of variable or class names?

javacoding-style

提问by Ashok Koyi

I have seen some programmers using _(underscore) in front of class names, and others using it for local variables.

我见过一些程序员_在类名前使用(下划线),而其他人将它用于局部变量。

Does the Java standard require/suggest the use of _ (underscore) in front of an private instance variable or class name?

Java 标准是否要求/建议在私有实例变量或类名前使用 _(下划线)?

采纳答案by Thorbj?rn Ravn Andersen

It is a matter of personal taste.

这是个人品味的问题。

Martin Fowler appears to like it. I don't care much for it - it breaks the reading pattern, and the information it conveys is already subtly told by color in your IDE.

马丁福勒似乎喜欢它。我不太在意它——它打破了阅读模式,它传达的信息已经在你的 IDE 中通过颜色巧妙地传达出来。

I've experimented with using _as the name of the variable holding the result to be returned by a method. It is very concise, but I've ended up thinking that the name resultis better.

我已经尝试使用_作为保存方法返回结果的变量的名称。它非常简洁,但我最终认为这个名字result更好。

So, get your colleagues to agree on what you all like the most and then just do that.

所以,让你的同事就你们最喜欢的东西达成一致,然后就这样做。

回答by Chii

if you want to follow best practice java , use the code convention outlined here http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

如果您想遵循最佳实践 java ,请使用此处概述的代码约定http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

I.e., no underscore for private member variable names.

即,私有成员变量名称没有下划线。

Tho, personally, i m not fussed either way, as long as consistency is applied.

Tho,就个人而言,只要应用一致性,无论哪种方式,我都不会大惊小怪。

回答by Francis Upton IV

Many people (including myself) use _ in front of field names. The reason for this is to easily distinguish them from local variables. However in the ages of IDEs this is not so necessary since syntax highlighting shows this. Using an underscore in front of a class name is just wrong. By convention, class names start with an uppercase letter.

许多人(包括我自己)在字段名称前使用 _。这样做的原因是很容易将它们与局部变量区分开来。然而,在 IDE 时代,这并不是那么必要,因为语法高亮显示了这一点。在类名前使用下划线是错误的。按照惯例,类名以大写字母开头。

回答by TofuBeer

A lot of people is it for instance variables as it makes them stand out. Personally I hate it mainly because methods should, for the most part, be no more than 10 lines of code (I hardly ever go over 20). If you cannot see that a variable isn't local in under 10 lines then there is something wrong :-)

很多人都喜欢它,例如变量,因为它使它们脱颖而出。我个人讨厌它,主要是因为方法在大多数情况下应该不超过 10 行代码(我几乎不会超过 20 行)。如果您看不到 10 行以内的变量不是本地变量,则说明有问题:-)

If you have 100 line methods and you don't declare all your variables at the top of the block that they are used in I can see why you would want to use _ to distinguish them... of course, as with most things, if it hurts then stop doing it!

如果您有 100 行方法并且您没有在使用它们的块的顶部声明所有变量,我可以理解您为什么要使用 _ 来区分它们……当然,就像大多数事情一样,如果疼就别再做了!

回答by duffymo

I think artifacts like these are pre-IDE and C++ vintage. Don't do it.

我认为像这样的工件是 IDE 和 C++ 之前的。不要这样做。

回答by PaulP1975

I agree with the others here... It breaks the standard and doesn't really buy you anything if you just use a naming standard. If you need to know the difference between your class and local variables, try a naming convention like lVariableName where the l is used to indicate that it is local. To me this is unnecessary since the IDE highlighting is sufficient, but might be useful to some.

我同意这里的其他人的看法...它违反了标准,如果您只使用命名标准,它并不会真正为您购买任何东西。如果您需要了解类变量和局部变量之间的区别,请尝试使用 lVariableName 之类的命名约定,其中 l 用于指示它是局部变量。对我来说这是不必要的,因为 IDE 突出显示就足够了,但可能对某些人有用。

回答by Pradeep

Using _ before a variable helps in one case where if the developer wants to use a standard/key word as a variable name knowingly or unknowingly. It would not give a conflict if that has an additional character.

在一种情况下,如果开发人员想要有意或无意地使用标准/关键字作为变量名称,在变量之前使用 _ 会有所帮助。如果它有一个额外的字符,它不会产生冲突。

回答by imdiva

It seems to be a convention that states if an underscore is used as an initial character in the name of method, the method cannot be overridden.

这似乎是一个约定,规定如果在方法名称中使用下划线作为初始字符,则该方法不能被覆盖。

For example, in context of JSP, _jspService() method cannot be overridden.

例如,在 JSP 上下文中,_jspService() 方法不能被覆盖。

However, i dont find any document that states above convention.

但是,我没有找到任何说明上述惯例的文件。

回答by etoricky

  1. Do not use _ whenyou use a modern LCD color monitor, with modern IDE like Eclipse, Netbeans, IntelliJ, Android Studio.
  2. Use _ whenyou need to use a old-style monochrome CRT monitor with only green or white text, or when you have difficulty to identify the color on LCD monitor.
  3. The consensus of your code collaborators overrides the above.
  1. 使用现代 LCD 彩色显示器时不要使用 _,以及现代 IDE,如 Eclipse、Netbeans、IntelliJ、Android Studio。
  2. 您需要使用只有绿色或白色文本的旧式单色 CRT 显示器,或者当您难以识别 LCD 显示器上的颜色时,使用 _
  3. 您的代码合作者的共识覆盖了上述内容。