• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

referencing fields qualified ?

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic