I'm here to make a point for the revival of the use of the keyword 'this' in programming (in the languages where it exists, that is).
But first of all, why revival? Is it dead, extinct, forgotten? I certainly hope not. But I think it's falling in disgrace in the programmer community.
I'm an ActionScript programmer myself and last night I was reading 'Essential ActionScript 3.0', by Colin Mook (which book, by the way, I received from what I believe was the first Romanian Flex Camp - thanks, guys). There was an entire subheading dedicated to the use or omittance of the keyword this (strangely named 'Omitting the this keyword'), and the main idea I got from it was that since the compiler is smart enough to detect member variables even when this is omitted, then why should programmers go through all the trouble writing it?. Their supporting argument was that writing this 'can be laborious and can lead to clutter' and that a this-free code 'improves readability'. To this point I was already pretty annoyed witht their reasoning; next came the only reference to the quasi-positive aspects they see in using this: 'However, some programmers always prefer to use this simply to distinguish instance variables and instance methods from local variables'. Not cool!
This having been said, here are my arguments for the use of this:
- it's compiler friendly
- using this is [usually] more readable than not
Ok, and what about the supposition that writing this all the time 'can be laborious and can lead to clutter'? What's interesting is that there is a certain similarity between this and public - they're both self-understood, aren't they? Yet, as much as I know, almost no-one omits public. Why might that be? Why would people go through the trouble of writing something which is self-understood? I think it's that because it saves you a brain cycle - you don't have to go through thinking that 'if the scope identifier is missing, then it must be public, since that's the default'. And my contention is that the same applies for this. It speeds up reading and clearly specifies parenthood and scope. As for clutter, I think it's quite a trivial argument - there are 5 extra characters. Indeed, when using many instance variables on the same line it might lenghten it to some extent, but I think - and this is actually my overall contetion - that the benefits far outweigh the costs of using the keyword this in your code.
What do you guys say about this? I'd love to hear about your coding practices.
No comments:
Post a Comment