简体   繁体   English

RequireThis背后的动机是什么?

[英]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.

相关问题 物业的动机是什么? - What's the motivation for properties? 默认方法与抽象类的接口,动机是什么? - Interface with default methods vs abstract class, and what's the motivation? 使用工厂方法模式而不是简单工厂的动机是什么 - What is the motivation of using factory method pattern rather than simple factory 在Java中,将所有接口提取到单独项目中的技术动机是什么? - In Java, what is the technical motivation for extracting all interfaces into a separate project? 使用非Sun JRE / JDK的动机是什么? - What is the motivation for using a non-Sun JRE/JDK? 是否应该启用 Checkstyle 中的 RequireThis 检查? - Should the RequireThis check in Checkstyle be enabled? 在Java中使用基于Vector的Stack实现而不是链表的动机是什么? - What is the motivation of using a Vector based implementation of a Stack in Java instead of a linked list? Tomcat 为每个 HTTP 请求分配一个新线程,那么使用 future.get() 的动机是什么? - Tomcat allocates a new thread per HTTP request so what's the motivation of using future.get()? JSR-310中两个不同的基于周的年定义的动机是什么? - What is the motivation for two different week-based-year definitions in JSR-310? 使用Maven的动机是什么? 我可以只创建一个构建脚本吗? - What is the motivation of using Maven? Can I just create a build script instead?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM