The Coding Monkey

Wednesday, September 28, 2005

Killing Readability?

There's been a lot of action recently in blogs over at Microsoft talking about C# 3.0. I won't even begin to discuss how silly this is since C# 2.0 isn't even out of beta yet... but anyway. The coolest feature to be included is something called Linq. If you even looked at C-Omega, then Linq ought to look very familiar. The basic idea is to allow you to inject SQL type language structures directly in your code, make data calls more type safe, and have result sets be first class objects in your code. This basically gets rid of funky data layers that try to meld between your database and business objects. It's a very cool idea. Cyrus has been talking a lot about here and here.

Linq has naturally been getting a lot of the attention. What's been getting less attention is another new feature called "Implicitly Typed Local Variables". Cyrus recently blogged about it here. The idea is to take code like this:

int i = 5;
string s = "Hello";
double d = 1.0;
int[] numbers = new int[] {1, 2, 3};
Dictionary<int,Order> orders = new Dictionary<int,Order>();

And turn it into code like this:

var i = 5;
var s = "Hello";
var d = 1.0;
var numbers = new int[] {1, 2, 3};
var orders = new Dictionary<int,Order>();

To be very clear. Var is not like object! Variables declared with var will still be of their specific type. The difference is that now the compiler will figure out what type it is for you, and generate the right type under the hood. So the generated IL for both would be the same. In other words, it's just syntactic sugar.

So this makes for less work which is a good thing right? Here is my fear. I think it will cut down on readability. As the .NET languages progress, Microsoft continues to add things to the language that require you to make use of intellisense to figure out. If I see the following:

var foo = BarWhichReturnsSomething();

I have to use intellisense to figure out what foo really is. Call me old fashioned, but I still actually print out code on paper (I'm no environmentalist) when I review code... especially other people's code. You can't exactly hold your mouse over a piece of paper and have a tooltip pop up can you?

I find this feature especially strange since many of the language requirements in C# are built around making code explicitly more readable. My understanding is that Anders Hejlsberg (the father of C#) was a stickler for this sort of thing. For instance, if you declare a method as taking an out parameter, you have to also put out on the variable when you call the method. When you override a virtual method in an inherited class, you have to put override on your method. Neither of these to things are actually needed (the compiler is perfectly capable of inferring both), but C# requires it because it makes code more readable, and therefore reduces bugs (hopefully).

So why the change in this paradigm? Where are you Anders?

0 Comments:

Post a Comment

<< Home