Java peoples: Do you put "this" even when implied?

AFB

Lifer
Jan 10, 2004
10,718
3
0
Or am I just anal? I think it gives a nice easy/quick way to tell local variables from instance variables (same with methods).
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
I always use this-> in C++. I also tend to not use "using" in C++ too much.

I also like how python gets rid of the "magic" here and just makes you use the passed "self" object as you would any other object.
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
This is against every coding guideline doc I have ever seen. this should really only be used in constructors. If you want a way to visually distinguish your members, use a naming convention. In C++ it's m_var for members, currently in C# I'm using _var. Typing 5 extra chars per variable is a waste of screen real-estate and my eye-power.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: UCJefe
This is against every coding guideline doc I have ever seen. this should really only be used in constructors. If you want a way to visually distinguish your members, use a naming convention. In C++ it's m_var for members, currently in C# I'm using _var. Typing 5 extra chars per variable is a waste of screen real-estate and my eye-power.

It's in and IDE so it's highlighted. That's the main reason why I do it.
 

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
When I was taking Java, I tended not to use this at all... then again I made a pretty sh|tty programmer too.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: UCJefe
This is against every coding guideline doc I have ever seen. this should really only be used in constructors. If you want a way to visually distinguish your members, use a naming convention. In C++ it's m_var for members, currently in C# I'm using _var. Typing 5 extra chars per variable is a waste of screen real-estate and my eye-power.

Geez whiz. Buy a bigger monitor. this isn't much worse than appending an m_ in front of a variable name and it's far more standardized. Are there some semantic differences between C++/# and java that make it less desirable to use? In a large class where the the class members are a long way away from what you're reading it can be a helpful reminder of scope.

I tend to use it when there's a naming conflict between a method and the containing class (fairly often in constructors). Another thing it's very useful for is triggering code completion in an editor. Typing "this." plus the first few letters of the variable name is faster than typing the whole name from memory.

Edit: I guess I don't really have anything against naming conventions. There are some very good and well accepted ones in Delphi. But I've never seen anything like that in the java world that you can assume will be universally understood.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
I only use "this" in cases where it's not obvious what I'm referring to otherwise, which is rarely.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: CTho9305
UCJefe has the proper way to do it - naming convention.

What exactly makes you say that it is "the proper" way to do it? Last I knew it was a matter of personal opinion.

Personally I agree with kamper. mPoo or m_Poo is hideous IMO and is just a replacement for something that the language will do in a much more uniform and accepted manner.

Technically off topic but in C++ sometimes you actually *need* to use this->, although I don't really remember what for. Something to do with templates or inheritance or something.

But in the end, it's all just personal opinion.

(I actually used to not have this preference, but a comment from some c++ guru guy made me think about it, and I grew to like it)
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
Originally posted by: BingBongWongFooey
Originally posted by: CTho9305
UCJefe has the proper way to do it - naming convention.

What exactly makes you say that it is "the proper" way to do it? Last I knew it was a matter of personal opinion.

I agree here. The important fact is that it should be a convention and that consistency needs to be maintained across the entire program. That being said, my personal opinion happens to be the correct one. :)

Personally I agree with kamper. mPoo or m_Poo is hideous IMO and is just a replacement for something that the language will do in a much more uniform and accepted manner.

I never really liked the 'm' either which is why I moved to just _var. But seeing 'this' all over the place is way too much code clutter for me. Call me a code Nazi if you will, but I like it clean.

Technically off topic but in C++ sometimes you actually *need* to use this->, although I don't really remember what for. Something to do with templates or inheritance or something.

Also very true. There are several cases where you have to use the this keyword which is why it's there in the first place. Resolving ambiguity, some operator overlaods where you need a reference to the LHS, returning a pointer to yourself from a member function. There are probably some more that I'm missing.

But in the end, it's all just personal opinion.

It's only personal if you're the only one touching your code. In groups, having a coding standard is very important. Pick something and be consistent.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: UCJefe
I never really liked the 'm' either which is why I moved to just _var. But seeing 'this' all over the place is way too much code clutter for me. Call me a code Nazi if you will, but I like it clean.

I was actually going to say something almost identical (arguing that my style is cleaner), but when I thought about it, you can argue that either way is cleaner. Two people that both consider themselves code nazis could easily have very different coding styles.

But in the end, it's all just personal opinion.

It's only personal if you're the only one touching your code. In groups, having a coding standard is very important. Pick something and be consistent.

But what's required or pragmatic is not the same as what you personally prefer. ;) If you're required to do something then the entire discussion is irrelevant in the first place.