David Stone's Blog

I'm open to suggestions for a subtitle here! (Really!)

C# Getting Unreadable?

Mike Gunderloy remarked, in today's Daily Grind,

"Ian Griffiths explains some of the advanced syntax of C# 3.0. I freely admit that my eyes glazed over. As far as I'm concerned, C# is now going the way of C++: it's got syntax that I will never, ever use and is well on its way to complexifying its way out of my life."

I find it interesting that people keep saying this. There was one guy at the PDC during the Linq End-To-End panel that was very convinced that C# was quickly becoming too complex, and ergo unreadable. He stated that he believed that the introduction of generics made the BCL look too much like C++’s STL and made the code produced too complex for the average developer to learn. From there, he transitioned into voicing concern over the new languages features in C# 3.0.

I, personally, can’t understand this aversion to Linq and the new C# 3.0 language features. Let’s look at the following code, shamelessly stolen from Ian Griffith’s post. First, we’ve got the C# 2.0 anonymous methods version of it. (I’m not even going to bother reproducing the C# 1.0 version. That would be pointless. Whidbey is here, people. Get used to it.)  

public static void ShowDivisible()
{
    for (int d = 1; d < 10; ++d)
    {
        Console.WriteLine("Numbers divisible by " + d);
        PrintMatchingNumbers(1, 10,
            delegate(int i)
            {
                return (i % d) == 0;
            });
    }
}

Now I think we can all agree that anonymous methods are pretty cool. It saves a lot of work when constructing one time use methods just to fulfill a delegate. Also, I believe the code is more readable than it’s C# 1.0 counterpart due to the fact that I don’t have to go hunt down a method somewhere else in my codebase. I’ve got it right there in the place where I’m using it. So, having seen that, lets take a look at the C# 3.0 version of the same code.

public static void ShowDivisible()
{
    for (int d = 1; d < 10; ++d)
    {
        Console.WriteLine("Numbers divisible by " + d);
        PrintMatchingNumbers(1, 10, i => (i % d) == 0);

    }
}

Now, as I said, I personally think this is much more readable. The new lambda expression syntax puts the focus more on the actual intent of the code (the fact that I only want to print out numbers where i % d == 0) than the construction of the delegate. In addition, thanks to C# 3.0’s new local variable type inference abilities, I don’t have to worry about how to construct my delegate and what the signature looks like. The compiler just knows. I get all this at the cost of…what? Learning a new => operator? If it means that I don’t have to worry about those delegate signatures anymore, then count me in. I’m a big fan of the fact that I can let the compiler worry about that for me.

What I really can’t understand is the aversion to the new language syntax for the standard query operators. They’ve introduce querying ability baked directly into the language that supports query operations over anything that implements IEnumerable<T>. First, you get one query language for any type of data, be it relational, XML, in-memory collections, etc. Second, that query language is not only going to be full of IntelliCrack goodness., but it will be checked for you at compile time, eliminating the need for nasty errors that crop up at runtime when SQL Server decides you don’t know anything about how to write SQL queries. Third, there’s a very short migration path from SQL (Unless you’re using VB 9, at which point there’s almost no migration path) or XQuery, so it’s not that hard to learn the new keywords and syntax.

Maybe someone from the other side of this argument would like to explain to my why they think these new features are unreadable, too complex, or why you wouldn’t use query capabilities built into the language. And, by the way, if you think that C# is too complex because it’s got these things in it, where are you going to go? Because VB 9 is going to have them as well.

Comments

. said:

Yes, C# is too complex already. Most people that write .NET blogs are not average developers, they are geeks that spend their whole day reading C# books and other blogs. The average guy is dying on all this new C# and VB feature load.
# October 4, 2005 3:39 AM

Peter Bridger said:

New features are great, if I've the time and use for them, I will learn them.

If not, I don't need to use them.

The more the language grows and develops, the better for everyone.
# October 4, 2005 5:35 AM

Paul Watson said:

It seems both sides have a point. The example above is quite simple and is easily readable (both are). What happens when the anonymous method gets to be a complex, real world tangle of code though, how readable is it then?

Maybe having the functionality is useful for when the use is simple and one should know not to use it when the use is complex as it becomes unreadable. But then should a language clutter itself up just to cater for simple cases? If you let in this change what about other changes, ones you may not agree with?
# October 4, 2005 5:37 AM

Guy Murphy said:

My preference is C# and I welcome new additions to the language. I'm aware however that some may find it over the top, especially in the future, hence for a framework I'm currently developing while I'm using (as per normal) C# for the core framework I'm considering BOO (http://boo.codehaus.org/) for action handler exntentions. The reason for this is more junior developers on the team, or those developers whose core skills don't include C# may find BOO easier to read.... BOO btw is strongly based upon Python syntax.

Generics are not a lightweight feature. They bring an awful lot to the table and therefore are in my view more than worth any syntax baggage they bring. They are a lot more readable and maintainable that custom built collections etc.

Lastly, and perhaps unkindly, C# and the .NET framework has to be able to service the profesional programmer/developer/project. It can't be picthing to the lowest common denominator. The working programmer whether .NET, Java or C++ can reasonably be expected to keep abreast of new features at a reasonable pace. If the average chap can't, I'm genuinely sorry to say, that's tough.... this isn't about being a geek, it's about being a working developer.
# October 4, 2005 9:14 AM

Lynn said:

I don't buy the notion that C# is too complex or becoming to complex . Not at all.

What qualifies as more complex any way? The Linq aspects of C# will back anything that's IEnumerable filterable with an SQL like syntax. That's much more efficent than writing a filter or using the 2.0 predicate system, which I don't even follow yet. That the same query engine can be used for o/r mapping, most likely winfs, and other things not yet imagined it pretty cool as well. That makes leaning the query engine very worth while.

Also, nobody learns a language at once if they're new to progamming. Learning the BCL, OOP, SQL and VB and/or C# takes a while. Months. Years. If you're already a programmer, these enhancements make the language just much more rich.








# October 4, 2005 11:47 AM

Edwin said:

The C# team has been designing the language from the start being careful not to turn it into a monster. Although I welcome new additions to the language, I guess it is also becoming a monster, probably will become too complex in the near future
# October 4, 2005 12:55 PM

Shog9 said:

For what it's worth, i'm a "light" C# user at best, and from my perspective the worst (for readability) features in C# were already there in version 1. Most of what's come since has been aimed at trying to fix the problems that became obvious with heavy use - sure you can come up with an example of ugly code using the new features, but you don't have to look very far to see plenty of non-contrived examples in production today.

Then again, i'm of the opinion that the worst (least readable) C++ code out there is the stuff written for early '90s (no templates, STL, etc.) compilers...
# October 4, 2005 7:20 PM

Keith Hill said:

"Lastly, and perhaps unkindly, C# and the .NET framework has to be able to service the profesional programmer/developer/project. It can't be picthing to the lowest common denominator."

I whole heartedly agree with this! After talking some to Anders at the PDC I also realize that he doesn't add anything to C# lightly. He is very concerned about C# becoming overly complex. IOW, any new functionality going into C# has to have a really good ROI.
# October 4, 2005 10:54 PM

Steve Sweeney said:

I can see how the language is being 'evolved' with the best of intentions.... However, my gripe goes back to the 'Code Complete' days where readibility is favoured over writability of code.

I have to say that the examples on the web (i.e. the link currently on the VS2005 start page) that compares non-LINQ/verbose syntax against the new .NET 3.0 syntax may reduce the actual lines of code needed to achieve certain functionality. But it does so at the expense of readibility. Statements just seeem more ambiguous to me and I would have to study a given line of LINQ-syntax much more closely than I would scan the .NET 2.0 equivalent.

Just my ten-cents... perhaps after I've reviewed 10000+ lines of LINQ code it will be much clearer, but compared to my earlier experiences learning .NET 1.1 and 2.0, this is a marked downturn in an otherwise enjoyable journey.

P.S. I've been doing (full) C++ for 7+ years now and I'm not averse to templates, predicates etc... :)

# May 29, 2007 4:59 AM

rov said:

It's really getting more and more complex with every new  release. May be it's better not to try to make all-in-one language

# September 14, 2007 3:16 AM

Englezul said:

Suck it up!

Too complex? You have to be kidding me. You're only saying that cause you're too lazy to learn the darn thing. English is much more complex than c# and you're not complaining about that.

If you can't handle it. It's fine. Quit your job. I'll take it. If it pays well enough.

# March 6, 2008 5:01 PM

... said:

Gute Arbeit hier! Gute Inhalte.

# March 4, 2009 3:26 AM

... said:

Dies ist ein gro�er Ort. Ich m�chte hier noch einmal.

# March 9, 2009 3:19 PM

robert said:

Both c++ and C# have features that are overly complex. Features such as function and operator overloading, inheritance, polymorphism in my opinion should not be used unless totally needed.

The languages totally disregard common sense.  

Bottom line -

The negative - the costs of maintaining OOP languages is off the charts high

The positive - guaranteed employment for years to come.

To the creators of C++ and C# - thanks for creating all the complexity      

# January 19, 2010 10:45 AM

kikus said:

интеретсный блог почему только так мало читателей на нём

# June 13, 2010 7:49 AM

Pupeevetlylit said:

Доброго времени суток,  

Хочу представить вам прозелит лавка курительных смесей

сайт магазина http://spice-family.ru  

3г микса Relax - 1,500 р. + доставка (ems, pony get across)  

Сообразно вопросам опта отмечать поурочно в скайп - FomaX2

# September 2, 2011 8:11 PM

ReageasencyuI said:

Что для Вас Юмор?    

<a href=http://xn--c1aeb8eua.xn--p1ai/>сценки на юбилей и картинки</a>

# January 31, 2012 8:04 AM

ReageasencyuI said:

До чего христиане нудные! Юмор то хоть есть у них?    

<a href=http://xn--c1aeb8eua.xn--p1ai/>видеодомофоны цены где купить</a>

# January 31, 2012 1:07 PM

boumperourogs said:

http://tor4.su - борьба с зависимостью

# February 26, 2012 7:28 PM

boumperourogs said:

не видишь выхода из ситуации? тебе всегда помогут советом бывалые http://tor4.su

# March 25, 2012 2:39 PM

LE said:

@Englezul : Spoken like a true, class 1, ass.

These are the people, no matter how smart they are, I don't want to work with or hire.

# November 11, 2012 9:57 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)