Skip navigation

I had an interesting discussion today with a coworker about how he has changed his style of programming. As of now we are writing in primarily C# at work. The discussion surrounded using the built in shortcuts of C#. Generally I am in favor of shortcuts but the ones in question seem too VB-like to me. Let me start by saying my first language was C++, which is obviously a very strict language.

The first piece of the discussion surrounded the ‘this’ keyword. My colleague explained at length why he felt the ‘this’ keyword is not necessary. Interestingly enough he used to be strongly in favor of using ‘this’. He argues that the ‘this’ keyword is not necessary because it is implied. However, when he is accessing a static property or method, he always prefixes it with the class it belongs to, even when calling a static from the same class. I think that ‘this’ should always be used where applicable. The biggest issue I see is in the case of a constructor that accepts parameters. Most of the time, these parameters are going to correspond to members of the class. So, you would either have to name the members differently or name the parameters differently if you do not use ‘this’. The second point is consistency. If you prefix static members and methods with the class name they belong to, then you should also prefix local class members and methods with ‘this’. I think C# should be more strict about this because using ‘this’ makes the code a lot more readable and easier to follow.

The second point being argued was the use of the keyword ‘private’. My colleague again argued because it is implied, it shouldn’t need to be used. My argument was that for consistency, you should use the ‘private’ keyword. This is because you have to use ‘public’, ‘protected’  and ‘internal’ so for consistency sake, use ‘private’. Again, I think this should be enforced by the compiler. It seems too much like VB to me, and yes, that is a bad thing.

The last argument was over the initialization of variables. I think variables should always be initialized (again stemming from my C++ upbringing). Yes, .NET handles initialization of most types (I.E. int is initialized to 0). But, some people forget the fact that the string type is a reference type, which means it’s default value is null. In most instances, a null value in a string isn’t what you want. Again, if you’re initializing variables in once place, you should do it everywhere for consistency.

You may think that I am looking too much into this, but simple things like this can really make a difference in large scale systems. Writing clean, easy to follow code reduces the chance of bugs, and makes it easier to locate and repair them. The further we stray from good programming techniques, the worse our code will get. I may sound anal, but again, I’m a C++ programmer at heart.  It’s not like it’s hard to write out a few extra characters in each line of code we write.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: