Using LINQ to reduce code and increase code readability

Written on

Simply put: using LINQ enables you to do more, with less code.

I bought the LINQ pocket reference book (a great book, and a great format) and have read the whole thing several times. One of the examples is this scenario: we have a list of names, and we want to erase the vowels, and then get the resulting names that have more than 2 characters, and order them alphabetically (yes, hardly a realistic every day scenario - but a good example nevertheless!).

string[] names = { "Harry", "Tom", "Dick", "Mary", "Jay" };

The book goes on to say that using LINQ we can solve this problem in both comprehension and lambda syntax. Firstly, my preference, comprehension syntax:

var query = from n in names
            select Regex.Replace(n, "[aeiou]", "")
            into noVowel
            where noVowel.Length > 2
            orderby noVowel
            select noVowel;

// Result: { "Dck", "Hrry", "Mry" }
return query.ToList();

But the same thing can be done (arguably) more elegantly and succinctly using lambda syntax:

var query = names.Select (n => Regex.Replace(n, "[aeiou]", ""))
                 .Where  (n => n.Length > 2)
                 .OrderBy(n => n);

// Result: { "Dck", "Hrry", "Mry" }
return query.ToList();

Nowadays, after I became hooked on LINQ, I wouldn't consider trying to do this without LINQ. But this example got me wondering: just how much time am I saving? How many lines of code have I done away with? I've knocked up another solution without using the System.Linq namespace:

List results = new List();

// Get the names with more than 2 consonants
foreach (string n in names)
{
    string noVowel = Regex.Replace(n, "[aeiou]", "");

    if (noVowel.Length > 2)
    {
        results.Add(noVowel);
    }
}

// Sort the results if necessary
if (results.Count > 1)
{
    results.Sort();
}

// Result: { "Dck", "Hrry", "Mry" }
return results;

By using LINQ we're writing less than half the code we would normally write. It's also important to note several things here (and each one of these points follows on from the one before it):

  1. Both forms of LINQ solve the problem in a single expression, or statement.
  2. The LINQ expression itself is undoubtably more semantically meaningful (to humans) than a cumbersome set of foreach loops and if statements - i.e. we're moving towards code that reads more like English than code!
  3. By having our code more readable, and less of it, as developers we can express our intent faster, more efficiently, and have our code make "more sense" than ever before.

LINQ now makes C# code more readable and more powerful at the same time, and that's a truly awesome thing.


Comments