ryanm wrote:
The idea is to use it (this) when you have two variables with the same
name, usually to distinguish between the class member and a locally
declared variable. You must know that :) The compiler won't know to
use "this" or not in those cases, so it is important to use it in those
contexts.
Actually, the compiler *does* know, it always adds "this." to
references without explicitly stated scope.
Which brings us full circle, all the way back to maintainability.
Now, if you have a class, and all of the member functions and variables
are referenceless (using "assumed" scope without the this reference),
and all of a sudden, in the middle of a method, you have a this
reference because of a potential scope conflict, what does a developer
looking at your code 2 years after you've left the job think about it?
Does he remove the single this reference, to make it more readable and
consistent, thus breaking the method and blowing up the whole project,
all without knowing how or why? Martin piped up with "I would refactor
the method to make it more readable..." immediately, which, in this
case, would've broken the method (if he removed the reference) and
possibly the whole class, which is exactly how these sort of expensive,
time consuming, and difficult to troubleshoot problems come up. On the
other hand, if the original developer used the this reference every
time, it would already be consistent, readable, and explicit about scope.
The other question is, would Martin (not to pick on Martin, but he
was vocal about it, so I use him as an example) take a class that stated
scope explicitly and remove all the this references, possibly breaking
the class in the process? It sounds like it, from his post. The question
is, is that a good practice or a bad practice, and does that make him a
"good" developer, or a "bad" one, given the potential for expensive and
time consuming breakage caused solely by his dislike for the keyword this?
Its ok, you can pick on me, i dont mind ;)
I'll take each piece seperately.
1. Removing the single 'this' reference.
well no, i wouldnt remove it because i know why its there. If the method
was long and could / should be refactored, then that is what i would do.
Of course there exists edge cases where that isnt feasible, but most
programs dont implement DES algorithms (to relate this to an earlier
post) and a lot of legacy code i have worked with has benefitted from
being re-factored.
If i see one 'this' reference among a series of unspecified local
references then I would look at it carefully and think that the
programmer had good cause to specify the scope in that case.
One 'this' reference doesnt have much of an impact on readability but
using it everywhere does, as Jesse highlighted.
I would rarely advocate becoming dogmatic about any technique, *always*
do this or *always* do that, because its not often an all or nothing
decision.
Of course, in some far off dreamland this code would also have unit
tests so that if i did refactor it or unthinkingly removed the one
reference to 'this' then a nice little red bar would tell me id done
something bad, but again, im realistic, most code doesnt have unit tests. :)
2. Removing all references to 'this' in supplied code.
It really depends on the level of maintenance im expected to undertake
with the legacy code. If it was just a minor bugfix or two, or some
tweak then i would be inclined to save myself the effort and just do
what is required (i.e. leave the extraneous references to 'this' intact).
If the code required some major work then I would remove the references
to this, except where necessary.
Like i stated in my first post about this subject, sometimes its
necessary and i know when that is.
Most of the time it isnt and personally i feel that it clutters the code.
One more thing worth mentioning, and I don't say this to be rude or
as a slight to anyone on this list, but the opinion that the this
reference is bad and should be avoided seems to be unilaterally coming
from people who learned programming in Flash, while the opinion that
scope should be stated explicitly seems to be coming from people with
more formal training in software development and experience in
(non-Flash) real world development. Personally, I have to give more
weight to the opinions of people with more formal training and more
varied real-world experience, because there are some things you just
can't learn in a year or two of using Flash for web development.
Unless you are a mind reader or have access to information about each
poster that is in this discussion then i would say that its a dangerous
and false generalization.
I initially learnt my programming way back in the mists of time, before
flash was but a glint in some guys eye, and have had my fair share of
formal training and developed in non-flash 'real world situations', but
in my opinion that isnt really relevant to the discussion, suffice it to
say :
I only use 'this' in Java where necessary, as does most of the other
Java source code i've seen.
I only use it in C++ where necessary, again as does most of the other
C++ code that i've seen.
I could go through the plethora of other languages i've used over the
years, but most of them dont even have 'this' :)
> There
> is a reason that experienced developers like to type those extra 5
> characters, and it is to save time, money, and the embarrasment of
> explaining to your boss that you deleted something without fully
> understanding the implications.
which in some ways is an oxymoron. An experienced developer would not
delete something without understanding the implications, or would only
do so if they had a safety harness (i.e. unit tests) to allow them to
test the effects quickly and easily.
thanks,
Martin.
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders