-1

In a Java code there are a few lines like:

this.noWar=Con.get("xys").isEmpty()  
if(noWar) ...  

What are the best practices suggested above? The first line uses this.noWar which could have been simply written as noWar=... Is there any advantage to using this.noWar here? or in general when assigning a value to class properties?

pensee
  • 375
  • 1
  • 10
  • 1
    with only seeing this bit of code, we can't tell whether or not it is mandatory. With a score of 379, I would assume you had enough experience (if not that many) to know that. Do you know why/when the this keyword is used? – Stultuske Aug 24 '23 at 13:11
  • This question's going to come down to opinions. I'll throw mine in: I always use it, because then `this.` becomes synonymous with "scoped field/method access". It also can make autocomplete a little nicer to work with. The drawbacks are excessive `this.` calls in your code. The only case where you _need_ `this.` is when the field name is overshadowed by a local variable, and thus `this.` becomes the only way to disambiguate the two. – Rogue Aug 24 '23 at 13:11
  • To contrast Rogue's opinion, and to make clear why this isn't a suitable question for Stack Overflow: I only use `this.` to avoid ambiguity. I find it absolutely jarring and annoying to read unneeded and unnecessary uses of `this.`. – Mark Rotteveel Aug 24 '23 at 13:13
  • And the question also shows no evidence of prior research. Yes, you may have searched for [prior similar questions](https://www.google.com/search?q=site%3Astackoverflow.com+java+when+use+this), but where do you *show* this in the question, where do you use it to make the question more unique, more helpful to the site, and of higher quality, rather than yet "one more" of the same? This is a reason for a -1 vote – Hovercraft Full Of Eels Aug 24 '23 at 13:13
  • @Rogue But in this code this. is used in one line and not in another. Do you think it should be consistent? Either use it in both lines or ignore it on both. – pensee Aug 24 '23 at 13:51
  • 1
    @pensee re-read my first comment. with only this code shown, this is impossible for us to know. it might be that it HAS to be there, it might be optional, but that depends on the rest of the code. Also: this code won't compile, so it doesn't really matter – Stultuske Aug 24 '23 at 13:55
  • @Stultuske Yes I do understand the use of `this` keyword it's used to reference the class instance variable. The `noWar` variable is an instance variable and there is no other variable of the same name anywhere else in the class. So one could get rid of this. altogether. – pensee Aug 24 '23 at 14:04
  • in that case, yes, one could, but it's clear that based on those two lines of code alone, we couldn't know whether or not there is just one variable by that name – Stultuske Aug 25 '23 at 06:32
  • @pensee consistency is honestly always important. Inconsistent code leads to code full of quirks and surprises, making it hard to read. I'd say the same applies for your usage of `this.`: whatever you should choose, that choice should be consistent across your code for a project. – Rogue Aug 25 '23 at 14:28

0 Answers0