Kevin Kubasik’s Personal Blog
RSS icon Email icon Home icon
  • The Changing Face of High-Level Programming

    Posted on October 24th, 2007 Kevin Kubasik 6 comments

    Ok, so I’m sure most MS .Net dev’s have already seen these posts far too many times, for the Mono users out there, I have a little treat. While Moonlight and WPF get tons of hype, I think the biggest and most exciting change coming soon to a C# compiler near you is support for lambda expressions, anonymous types, and extension methods.

    Now on the whole this doesn’t sound all that exciting, I mean, before a few months ago, I had never really used lambda expressions to accomplish much beyond pass that unit in an intro to CS class. Individually, there’s nothing to jump for joy about, but when used in conjunction, we can produce startlingly clean and readable code.

    To demonstrate this I’ve whipped up two examples that I was fiddling with as I read a million tutorials. They aren’t fancy XML or Database providers, just some simple (and quite common in my experience) text parsing tasks that have disproportionately complex code. We will use some of the new C# 3.0 features to make far cleaner and more readable code.

    The first example is an exclusion string, or a set of characters that are not allowed in another.

    var illegalchars = "abcdefg"; 
    string testString1 = "Kevin"; 
    string testString2 = "hijkmlppp";

    The ‘old’ way of checking both strings for one of the illegal chars:

     foreach (char c in illegalchars) { 
    if (testString1.Contains(c) || testString2.Contains(c)) 
     Console.WriteLine("illegal char!"); 
    }

    Using awesome new stuff:

     if (testString1.Intersect(illegalchars).Any() 
    || testString2.Intersect(illegalchars).Any()) 
    Console.WriteLine("Linq found it too");

    Our next example is ‘exploding’ or splitting a series of values out of a string (CSV and PSV are common examples of this) into an array:

     string pipeDelined = "Kevin | McCool | Kubasik";

    An old solution might have been (I know we could optimize this, or clean it up, just making a point ;) ):

     List<string> names = new List<string>(); 
    foreach (string s in pipeDelined.Split('|')) { 
    var ts = s.Trim(); 
    if (ts == "") continue; 
    names.Add(ts); 
    } 
    var allNames = names.ToArray();

    Using our cool new C# 3.0 tools, we can change this to the super-sexy:

     var allLinqNames = pipeDelined.Split('|') 
    .Select(s => s.Trim())
    .Where(s => s != "")
    .ToArray();

    While a hardened child of OOP (via C# and Java) might baulk at the new syntax, I think that it can quickly start to grow on a developer. Moreover, it has the distinct advantage of being unambiguous, and makes reading someone else’s dense code much more fluid. 

    I really can’t wait for C# 3.0, and not for those flashy API’s, just the simple syntactical sugar that is already making me lazier by the minute.

     

    6 responses to “The Changing Face of High-Level Programming”

    1. Coming from the Ruby world, I simply can’t imagine *not* having blocks/lambda expressions!

    2. I’ve never really done anything serious in ruby, a little rails work, but not much else. Do they have bindings for gtk/gvfs etc?

    3. I’m just getting into C# to contribute primarily to Banshee. I was wondering what resources you’ve used to learn and stay sharp (pun not intended, but noticed) in C#/Mono. For instance, where’d you find info about this little gem?

      Thanks!
      Andrew Conkling

    4. Well, for starters, MSDN (Microsoft Developers Network) isn’t the enemy, there is a lot of documentation there, and plenty of it is geared at beginners. This post was a little bit of stuff from everywhere, its just when I realized that we were enumerating most everything with linq, why not use that to kill the ugly loop syntax.

      Also, mono-project.org is a great resource, but to be honest, the best way to learn it is find some small task you would normally script or something, and do it in C#.

    5. Good stuff. I couldn’t find a whole lot of just-C# stuff on the MSDN, but I did notice there’s a good bit in the Mono Doc Viewer, as well as a few links in MonoDevelop. Seems like there’s a lot at my disposal!

      Thanks for the help.

    6. Most definitely! Those are some great resources! They are a little light on this stuff as its way new and still incomplete in Mono, but for the stable API’s its a great idea.

    Leave a reply