在 Java 变量和方法名称中使用下划线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/150192/
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
Using underscores in Java variables and method names
提问by Georgi
Even nowadays I often see underscores in Java variables and methods, an example are member variables (like "m_count" or "_count"). As far as I remember, to use underscores in these cases is called bad style by Sun.
即使现在我经常在 Java 变量和方法中看到下划线,一个例子是成员变量(如“m_count”或“_count”)。据我所知,在这些情况下使用下划线被 Sun 称为糟糕的风格。
The only place they should be used is in constants (like in "public final static int IS_OKAY = 1;"), because constants should be all upper case and not camel case. Here, the underscore should make the code more readable.
唯一应该使用它们的地方是常量(如“public final static int IS_OKAY = 1;”),因为常量应该全部大写而不是驼峰式。在这里,下划线应该使代码更具可读性。
Do you think using underscores in Java is bad style? If so (or not), why?
你认为在 Java 中使用下划线是不好的风格吗?如果是(或不是),为什么?
采纳答案by Tanktalus
If you have no code using it now, I'd suggest continuing that. If your codebase uses it, continue that.
如果您现在没有使用它的代码,我建议您继续这样做。如果您的代码库使用它,请继续。
The biggest thing about coding style is consistency. If you have nothing to be consistent with, then the language vendor's recommendations are likely a good place to start.
编码风格最重要的是一致性。如果您没有任何需要遵守的内容,那么语言供应商的建议可能是一个不错的起点。
回答by Nicholas Mancuso
using 'm_' or '_' in the front of a variable makes it easier to spot member variables in methods throughout an object.
在变量前面使用“m_”或“_”可以更容易地在整个对象的方法中发现成员变量。
As a side benefit typing 'm_' or '_' will make intellsense pop them up first ;)
作为附带好处,输入“m_”或“_”将使智能首先弹出它们;)
回答by davetron5000
Rules:
规则:
- Do what the code you are editing does
- If #1 doesn't apply, use camelCase, no underscores
- 执行您正在编辑的代码的功能
- 如果 #1 不适用,请使用驼峰式命名,无下划线
回答by Chris Cudmore
It's a blend of coding styles. One school of thought is to preface private members with an underscore to distinguish them.
它是编码风格的混合。一种思想流派是在私有成员的前面加上下划线以区分它们。
setBar( int bar)
{
_bar = bar;
}
instead of
代替
setBar( int bar)
{
this.bar = bar;
}
Others will use underscores to indicate a temp local variable that will go out of scope at the end of the method call. (I find this pretty useless - a good method shouldn't be that long, and the declaration is RIGHT THERE! so I know it goes out of scope) Edit: God forbid a programmer from this school and a programmer from the memberData school collaborate! It would be hell.
其他人将使用下划线来指示将在方法调用结束时超出范围的临时局部变量。(我觉得这很没用 - 一个好的方法不应该那么长,声明就在那里!所以我知道它超出了范围)编辑:上帝禁止这所学校的程序员和 memberData 学校的程序员合作!那将是地狱。
Sometimes, generated code will preface variables with _ or __. The idea being that no human would ever do this, so it's safe.
有时,生成的代码会以 _ 或 __ 开头变量。这个想法是没有人会这样做,所以它是安全的。
回答by Joe Ratzer
I think any style that breaks a language's own style guidelines (without due reason) is ugly and therefore "bad".
我认为任何违反语言自身风格指南(没有正当理由)的风格都是丑陋的,因此是“坏的”。
No doubt the code you've seen was written by someone who used to work on a language where underscores were acceptable.
毫无疑问,您所看到的代码是由曾经使用下划线可接受的语言编写的。
Some people just cannot adapt to new coding styles...
有些人就是无法适应新的编码风格......
回答by Christophe Herreman
"Bad style" is very subjective. If a certain conventions works for you and your team, I think that will qualify a bad/good style.
“坏风格”是非常主观的。如果某些约定适用于您和您的团队,我认为这将是一种坏/好风格。
To answer your question: I use a leading underscore to mark private variables. I find it clear and I can scan through code fast and find out what's going on.
回答您的问题:我使用前导下划线来标记私有变量。我觉得很清楚,我可以快速浏览代码并找出发生了什么。
(I almost never use "this" though, except to prevent a name clash.)
(不过,我几乎从不使用“this”,除非是为了防止名称冲突。)
回答by Christophe Herreman
The reason people do it (in my experience) is to differentiate between member variables and function parameters. In Java you can have a class like this:
人们这样做的原因(以我的经验)是为了区分成员变量和函数参数。在 Java 中,你可以有一个这样的类:
public class TestClass {
int var1;
public void func1(int var1) {
System.out.println("Which one is it?: " + var1);
}
}
If you made the member variable _var1 or m_var1, you wouldn't have the ambiguity in the function.
如果您将成员变量设为 _var1 或 m_var1,则函数中就不会有歧义。
So it's astyle, and I wouldn't call it bad.
所以这是一种风格,我不会称之为坏。
回答by Anders Sandvig
sunDoesNotRecommendUnderscoresBecauseJavaVariableAndFunctionNamesTendToBeLongEnoughAsItIs(); as_others_have_said_consistency_is_the_important_thing_here_so_chose_whatever_you_think_is_more_readable();
回答by PhiLho
Personally, I think a language shouldn't make rulesabout coding style. It is a matter of preferences, usage, convenience, concept about readability.
Now, a project mustset coding rules, for consistency across listings. You might not agree with these rules, but you should stick to them if you want to contribute (or work in a team).
就我个人而言,我认为一种语言不应该对编码风格制定规则。这是一个偏好、用法、便利性和可读性概念的问题。
现在,项目必须设置编码规则,以确保列表之间的一致性。您可能不同意这些规则,但如果您想做出贡献(或在团队中工作),就应该遵守这些规则。
At least, IDEs like Eclispe are agnostic, allowing to set rules like variable prefixes or suffixes, various styles of brace placement and space management, etc. So you can use it to reformat code along yourguidelines.
至少,像 Eclispe 这样的 IDE 是不可知的,它允许设置变量前缀或后缀、各种样式的括号放置和空间管理等规则。因此您可以使用它来按照您的指导重新格式化代码。
Note: I am among those keeping their old habits from C/C++, coding Java with m_ prefixes for member variables (and s_ for static ones), prefixing booleans with an initial b, using an initial uppercase letter for function names and aligning braces... The horror for Java fundamentalists! ;-)
Funnily, that's the conventions used where I work... probably because the main initial developer comes from MFC world! :-D
注意:我是那些保持 C/C++ 旧习惯的人之一,使用 m_ 前缀为成员变量(静态变量使用 s_)编码 Java,使用首字母 b 前缀布尔值,使用首字母大写字母作为函数名称和对齐大括号。 .. Java 原教旨主义者的恐怖!;-)
有趣的是,这是我工作时使用的约定......可能是因为主要的初始开发人员来自 MFC 世界!:-D
回答by Matt
Here's a linkto Sun's recommendations for Java. Not that you have to use these or even that their library code follows all of them, but it's a good start if you're going from scratch. Tool like Eclipse have built in formatters and cleanup tools that can help you conform to these conventions (or others that you define).
这是Sun 对 Java 的建议的链接。并不是说您必须使用这些,甚至它们的库代码都遵循所有这些,但如果您从头开始,这是一个好的开始。Eclipse 之类的工具内置了格式化程序和清理工具,可以帮助您遵守这些约定(或您定义的其他约定)。
For me, '_' are too hard to type :)
对我来说,'_' 太难打了 :)