在java中创建_names_of_fields_like_these有什么问题?
有时我会看到这样的事情:
class Clazz {
private int _goodName;
private int _anotherGoodName;
...
}
我不明白。阅读这样的代码既困难又不寻常。有什么优点?
From time to time I see something like this:
class Clazz {
private int _goodName;
private int _anotherGoodName;
...
}
I don't get it. It's hard and unusual to read such code. What are the pros?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是一些人用来表示“这些是私有变量”的命名约定。
就我个人而言,我不是粉丝,因为我认为你可以省略下划线并获得相同的结果,但每个人都有自己的结果。我认为它可能起源于 IDE 出现之前的时代,当时您可能正在查看一个方法,并且某些变量的可见性/所有权并不总是清晰的。
It's a naming convention used by some people to indicate "these are private variables".
Personally I'm not a fan as I think you can leave off the underscore and achieve the same result, but to each his own. I think it may have it's roots in pre-IDE days when you might be viewing a method and the visibility/ownership of certain variables is not always clear.
我见过的带有下划线前缀的成员和方法的示例使用约定来指示当您无法将其设为私有时,您不应触及该成员或方法。我把它读作“这里有龙”,我自己从来没有理由使用它。
一个示例是 _jspService() servlet 开发中的方法。查看链接的 JavaDocs。
The examples of members and methods prefixed with an underscore I've seen use the convention to indicate that you shouldn't touch that member or method, when you can't make it private. I read it as 'here be dragons', never had a reason to use it myself.
An example is the _jspService() method in servlet development. Check out the linked JavaDocs.
这只是一个偏好问题。有些人喜欢在类的所有私有成员变量中添加“_”,而其他人则不喜欢。我个人不喜欢它,但又是偏好。
It's just a matter of preference. Some people like to add a '_' to all private members variables of a class, other's dont. I personally do not like it but again it's preference.
命名约定都是关于编写代码的人如何舒适地阅读/复制(尽管它们应该是关于每个人都容易阅读的内容)
我有一个朋友使用这个约定以及诸如:
对于他的所有字段等。它表示它是您正在查找的特定类的成员,但如果您不这样做,那么读起来会非常烦人。
Naming conventions are all about what's comfortable for the person writing the code to read/replicate (though they should be about whats easy for everyone to read)
I have a friend who uses this convention along with something like:
for all of his fields etc. It denotes it as a member of the particular class you're looking in but it gets very very annoying to read if you don't do the same.
如果您可能有一个成员变量、属性和参数都引用同一事物的情况,那么区分是非常有意义的:
If it is a likely scenario that you could have a member variable, a property and a parameter all refer to the same thing, it makes perfect sense to distinguish:
我曾经在代码中使用这些约定:
非最终静态字段以下划线开头。
参数以下划线结尾。
虽然您可能认为很难阅读变量的声明,但关键是要确保在阅读使用它们和的代码时它们弹出确保它们始终不同于任何非静态字段、参数和本地定义的变量。
我认为,这样的标准的另一个好处是,当其他人来修改代码时,他们可以快速识别静态字段和参数,这使得更容易理解代码,而不必总是引用定义。
我通常不考虑实例字段的约定,因为对它们的访问几乎完全是通过 s/getters 进行的。
我已经不再使用 Java 代码,而是更喜欢让我的 IDE 始终使用
this
完全限定实例字段,使用类名指定静态字段,并始终使用final
每个不变的变量,甚至是方法参数。走这条路的有趣之处在于,它鼓励良好的命名实践,至少对我来说是这样,因为我喜欢我的变量在带有this
前缀或类名时能够很好地阅读。I used to use these conventions in my code:
Non-final, static fields begin with an underscore.
Parameters end with an underscore.
And while you may consider it hard to read in the declaration of the variable, the point is to make sure they pop when reading the code in which they're used and to ensure that they are always different than any non-static fields, parameters and locally defined variables.
Another benefit of standards like this, i think, is when others come along to modify the code, they can quickly identify static fields and parameters which makes comprehension of the code happen more readily rather than having to always refer back to definitions.
I don't generally consider a convention for instance fields because access to them is almost exclusively through s/getters.
I've moved away from this with Java code, preferring instead to have my IDE always fully qualify with
this
for instance fields and with the class name for static fields and always usingfinal
on every variable that doesn't change, even method parameters. The interesting thing about going this route is that it encourages good naming practice, at least for me, because i like my variables to read nicely when prefixed withthis
or the class name.