Monday, June 23, 2003

"this" Considered Harmful

This is a favourite of mine, one that I would consider others adopting: "Don't Repeat Yourself. If you really insist upon adorning your object fields with some token, I will argue that "this." is a horrible way of doing so. Instead, consider prefixing your field with a consistent name or character...Adopting a prefix like "m_" or "_" is preferable to "this" because the compiler ensures consistency. It does not rely on human programmers to remember to repeat the "this" prefix over and over."

I would prefer using something a little more expressive to prefix my variables names instead of "m_" or "_". I prefer to name the variables being passed in with prefixes such as "new", "old", "existing", etc., whatever makes sense.

This is similar to my beef on checked vs unchecked exceptions.
Post a Comment