Sunday, November 18, 2007

How to make your code completely unreadable with .NET 3.5

With the time we got our developers less and less professional by enhancing programing languages and using easier code syntax. However, while we are trying to make things better, we discovered that things are going bad. Let's see following code sample

delegate string BringBeerDelegate(int amount);


static void Main(string[] args)
            Console.WriteLine("Bringing beer using anonymous delegates");



static void AskBarmenAboutBeer(int amount)
            BringBeerDelegate bringBeer = new BringBeerDelegate(BringBeer);

            Console.WriteLine("You got {0}", bringBeer.Invoke(amount));
            BringAnotherBeer(bringBeer, amount+1);


static string BringBeer(int amount)
            return string.Format("{0} beers",amount);


static void BringAnotherBeer(BringBeerDelegate firstPint, int newAmount)
           string res = firstPint(newAmount);
           Console.WriteLine("You got another {0}",res.ToString());

It pretty clear what this code is doing. It creates new delegate named BringBeerDelegate, that actually formats string. Then it invokes the delegate. After it the delegate transferred into BringAnotherBeer method and invokes there. This way it works in .NET 1.1 So far, so good. Let's use anonymous delegate, introduced in .NET 2.0 instead on using method to invoke it. Now, we'll add another method, that creates and invokes the anonymous delegate BringBeerDelegate.

static void AskBarmenAnonymouslyAboutBeer(int amount)
            BringBeerDelegate bringBeer = delegate(int am) { return string.Format("{0} beers", am); };

            Console.WriteLine("You got {0}", bringBeer.Invoke(amount));
            BringAnotherBeer(bringBeer, amount + 1);

Now, calling AskBarmenAnonymouslyAboutBeer with number of pints we want, we'll create and invoke the anonymous delegate. The code less readable (as for me), but never mind, we saved a couple of code lines.

Now, let's write following:

BringBeerDelegate bringBeer = am => string.Format("{0} beers", am);
BringAnotherBeer(bringBeer, ++amount);

What the hell all those => are doing? This is anonymous delegate BringBeerDelegate defined by lambda¹ expression.

small lyrics: ¹Lambda (uppercase Λ, lowercase λ) - the 11th letter of the Greek alphabet. In the system of Greek numerals it has a value of 30. Letters that arose from Lambda include the Roman L and the Cyrillic letter El (Л, л).
Lambda expressions provide a more concise, functional syntax for writing anonymous methods.

Is it really more concise and functional syntax? What do you think, following code doing?

Func<int,string> firstTime = (int am) => { return string.Format("{0} beers", am); };

Func<T..> is generic delegate type for lambdas. That's piece of cake, isn't it? Let's see you to predict what following code will output...

BringBeerDelegate bringBeer = am => string.Format("{0} beers", am);
Func<int, BringBeerDelegate, string> bringBeerFunc = (int am, BringBeerDelegate beer) => { return beer(am); };
Func<int, Func<int, BringBeerDelegate, string>, string> lastTime = (int am, Func<int, BringBeerDelegate, string> func) => { return func(am, bringBeer); };

The other awful thing in new .NET 3.5 is anonymous types. So, great news, VB programmers, there is new king in the village. His name is VAR. Following previous code section, following make our life even more complicated.

var whatBeer = bringBeer;

//At least, either compiler and interpreter knows, that whatBeer variable is of BringBeerDelegate type


Happy and very unreadable coding, my friends.

Source code for this article.

No comments: