Last year I was mentioning to Neal that
one of my pet peeves in coding is unnecessary, and
dangerous, use of this, as in:
public void foo(int someVariable)
{
this.someVariable = someVariable;
?
}
The reason I don?t like this is
- the parameter someVariable hides the member variable within this method foo().
-
if later in this method, I update someVariable (thinking
that it is the member) I am
modifying the local variable/parameter and not the member. -
once I see developers get into this habit, I see more and more superfluous-often noisy-use
of
this.someThing() and this.someThingElse().
It concerned me that tools often promoted this style of using this.
Neal mentioned a convention
Thoughtworkers are using (most likely proposed and/or promoted by
Martin Fowler?). They use
_ for member variables. At first thought that sounded strange. But, once
I tried it for a few months, I found it relatively easier to use that style.
public void foo(int someVariable)
{
_someVariable = someVariable;
?
}
After a recent presentation I gave, a couple of people wrote ?using _ for member names
is not an
industry standard.? When I mentioned this to Neal,
he said, ?I mention this in my talks. Google for
Java Coding Style and you will see how many industry standards exist. Which one are
you referring
to. So, if someone has a problem using _ send them to me.?
So, here you have it folks. You know where to find Neal!
And if you are going to attend my talk today, be
ready for some interactive live coding and some more of those underscores! :)