[英]What is the motivation behind RequireThis?
Checkstyle warns about RequireThis Checkstyle警告RequireThis
Personally, I find it more readable (especially when reading not from IDE, but from code review tools, for example), but are there other reasons for this check beside readability? 就我个人而言,我发现它更具可读性(尤其是当不是从IDE而是从代码查看工具读取时),但是除了可读性之外,还有其他原因进行此检查吗?
Consider the following: 考虑以下:
int b,c;
public MyConstructor(int a, int b){
b = a;
c = b;
}
It's legal. 是合法的 It compiles. 它编译。 It probably doesn't do what you wanted it to. 它可能没有执行您想要的操作。 Adding this
makes sure that you don't accidentally shadow something you didn't mean to. 添加this
可确保您不会意外地遮盖了您本来不是想要的东西。
It's just convention with some pros and cons. 这只是一些优点和缺点的约定。
This check is most useful if you do not use IDE. 如果您不使用IDE,则此检查最有用。 It may help for code review to disambiguate a code reference, improve readability and prevent confusion with local variable or parameter accesses. 它有助于代码审查以消除代码引用的歧义,提高可读性并防止与局部变量或参数访问混淆。
On the other hand, most coding styles do not recommend using this
everywhere, as this may be considered as unnecessary, redundant and too verbose. 另一方面,大多数编码样式不建议在各处使用this
,因为这可能被认为是不必要,多余和太冗长的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.