Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

referencing fields qualified ?

 
Uwe Schäfer
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

what is your feeling about using "this."-prefix in every access to a class-member ?
i feel i�d have to, in order to be consistent with setters i wrote (without event thinking about it) like:

or with constants i use


but now i see "this." all over the place. good or bad style ?
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo Uwe,

here is what Kathy Sierra and Bert Bates say about it:


Avoid shadowing variables. This occurs when an instance variable has
the same name as a local or block variable. While the compiler will allow
it, shadowing is considered very unfriendly towards the next co-worker
(remember: potentially psychopathic) who has to maintain your code.


So better avoid the "this." approach.

Regards,
Darya
 
Uwe Schäfer
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Darya Akbari:

So better avoid the "this." approach.


thanks for your answer. this is not necessarily about shadowing variables which is obviously a bad idea most of the time.

consider the simple code:


i do think that "this." improves readability here, even if it is not really necessary. what do you think ?
 
Frans Janssen
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Uwe,

My personal preference is to only use this or the class name where it is absolutetely required. And as Darya pointed out already, it is generally good style to avoid the possibility for naming conflicts.

I do however make exceptions for trivial setters or constructors, but I must confess that I already had to solve numerous bugs where I mixed up local and class level variables (so maybe I am too easy on myself! ).

BTW, in the coding rules we use at the workplace, we have the K&B rule that forbids shadowing.

Frans.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11943
212
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Uwe,

In many ways this is a case of personal preference. If you have a corporate statndard, use it, otherwise find a standard you like, and stick to it .

Personally I like the 'this.' prefix - it makes it clear that we are talking about a class variable, which can be useful for the next person to maintain your code (or even for yourself when you revisit the code 6 months later). I have noticed that many of the automatic code generators use the 'this.' prefix when referring to class variables - presumably to help humans read it (since auto generated code should be correct).

Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic