ReadableInterface
- 完全是多余的。在现代 IDE 中,您不需要指定变量的类型或其范围 - 着色和快速跳转为您完成所有工作。因此,在我看来,这是一种糟糕的风格,因为您会输入冗余字符并迫使人们阅读/维护它。 - kiedysktossun不建议使用下划线,因为Java变量和函数名通常已经足够长了。 正如其他人所说,一致性在这里很重要,所以选择你认为更易读的方式。
snake_case
形式更易于阅读。特别是对于短单词(1-2个字母),我肯定会这样认为。至于“难以输入”,键入一个具有许多混合大小写的单词也不是很方便。我主张这是一个习惯问题。在Java中,我建议使用JLS /等推荐的常见形式。在Ruby/Python/C中,使用蛇形命名法。等等... - Per Lundberg规则:
this
来表示成员变量。然而,我并不是一个狂热者。 - Chris Cudmore在变量前使用'm_'或'_',可以更容易地在对象的方法中找到成员变量。
另外一个好处是,键入'm_'或'_'会使智能感知首先弹出它们 ;)
if (itsEmail.equals(email))
。 - davetron5000I happen to like leading underscores for (private) instance variables. It seems easier to read and distinguish. Of course, this thing can get you into trouble with edge cases (e.g., public instance variables (not common, I know) - either way you name them, you're arguably breaking your naming convention:
private int _my_int;
public int myInt;? _my_int? )
As much as I like the _style of this and think it's readable, I find it's arguably more trouble than it's worth, as it's uncommon and it's likely not to match anything else in the codebase you're using.
Automated code generation (e.g., Eclipse's generate getters and setters) aren't likely to understand this, so you'll have to fix it by hand or muck with Eclipse enough to get it to recognize it.
最终,你将与(Java)世界的其他人偏见相违背,并可能遇到一些烦恼。正如之前的帖子所提到的,代码库中的一致性胜过以上所有问题。
为什么在早期使用下划线会被认为是不良风格,这是有原因的。在运行时编译器还是无法承受的东西,显示器只有惊人的320x240像素分辨率的时代中,_name
和__name
之间往往很难区分。
区分私有变量和公共变量很好,但是我不太喜欢在编程中使用下划线。如果可以的话,我会避免在新代码中使用它们。