C# — this vs underscore
First of all, I want a full disclosure: I hate the “this” keyword. It makes me think of javascript. To be honest, I think as long as the entire team agrees to a coding standard, (maybe even enforce them with a custom rule set added in SonarQube or StyleCop), it doesn’t make any difference if you use one or the other, but since some people make a big deal out of it (yeah, I guess next will be space vs tab subject), I am writing here my findings.
Let’s start with a little history:
A long long long time ago, VB got in town (the old VB, not the new one). This old programming language was case insensitive (and wasn’t the only one at that time), so it doesn’t understand the difference between “MyVariable” and “myvariable”; in those programming languages, you couldn’t use pascal case or camel case, because, for them, it was the same thing. This is how the underscore/underline appeared in our lives. Programmers started to use it to mark a variable as private.
C# is a case sensitive programming language, why are we still using the underscore prefix, you would ask yourselves, well…to get the answer, let’s dig dipper
In C++, programmers started to use Hungarian notation. Pretty fast they started to adopt the underscore and use it between the type (or intention) and the name of the variable (for example “m_name” meant a member of a structure, “s_name” meant a static member of a class and to go even deeper with examples “m_nAge” meant that the variable is a member of a structure or class and it is an integer). They had an entire coding style, that in my opinion, it was pretty rigid but easy to “move” from a project to another after you learn it. This kind of coding style changed during the years, right now in 2021, even the guys working at CoreFx are using a derivation of it (yeah, in C#). You can find their coding standard here and pay attention to point 3. (.NET Core FX is the team working on performance-critical and multithreading issues from .NET Core)
About the “this” keyword, things are pretty short: Microsoft recommends using it to mark local members. Starting from this, if you take a group of JS lovers/programmers, and ask them to write C# they will say that the “this” keyword should be used all the time.
After the entire “investigation” of “to underscore or not the privates”, the conclusion is that some people like (and use) it but it doesn’t bring any value (they might say that it helps them if they use ef or others 3rd party software, but keep in mind that any packages added to your code, are just tools that should help you, not force you to use a specific coding style or anything else), others don’t like it and don’t use it, or use “this” keyword instead, but in the end, people tend to use what they are used to use. What I can say for sure about underscore, is that (almost) everyone agrees that it should be used when you use BDD and/or GWN in your tests.
I used underscore almost my entire life as a programmer when I wrote C# (and I needed privates), but excuses like “I am used to using it, we should all use it”, or “I think it’s the best because the guys from Microsoft or other projects are using it, so it must be the “correct” standard way of writing code”, or “it’s more clean and easy to read”, are not something that I can agree with. Be honest with yourself and the team you are in, and just say that you like to use it and it’s just a personal thing. There is no standard on the language! In the C# full rule set from Microsoft, a long time ago, when I last used it, it had the rule to use an underscore, but at the same time, in StyleCop, you have another one that says you must not use underscore and use “this”. It all depends on the analysers you have/use in the projects, but do not forget that, all analysers should be customised due to your needs. You shouldn’t use them “as they are”.
I am not saying that underscore is bad, I am saying only that it’s useless. You should use underscore in the programming languages that it was meant for. I am thinking right now about Maslow’s law: “If all you have is a hammer, everything looks like a nail”. Let’s be honest, in our modern IDEs, you need only to select or to hover the variable or parameter and you’ll know everything is to know about it. You even have IntelliCode/IntelliJ and other helping tools, why do you care so much about a notation?
If you are a Microsoft fan, you can find their general conversions here and you’ll have there all the DOs and NOT DOs, but I strongly suggest not to try to copy others and think for yourself. If you are a Microsoft fan, you should think about the fact that you should use “var” only in LINQ and anonymous types. This is what Microsoft recommends also even if, their own programmers break this rule (just look on any source code from .Net core).
Why coding style is important? Usually, people used Visual Studio defaults. They got used to doing that because VS was the only IDE but C# for example, but, right now more are on the market. For example, Rider has its default (since it’s another IDE with more or less R# integrated). If someone from your team use Raider and someone else VS2019 and another one VS2017, it will be chaos, every IDE will suggest something else. You will need to have a common rule set for code analysers not to go mad.
Conclusion:
Microsoft started with its guideline to use “this”. They changed their minds and want “underscore”. StyleCop wants “this”. Other tools are just standing on one or the other side in this “war”.
Advice:
Use whatever you want, as long as your team agrees and you have a common rule set defined.