12

I've always been explicit with my code when using instance members, prefixing them with this. and with static members, prefixing them with the type name.

Roslyn seems not to like this, and politely suggests that you can omit this. and Type. from your code where appropriate...

...so where I would do this... (no pun intended)

public void DoSomethingCool()
{
    this.CallAwesomeMethod();
    CoolCucumber.DoSomethingLessAewsome();
}

...roslyn suggests I do this...

public void DoSomethingCool()
{
    CallAwesomeMethod();
    DoSomethingLessAwesome();
}

...however when it comes to using extension methods, it appears that I cannot omit the use of this. for example...

public int GetTotal()
{
    // This doesn't work    
    return Sum(o => o.Value);

    // This does
    return this.Sum(o => o.Value);
}

Questions:

  1. Why does Roslyn not seem to like explicit use of this. and Type. ?
  2. Why do I need to explicitly use this. for extension methods?
  3. Whilst not strictly part of this question, how do I resolve the discrepancy between Roslyn NOT wanting me to use this. and Type. and StyleCop's analyzers insisting that I use them?
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
  • 2
    Roslyn is just brain-dead about not liking you to use this. I suppose the Roslyn team members don't need IntelliSense, smart programmers they are. But given the quality of the product, they perhaps ought to have used it a bit more. Knowing when to tune-out the lightbulb is up to you, we can't help you with that. Fwiw, somebody else was asleep when they picked an icon with a bright background for a dark color scheme. Fit and finish is plain inadequate, it is going to take time. – Hans Passant Nov 11 '15 at 14:29
  • 3
    @HansPassant: This check can easily be disabled in Options. – SLaks Nov 11 '15 at 14:42
  • @SLaks I noticed a .ruleset file in my project which contained all of the Roslyn and Stylecop rules for a project: I stuck with StyleCop's recommendation rather than using Roslyn – Matthew Layton Nov 11 '15 at 14:51
  • There are actually issues possible with omitting `this` on extension methods in somewhat exotic situations: http://stackoverflow.com/a/27884101/1864167 – Jeroen Vannevel Nov 11 '15 at 19:37
  • 2
    Also note that ReSharper thinks `this` should typically be omitted as well. – Jeroen Vannevel Nov 11 '15 at 19:42

2 Answers2

8

You are correct on this behavior. It has struck me too when using Visual Studio 2015 and I wondered the same as you did. Here are my thoughts on this:

  1. Why does Roslyn not seem to like explicit use of this. and Type.?

    It is overhead, and it seems it wants to minimize the code needed. I prefer being explicit in my code though. It is much easier to understand the scope of a variable without having knowing the rest of the code. (Maybe it is also a marketing strategy to let us know about the code analyzers... Or am I thinking too much)

  2. Why do I need to explicitly use this. for extension methods?

    I guess this is the rule to supply the first parameter of the extension method, the this. Without providing this it doesn't know the context to call the method on. Actually, the extension method this.Sum(x => x) is actually rewritten as Enumerable.Sum(this, x => x). See the 'need' for this? I guess omitting this is technically possible, but I prefer to be explicit in this case too and I am glad the compiler does too.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

this. is a syntax that is provided in C# to resolve ambiguity. For example:

class Square
{
    private int size;

    public Square(int size)
    {
        size = size;      // How do we differentiate member and parameter?
    }
}

So using this. to be explicit is rather an oxymoron, because it is solving a problem caused by a lack of explicitness.

The 'best practice' I would recommend is to write code that is not ambiguous in the first place, thus eliminating the need to use this.. Ambiguity is not only disliked by the compiler but it can be extremely confusing for other programmers trying to read your code.

To achieve disambiguation we need to use different names for things like member variables and parameters. The most common approach (apart from this.) is to use naming prefixes to identify members. Many programmers dislike prefixes but this is usually because they have never seen/used a well designed prefix system - I've described my approach here, and once you get used to it, it is a very powerful way of communicating useful information to yourself and your teammates, and eliminating several common types of bugs caused by ambiguity and mixups.

Community
  • 1
  • 1
Jason Williams
  • 56,972
  • 11
  • 108
  • 137
  • I rather like your linked approach. One thing though, what do you do for a member level pointer? mpTarget or pmTarget? Do you have a preferred 'pattern' to 'stack' prefixese? – user5151179 Nov 12 '15 at 00:49
  • Thanks! I guess my conventional ordering boils down to the "special modifiers" (member/static/volatile) and then "usage information" (pointer, event), which results in "mp" ordering. As static/volatile/event are usually members I tend to use "s" rather than "ms" to keep things cleaner, so for me it's usually only in C/C++ (where prefixes "mp" or "pp" crop up) that they end up being more than 1 character long. – Jason Williams Nov 12 '15 at 22:00