Mark Needham

Thoughts on Software Development

Lambda in C#: Conciseness v Readability

with 4 comments

One of the things I really disliked when I first came across C# 3.0 code was lambda functions.

At the time I remember speaking to my Tech Lead and expressing the opinion that they were making the code harder to understand and were valuing conciseness over readability.

After a week of reading about the new C# features and understanding how they worked the code was now more readable to me and a lot of the boiler plate code that I had come to expect was no longer necessary.

My favourite example of the power of lambda is when we want to iterate through a collection of items and apply the same operation to every item in the collection.

In normal C# we might do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Foo
{
    private String bar;
    private String baz;
 
    public Foo(String bar, String baz)
    {
        this.bar = bar;
        this.baz = baz;
    }
 
    public override string ToString()
    {
        return string.Format("{0} - {1}", bar, baz);
    }
 
}
1
2
3
4
5
6
7
8
9
var foos = new List<Foo>();
foos.Add(new Foo("bar1", "baz1"));
foos.Add(new Foo("bar2", "baz2"));
 
var fooString = new List<String>();
foreach (var foo in foos)
{
    fooString.Add(foo.ToString());
}

Using the power of C# 3.0 we can change that last for each statement to read something like this:

1
var fooString = foos.Select(f => f.ToString());

This is much more concise but I think the judgement on its readability depends on one’s understanding of the language feature.

One idea I am considering trying is using methods which describe more clearly what the lambda function is doing. This is an idea I came across from Kris Kemper’s post about using similar Ruby language features.

In the example I gave perhaps wrapping the foo.Select(…) in a method called ‘ConvertToStringRepresentation()’ might make it more readable – it’s clearly up for debate though.

When I was learning how lambda worked I found it useful to be able to do the comparison of how you would write code without it and being able to compare that with how you could do it with lambda. I also found having some understanding of how Ruby blocks worked made it easier as well.

Clearly having powerful language features means that a language is much easier to abuse but I think if used sensibly then readability and conciseness need not be mutually exclusive.

Be Sociable, Share!

Written by Mark Needham

November 24th, 2008 at 11:41 pm

Posted in .NET

Tagged with , ,

  • Agreed! I also find it useful to define custom LINQ operatorw that will simplify the expression required. Join, for example, is a tedious operator to use because it requires three expression. http://odetocode.com/Blogs/scott/archive/2008/11/16/12343.aspx

  • Nate

    var foos = Foos.with(new Foo(“bar1”, “baz1”), new Foo(“bar2”, “baz2”));
    var fooString = foos.ToString();

  • Nate

    Whoops, posted too quickly and didn’t convert to the same output as you. My point still stands though. I tend to push usages internal to another object as I find that in addition to the readability problems (I was looking at the lambda version when I wrote the code to convert it to a string — didn’t read it properly), duplication also creeps out from these kinds of statements.

  • Richard

    While I know it’s a contrived example to make what I’d consider a valid point (and which I agree with), variable naming isn’t helping with the readability either in this case –

    var fooStrings = foos.Select(foo => foo.ToString());

    has a lot more chance of being understood by the unfamiliar than the slightly daunting algebraic feel of the single letter variables. I’ve always used => as a general “Go from this to that” sign in my notes so found it relatively straightforward to pick up.