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?

In my opinion (and in the opinion of many much more competent developers than myself), it is always good to be explicit about scope, because it removes any ambiguity from the code, drastically reducing the possibility of expensive and time consuming breakage during maintenance.

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. 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.

ryanm
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to