简体   繁体   中英

Java “this” Keyword Impact on Code

So I've been writing code for a while using Netbeans, and the fastest way that I noticed that I could get a dropdown menu for variables and subroutines was using the this keyword(when I was new) and ever since it has become a habit.(I am aware of Ctrl + Space) I was curious of how this actually affects performance of a program when used for every variable name. Also, from what I've heard some programmers like the this keyword because it seems to be easier on the eyes sometimes.

这个:

It makes no difference performance wise as it compiles to the same bytecode.

However, IMHO, it does reduce readability by cluttering the code.

Note that with CTRL+SPACE you can auto-complete the class variable names without having to type this .

Personally, I prefer it when I read code that uses "this" to reference instance variables.

What if you have a class that is over 200 lines long?

You'd be constantly checking where the variable was defined.

I was curious of how this actually affects performance of a program when used for every variable name.

It has no performance advantage or disadvantage.

Also, from what I've heard some programmers like the this keyword because it seems to be easier on the eyes sometimes.

It is debatable. So programmers think it makes code easier to read. Other programmers think it makes code harder to read. You need to make up your own mind on that ... maybe in conjunction with other coders who you work with.

(I'm ambivalent. I use the Eclipse IDE, and Eclipse syntax highlighting makes it easy to ignore the clutter of (technically) unnecessary this keywords.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM