为什么Java使用this.member约定?
我经常看到 Java 代码中使用以下命名约定。
class SomeClass {
private final String name;
public SomeClass(final String name) {
this.name = name;
}
}
这对我来说似乎有点奇怪。首先,如果您碰巧拼错了方法签名中的变量,它仍然会编译...
class SomeClass {
private final String name;
public SomeClass(final String nane) {
this.name = name;
}
}
编译正常。可能将 nane 标记为未使用的变量,但赋值(只是变成自赋值)会静默编译。
我发现自己想使用“m”来表示成员变量...
class SomeClass {
private final String mName;
public SomeClass(final String name) {
mName = name;
}
}
它比 .this 变体短,并且捕获了前面显示的奇怪的拼写错误。
然而,当我将其作为我们新项目的惯例提出时,我的一位同事给了我各种批评,指出“在 Java 中我们不这样做”。
只是好奇为什么?
I quite often see the following naming convention used in java code.
class SomeClass {
private final String name;
public SomeClass(final String name) {
this.name = name;
}
}
This seems a little odd to me. First off if you happen to misspell the variable in the method signature it will still compile...
class SomeClass {
private final String name;
public SomeClass(final String nane) {
this.name = name;
}
}
Compiles fine. Possibly flags nane as unused variable but the assignment (which just becomes a self assignment) silently compiles.
I find myself wanting to use 'm' for member variables as such...
class SomeClass {
private final String mName;
public SomeClass(final String name) {
mName = name;
}
}
It is shorter than the .this variant and catches the odd misspelling error shown previously.
However a colleague of mine gave me all kinds of flack when I brought this up as a convention on our new project stating that "in java we don't do that.".
Just curious as to why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
就我个人而言,我不喜欢使用前缀 - 它使代码更难阅读,IMO。我相信不同的人以不同的方式阅读 - 我最终“在脑海中大声朗读”,而前缀中断了这个过程。显然你可以习惯它,但我宁愿不必。
然而,声称没有人使用这样的前缀是错误的。我曾在使用 Java 的各种不同公司工作过 - 有些使用了前缀,有些则没有。
我还想指出,大多数 IDE 都会向您发出有关拼写错误示例中的无操作分配的警告。例如,在 Eclipse 中我得到:
如果你经常忽略警告,我会说你有更大的问题:)
Personally I don't like using prefixes - it makes the code harder to read, IMO. I believe different people read in different ways - I end up reading "aloud in my head" and the prefixes interrupt this process. Obviously you can get used to it, but I'd rather not have to.
However, it would be a mistake to claim that no-one used prefixes like this. I've worked at various different companies using Java - some used prefixes, some didn't.
I'd also point out that most IDEs will give you a warning about a no-op assignment in your typo example. For example, in Eclipse I get:
If you regularly ignore warnings, I'd say you have bigger problems :)
我宁愿只在少数需要的地方使用
this
来消除歧义,而不是在代码中到处拖动前缀。I'd rather use
this
only in the few places where needed to clear the ambiguity, than drag with me the prefixes everywhere in the code.对成员变量使用特定的前缀是一种匈牙利表示法——技术性的、不好的那种。信息(什么是成员变量,什么不是成员变量)已经在代码中,无需重复。
正如您所注意到的,一个好的 IDE 会警告您有关未使用的变量的信息(并且可能允许您将该警告转变为错误)。语法突出显示还将区分局部变量和成员变量。为什么要提出丑陋的代码约定来完成 IDE 的工作,减少一种非常特定的程序员错误的可能性?
Using specific prefixes for member variables is a kind of Hungarian notation - the technical, bad kind. The information (what is and is not a member variable) is already in the code, no need to duplicate it.
As you've noted, a good IDE will warn you about the unused variable (and probably allow you to turn that warning into an error). The syntax highlighting will also distinguish between local and member variables. Why come up with ugly code conventions to do the IDE's job of making one very specific kind of programmer mistake less likely?
现代 IDE(例如 Eclipse)可以自动为您生成 getter 和 setter,这样就可以避免麻烦。
Modern IDEs such as Eclipse can automatically generate getters and setters for you so this way troubles can be avoided.
在第一个示例中,必须使用
this
限定符来显式引用实例成员name
,而不是局部变量(方法参数)name
。当您提到参数名称可能存在拼写错误时,您是对的,这也是一些令人沮丧的错误的根源。过去,我还对字段使用了前缀命名约定,但随着 java 越来越依赖反射,这确实带来了一定程度的痛苦,所以最近我放弃了它。
通过使用单元测试,以及使用在签入之前分析代码并为您标记这些错误的 IDE,可以减少由于拼写错误而出现错误的可能性。
In the first example, the
this
qualifier is necessary to explicitly reference the instance membername
as opposed to the local variable (method parameter)name
.You're right when you mention about the possibility of misspellings of the parameter name, and this has been the source of some frustrating bugs. In the past, I've also used a prefix naming convention for fields, but as java more and more relies on reflection, this does introduce some level of pain, so lately I've moved away from it.
The liklihood of bugs due to misspelling can be reduced by using unit tests, and also by using an IDE that analyses the code prior to checking in and flags these kids of errors for you.
我觉得很奇怪,你的问题做出了这样的假设
但这绝不是真的,因为不同的人有不同的约定。
在我的代码中有很多 this.member,但这只是因为我的 IDE(netbeans)生成了它,而且我不太关心重写。
It looks odd to me that your question make an assumption that
By no mean is this true, because different people have different conventions.
In my code there are a lot of this.member, but that's only because my IDE (netbeans) generates that, and I don't care that much to rewrite.
this.member
不是约定,但它是必要的,例如,当您必须在方法中显式引用与局部变量同名的实例变量时。在其他非强制性情况下,您可以省略它,因为它是隐式的,并选择您想要的约定。
this.member
is not a convention but it's necessary, for example, when you must explicitly refer to an instance variable that have the same name of a local variable into a method.In other not mandatory cases you can omit it, because it's implicit, and choose what convention you want.
实际上,我有一个讲座,我的一位教授在作业中给了我们以下 java 类:
我提到了 Java 编码约定 并善意地询问他我是否可以重构他的代码。基本上
this
是为了编写漂亮的构造函数和设置器。当然,对于所有其他情况,我需要将方法参数重命名为 underunder + 参数名称,甚至更好的 p + 参数名称,正如我经常看到的那样。I actually had a lecture where a prof of mine gave us the following java class in an assignment:
I referred to the Java Coding Conventions and kindly asked him if I can refactor his code. Basically
this
is for writing beautyful constructors and setters. And of course for all other cases for which I would otherwise need to rename method parameters to underunder + parameter name or even better p + parameter name as I have seen so often.