• 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

why using 'this' if we are in instance method???

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if it is in wrong forum, but I couldn't find coding standard forum and I thought it should fit in refactoring...





does that this is serving special purpose here??

Thanks.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rathi,

this is used to diffenciate between local variable and instance variable
For eg if you want to initialize i and j in constructor


this is not required in your example.

Regards,
Raja
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajasekar Elango:
Hi rathi,

this is used to diffenciate between local variable and instance variable
For eg if you want to initialize i and j in constructor


this is not required in your example.

Regards,
Raja



Yes, I agree. But there is no need to differentiate instance variable and local variable here....

But I have seen code using this unnecessarily... just like in my example...

Thanks.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some people prefer to always use the this prefix because they find it be more readable. Often those are people coming from other languages like Smalltalk, which unlike Java always require such a prefix.
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like using "this" because it makes data members stand out more.

Now that I use IntelliJ as my IDE it's less important, but the habit is still with me. Helps those who read my code without IntelliJ.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
Some people prefer to always use the this prefix because they find it be more readable. Often those are people coming from other languages like Smalltalk, which unlike Java always require such a prefix.



I am one of the "some people". in industry, readibility is really important when the code can be maintained by many people.

For most of the IDE with getter/setter generation function, "this" is always auto-assigned.

I also feel that it looks more OO when "this" got prefixed for class level variables and methods.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hank GU:

I am one of the "some people". in industry, readibility is really important when the code can be maintained by many people.



I wholeheartedly agree with the "readability important" part. The crux is: I feel that the this-prefix mostly adds clutter...
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hank GU:


I also feel that it looks more OO when "this" got prefixed for class level variables and methods.



IMHO this is misleading when it comes to instance methods as it indicates that the method that gets executed is in the class that you are currently editing but this is not the case - the method may be overriden.

D.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never use this because - as Ilja says - it adds to code clutter.

I also never do this sort of thing.


Hiding of class or instance variables is prone to error or confusion and is best avoided, in my opinion.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roger Chung-Wee:
I never use this because - as Ilja says - it adds to code clutter.

I also never do this sort of thing.


Hiding of class or instance variables is prone to error or confusion and is best avoided, in my opinion.



Whether or not one likes it, the following is somewhere in between "very common" and "the coding standard" in setters (and constructors), even when it is not used elsewhere.

I don't see any harm in a short method -- what else were you going to name the parameter: "aMumble", "_mumble", "pMumble"?

On the other hand, in a longer method (I know, yadda yadda refactor), yes in a longer method you can loose track and mumble when you should have this.mumbled...
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Don Kiddick:
IMHO this is misleading when it comes to instance methods as it indicates that the method that gets executed is in the class that you are currently editing but this is not the case - the method may be overriden.



Honestly, I've never read anyone else with this concern... In Java,
polymorphism is part of the furniture, as they say in Jamaica.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A problem with variable hiding is that one day someone will write this.

I'd rather avoid the potential problem by not hiding variables. Some people have standards for naming which involves prefixes or suffixes to distinguish their instance variables from their local variables.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roger Chung-Wee:
A problem with variable hiding is that one day someone will write this.

I'd rather avoid the potential problem by not hiding variables. Some people have standards for naming which involves prefixes or suffixes to distinguish their instance variables from their local variables.



1. If you are using an IDE it should warn you with a message like "assignment with no effect" in this case.
2. If you are not using an IDE, you are obviously too smart to ever write this! Seriously, even if you are using some other naming convention, like _mumble for parameters, I think you are just as likely to miswrite the above by way of a typo. Think about it...
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roger Chung-Wee:
A problem with variable hiding is that one day someone will write this.

I'd rather avoid the potential problem by not hiding variables.



I'd rather avoid it by using smart tools and extensive automated unit testing.

Some people have standards for naming which involves prefixes or suffixes to distinguish their instance variables from their local variables.



Ah, yes, the dreaded Hungarian Notation: http://ootips.org/hungarian-notation.html

Not something I like to see in my Java source code...
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even better: Naming :roll:
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roger Chung-Wee:
Even better: Naming :roll:


Amazing. I'm being forced to review the code of a contracter and I think he managed, in his brief stay, to use nearly 30 of those 34 items
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This'll probably make you all laugh but who cares, in a recent jsp/servlet/bean program I wrote with someone new to java who couldn't keep track of the scope of different variables I ended up prefixing the variables as follows :
application - a
session - s
request - r
instance - i
local - l
and arguments/parameters into methods with 'parm'.

I had to check a piece of the code today not having looked at it since before Christmas and found it very useful.

Go on then laugh.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not going to laugh, but rather to suspect that there is too much code in your JSPs...
 
steve Barf
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We specifically aimed for JSP's with an absolute minimum of code. This is because I inherited an applicaion with >1000 line JSP's which I swore I would never duplicate.

Each request from a given JSP first went to a corresponding servlet. Processing was performed by the servlet and a javabean with request scope. Shared data was held in session and application beans. The final response was generated by the JSP retrieving stored data in the session(s)/application(a) bean plus derived data in the request(r) scope javabean. ie. the jsp handeled the displaying and not the processing.

Along Model 2 lines.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still like to use scope warts. Laugh if you must, but they work for me and I mandate them on the projects I run.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
I still like to use scope warts. Laugh if you must, but they work for me and I mandate them on the projects I run.



I find that scoping errors are easily found by unit tests.

And did you know that Eclipse can syntax highlight fields differently from local variables?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:

And did you know that Eclipse can syntax highlight fields differently from local variables?



Yes, I do -- there's a discussion about that in the blog entry I linked to. But the thing is that not all changes are made in Eclipse (or IDEA, in my case) -- sometimes I'll make a quick change in vi, for example. Fancy colorizing and IDE-generated warnings aren't going to help, in this case.

I use many tools to make me more productive, but I don't like the idea of depending on tools to prevent errors if there's a practice which works and which is tool-independent. If I can't write good code unless I'm using a particular tool, well, than what does that say about me?

Now, about unit tests: point taken. For the fake "collection" example in the blog entry, the tests should have picked up the problem right away. The real bug was much more subtle, however -- and the relevant test passed because, by coincidence, the local and member versions of the variable held the same value in that particular case. There could have been multiple tests for this one fairly simple method, but one seemed like enough. As has been pointed out many times, tests can never prove the absence of bugs, and practices that prevent them from happening are a good thing.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeff Albrechtsen:


Honestly, I've never read anyone else with this concern... In Java,
polymorphism is part of the furniture, as they say in Jamaica.



"The Elements Of Java Style I think.

D.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
Yes, I do -- there's a discussion about that in the blog entry I linked to.



Ah, I see - that comment actually is posted by myself, but I got an error message and thought it didn't succeed...

If I can't write good code unless I'm using a particular tool, well, than what does that say about me?



Well, it's not that you can't write good code without the tools, it's just that the tools change the balance for some of the practices of writing good code.

As has been pointed out many times, tests can never prove the absence of bugs, and practices that prevent them from happening are a good thing.



Point taken, too. There is, however a balance - how often does the problem happen in practice, and what disadvantages does the practice of preventing it have?

To me, the advantages simply don't make up for the decreased readability and the increased maintenance effort (having to change the variable name when its role changes).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic