Thursday 4 July 2013

Underscore or this - C# best practices

Background

C# and .NET have been around for a long time now and the standards and abilities of the language just keep getting better and better. However there is no getting away that some of the more core principles of this language originated from C and C++ created many many years ago. When C and C++ was first created tools were basic and the IDEs around were not very advanced and often programs were coded in notepad or similar tools. For speed and productivity common conventions came about that allowed developers identify code easier.

  • Prefixing the type on to a variable e.g. intAge
  • Adding underscores to internal class variables to indicate they are as such

These were perfectly fine back in the day when working with poor tools but now with Visual Studio and all of the features we have in modern IDEs a lot of this has changed

Underscore vs this keyword

Now comes my main gripe of standards that seem to be lingering on and one, mostly from C and C++ developers and incorrect information. C# has a keyword that has been in it a long time call the ‘this’ keyword which has the following benefits/features

  • Indicates that something you are accessing is from the instance of the class you are using
  • As soon as you type it Intellisense is narrowed down to only instance level code constructs

This feature has been built in to the language and Visual Studio to support having a keyword to replace the need to use anything artificial.

But Microsoft use underscore argument

I have the above argument as a reason why we should be using underscores but here is my responses/reasons why they are wrong

  • Their argument is based upon underscore in the source code. If you look at newer source code (.NET4)
  • There is far less use of underscores
  • Coding everywhere uses ‘this’ keyword
  • Looking on forums you can find Microsoft employees stating that they do not use underscores but still some of the developers that have been around a long time do
  • http://msdn.microsoft.com/en-us/library/ms229045(v=vs.100) which shows .NET 4 suggest conventions lists
  • Do not use underscores, hyphens, or any other nonalphanumeric characters. as one of the standards, so Microsoft are most definitely not suggesting the use of underscores
  • Why the hell would they add the ‘this’ keyword if it wasn’t to be used
  • Stylecop the internal tool created by Microsoft to check code for standards compliance doesn’t like underscores but likes the ‘this’ keyword

Summary

As you can tell I hate underscores and developers trying to tell me it’s clever when it clearly isn’t. We aren’t in the coding dark ages anymore so there isn’t any reason to name things non logically with random crap characters all over the place. The ‘this’ keyword was added in to the language for a reason so use it. Basically if I see code with underscore I will assume your old or ignorant.

Thanks to http://scottreed.eu/csharp/underscore/

No comments:

Post a Comment