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”);
AskBarmenAboutBeer(2);}
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.
You may also be interested with:
November 18th, 2007 · Comments (18)
18 Responses to “How to make your code completely unreadable with .NET 3.5”
Leave a Reply
Discover other tags
My tools
- .NET Framework Detector
- Duplicate images finder
- Exchange Security Policy for Windows Mobile Devices Fix
- Gas Price Windows Vista SideBar gadget
- Israel Traffic Information Windows Vista SideBar gadget
- Localization fix for SAP ES Explorer for Visual Studio
- LocTester
- RTL and LTR in Windows Live Writer
- Silverlight controls library
- Snipping tool integration plugin for WLW
- USB FM receiver library
- Vista Battery Saver
- WebCam control for WPF
- Windows Live SkyDrive attachment for Windows Live Writer
- Wireless Migrator
- WPF Virtual Keyboard




January 1st, 2009 at 12:28 am
Me, personally, not a big fan of labdas . However, it looks like new blogger M. Orçun Topdağı is
January 1st, 2009 at 12:28 am
Did you try to cast anonymous types (vars)? You are. However, you never was able to pass var from one
January 1st, 2009 at 12:28 am
aL,
1) I’l not. Actually, I never proof read my posts. I just write – this is my way of writing blog
2) Instead of writing "I can predict", just predict it
BTW, it uses all arguments there.
3) Regarding C#, you are right, I do not know C#, as well as I’m not programmer at all – here the proof blogs.microsoft.co.il/…/70-552-should-be-removed-or-at-least-changed.aspx
4) You are not have to read my blog, really, you dont, ‘cos I’m awfull unprofessional in any context and my english is bad
January 1st, 2009 at 12:28 am
did you proof read your post at all..? thait has to be the most poorly written thing ive read in a long time..
ok fine, i can get over the grammar but your example of a "bad" lambda is just silly.. first of all you ask if i the reader can predict what the code:
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, BringBeerDnelegate, string> func) => { return func(am, bringBeer); };
will output. well yes actually, i can.. not a damn thing. you never call lasttime so it wont output anything. its an unrealistic example anyway because you declare several arguments that you never use.
and your bash on the var keyword? there isnt even an argument as to why it will make your code unreadable!
you dont seem to know very much c# and that whould explain why you seem to claim most language features that differs from c are "unreadable"
i'll never visit this blog again so you can flame me all you want. but you should know that your laughable examples and your apparent complete ignorance of the workings of c# makes for an awfully unprofessional blog.. atleast in the context of c#.
the horrible english doesnt help either
January 1st, 2009 at 12:28 am
Remember new features, that make your code unreadable ? A couple of days ago, CLR team released first
January 1st, 2009 at 12:28 am
Here , one of visitors, begun a discussion about good and bad programmers. Here , I wrote about absolutely
January 1st, 2009 at 12:28 am
> I have neither time nor wish to teach programmer. I believe, that those, who want to write code, must understand what they are doing and not be "code monkeys".
I would agree to that, but then your problem vanishes anyway: if programmers know what they are doing, then they won't produce cryptic code like the "bad example" code in your article. If they do anyway, they should be slapped around the head and forced to do maintainance programming for at least a year.
> purchases.SortBy(p => p.Price); statement is very very expressive, however, I am not speaking about "simple patterns". …
Me too: You wouldn't need lambdas for "simple patterns". But the more complex your task gets, the more helpful lambda expressions will be.
> Let's imagine, that p.Price is not number, but object with locale formatting.
Yes, let's: What options would you have then? You could:
- write your own sorting routine that uses the right comparison function (bad, lots of duplicate code)
- Use a compare-function-pointer (as in C's qsort or C++'s sort functions) or a delegate pointing to a static function. (bad, violates code locality.)
- Use an anonymous delegate (not that bad, but verbose; and delegates are a redundant feature, so they will (hopefully) go away some day.)
- Or, in C# 3.0, you could finally use a simple lambda expression:
customers.Sort( (l,r) => l.Name.Compare(r.Name, Globalization.CultureInfo.SomeWeirdEuropeanCulture);
January 1st, 2009 at 12:28 am
Niki,
I understand you’r statement. However, how many production code wrote, by using Scheme? It’s really cool academic appropach, but still it’s very far away from being production.
I have neither time nor wish to teach programmer. I believe, that those, who want to write code, must understand what they are doing and not be "code monkeys".
purchases.SortBy(p => p.Price); statement is very very expressive, however, I am not speaking about "simple patterns". Let’s imagine, that p.Price is not number, but object with locale formatting. Can you imagine how your code will work? Can you know if in your statement sorting will be ascended or decended? You can not.
I’m oldfashined, so I think, that events are bad, ‘cos they are much worth then system messages. And that’s exactly my point.
January 1st, 2009 at 12:28 am
That's exactly what I mean, when writing such articles . Translation for English speakers: "Wake
January 1st, 2009 at 12:29 am
I just have to reinstall VS2008 because I keep getting weird "unexpected errors" (very helpful error message, by the way. thank you, microsoft!). So I have a few minutes to write down a reply:
> Niki, no it is not my statement. My statement is that we are studing developers to write bad code, by adding more and more unnessesery features to the programming language.
That's your conclusion, but I still don't see how you get to that conclusion. Yes, of course, the feature is kind of redundant, but not more redundant than for-loops, foreach-loops, do-while-loops, events, switch-case or properties are. Yes, there is potential for misuse, but IMHO you can do far more damage e.g. with events, which can lead to serious spagetti code.
I see two possible conclusions out of this, either:
a) Use a minimalist language like Scheme, that does not have all those redundant features. I like Scheme, but unfortunately I'm quite alone with that, most companies seem to prefer "rich" languages. (And guess what: Yes, Scheme does have lambdas…)
b) Teach your programmers to use the features of the language wisely, not to obscure the meaning of the code they're writing, but to clarify it.
> In Python 3000, for example is was decided to remove lambdas from the language and replace them with anonymous delegates.
I think I read a statement from Guido van Rossum (the man behind Python) that lambdas will stay in the language, but that might be outdated. Anyway, the reason why there's discussion about lambdas in Python are largely syntactic: they simply don't fit into the indentation-oriented block structure. That's not a problem that in C#.
> So, are you really thinking , that lambdas are good?
Yes, of course I do. Why? For one thing, I have some mathematical background, so I've found lambda calculus intuitive long before anyone thought C# might get lambda expressions some day. Also, I know a bit of higher-order-programming, and know how much it can simplify development if used correctly. And I can think of code like this:
purchases.SortBy(p => p.Price);
I don't see how I could make this code any clearer. In .NET 1.1 (or in C++), I would have a static function like "GetPrice" somewhere far aways form the SortBy-call, which is of course bad because it destroys code locality. In C# 2.0, I could to do something similar with an anonymous delegate, which does the same thing, but makes the code hard to read, because it is far more verbose, and also less flexible, because it cannot be used as an expression. (I am hoping that anonymous delegates will be removed in some future version of C#.)
January 1st, 2009 at 12:29 am
I agree with the point you are trying to make. My feelings are reflected by this post on LINQ (the reason anonymous types and lambda expressions were implemented) that I made only a few days ago …
http://www.angelosp.com/…/TechEd-Wisdom-%e2%80%93-Still-not-convinced-about-LINQ.aspx
January 1st, 2009 at 12:29 am
Pingback from Daily Dose of Links – 20071119 « Daily Geek Bits
January 1st, 2009 at 12:29 am
Niki, no it is not my statement. My statement is that we are studing developers to write bad code, by adding more and more unnessesery features to the programming language.
In Python 3000, for example is was decided to remove lambdas from the language and replace them with anonymous delegates. Regarding Ruby, delegates works as block expressions. They are working very hard to add delegate support to the language += and -= expressions.
So, are you really thinking , that lambdas are good?
January 1st, 2009 at 12:29 am
I agree with you, I hate anonymous types. I expect to frustrated when I start reading people's code in .net 3.5.
January 1st, 2009 at 12:29 am
Your point seems to be, if a feature can be used to write code that's hard to understand, then the feature is bad. Is that right?
Wouldn't it make more sense to learn those new programming language elements, and see how they can be used to make code more readable?
BTW: Maybe you should have a look at languages that are usually regarded as "elegant", like Lisp, Python, Ruby, Haskell or ML. Guess what? They all have lambda expressions!
January 1st, 2009 at 1:06 am
[...] personally, not a big fan of labdas. However, it looks like new blogger M. Orçun Topdağı is. He wrote three posts (1, 2, 3) [...]
January 1st, 2009 at 1:12 am
[...] Here, one of visitors, begun a discussion about good and bad programmers. Here, I wrote about absolutely unnecessary and not relevant questions to good programmers. Someone things, that good programmers should write good programs. However, I want to interpose my objections to the statement. And this why. [...]
April 21st, 2010 at 11:00 am
Nice blog you have… looking forward to read your next post