Odd how sometimes the smallest detail can open up a whole battle. Recently Microsoft released their source analysis tool which, very much in the FxCop fashion, gives you hints and directives to improve your code on every level. When you run the tool you will discover how bad your programming habits are. In particular, I was surprised to see hundreds of times the SA1101 rule appearing after running the analysis on GraphSquare
SA1101: Verifies that calls to local members are prefixed with the ‘this.’ notation.
Ah well, why not indeed? It wasn’t clear to me why this is such a problem yet as a good believer I started to consciously use ‘this’ more often. Until…I started to use Resharper v4.0 (see the screenshot below) which keeps on saying that ‘Qualifier this is redundant’.
Yeah right, so now what? After questioning a bit the Oracle I discovered that I’m not the only one who is wondering what to do. But instead of finding a clear answer it seems that the whole discussion around ‘this’ is more a style-and-colors discussion than a deep compiler-and-performance issue. To some ‘this’ is noise while to others ‘this’ is a clarifying keyword. If you follow the thread a bit you will see that the discussion swiftly circles around the usage of ‘this’ to emphasize the use of private vs. public fields, which then in one go turns into the holy battle of naming convention (do you use underscores or ‘m’ or ‘m_’ to name private fields?) with statements like:
I hate the ‘this.’ keyword.
It produces visual noise and makes code unreadable and unmaintainable.
Maybe we should consider always use the full namespace when referencing a type as well?
Well, luckily someone has the wise observation that
…everybody is different and everybody thinks their way is the best way.
I don’t think there’s any right or wrong answer to this.
Personally, I think the compiler should somehow force you to do one or the other. Artistic freedom on a code level is in general a bad thing. Languages like the old ASP or JavaScript are very permissive and allow bad code and bad habits, which in the hands of not-too-smart or not-too-experienced programmers results in
On top of this all there is a bug with SA1101 when you use anonymous types, see the bug report here. To end with a positive twist, the source analysis is nevertheless a wonderful addition to one’s toolbox and you can model it to your needs, see Love the dot who has a tutorial on creating your own analysis rules. You can also find a complete list of the analysis rules in Wojciech Zimirski’s blog.
Addendum: Resharper’s default setting for ‘this’ can be changed and the behavior I refer to is the default one after setup.
Maybe tbe solution is an IDE that formats the code to *your* personal style.], big issue with such a solution is that code merge doesn’t work at all unless code merge tools can do intelligent pre formatting before…
By Daniel Fernandes August 22, 2008 - 9:50 pmIf that was possible then one doesn’t get annoyed because he/she didn’t obviously “get” that using the “this” prefix is useless. All this is obviously a matter of personal preference and sadly that’s the sort of things were a decision needs to be taken from the top.