Mark Needham

Thoughts on Software Development

C# lambdas: How much context should you need?

with 5 comments

I had an interesting discussion with a colleague last week about the names that we give to variables inside lambda expressions which got me thinking about the context that we should need to hold when reading code like this.

The particular discussion was around an example like 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
var oneFoo = new Foo("bar", "baz");
var anotherFoo = new Foo("otherBar", "otherBaz");
 
new List<Foo> {oneFoo, anotherFoo}.Select(foo => foo.ToString().ToUpper()).ForEach(Console.WriteLine);

I suggested that we could just replace the 'foo' with 'x' since it was obvious that the context we were talking about was applying a function on every item in the collection.

My colleague correctly pointed out that by naming the variable 'x' anyone reading the code would need to read more code to understand that x was actually referring to every 'Foo' in the collection. In addition naming the variable 'x' is quite lazy and is maybe equally bad as naming normal variables x,y and z (unless they're loop indexes) since it is completely non descriptive.

The only real argument I can think of for having it as 'x' is that it makes the code a bit more concise and for this particular example I had to change the name of my first Foo to be 'oneFoo' so that I could use the variable name 'foo' inside the block since other variables in the same method are accessible from the closure.

I'm not sure what the good practice is in this area. I've done a little bit of work with Ruby closures/blocks and the convention there seemed to be that using single letter variables for blocks was fine.

In this case the extra context wouldn't be that great anyway but I think trying to keep the necessary context that someone needs to remember as small as possible seems to be a reasonable rule to follow.

Written by Mark Needham

December 27th, 2008 at 11:15 pm

Posted in .NET

Tagged with , ,

5 Responses to 'C# lambdas: How much context should you need?'

Subscribe to comments with RSS or TrackBack to 'C# lambdas: How much context should you need?'.

  1. [...] C# lambdas: How much context should you need? – Mark Needham [...]

  2. I don't like to use 'x' because it's non descriptive, but I do like its brevity. I typically would use 'o' on object Order or 'os' on object OrderService. It's still brief, but slightly more helpful to recognize the object being used.

    Chris Missal

    29 Dec 08 at 5:02 am

  3. [...] C# lambdas: How much context should you need? – Mark Needham considers one of the important questions when it comes to lambda expressions, how clear is it when you use the (x -> x.blah) notation, so we need to be more careful about our choice of name for x to improve clarity. [...]

  4. The name 'x' is idiomatic for lambda variables, but I would it because I tend to reserve that name for the Cartesian coordinate or the real part of a complex number.

    I think it depends; could you imagine using that lambda expression for items other than 'Foo' instances? Here, you could certainly inagine that, and so I would use a generic name like 'item' or 'elem'. If the lambda expression depended on the variable being of type 'Foo', I would use a specific name.

    Eric Jablow

    30 Dec 08 at 6:43 am

  5. Especially since foo is here part of a much larger line of code, I like foo. In the SmallTalk code I've seen, it was convention to have a role-revealing name for block variables expressing what the block author thought the block was going to receive. Even for very short blocks like

    eachFoo => return eachFoo.ToString();

    Dave Cameron

    1 Jan 09 at 1:50 pm

Leave a Reply