Making c# lambda expressions more readable

How often do you use lambda expressions? I use them a great deal, mostly when I am making method assertions in Rhino Mocks. Their use is also on the rise with the popularity of fluent interfaces growing. If you do the bare minimum, which i see a lot, the expression can be somewhat cryptic.

Less readable:

Users.Find(x => x.Id == selectedUserId)

I am guilty of doing this as well, without even realizing. Maybe I am just being nit picky.

I think it is much more readable if you use something more descriptive than some arbitrary letter in the alphabet.

More readable:

Users.Find(user => user.Id == selectedUserId)

This becomes much more useful when you are coding more complex lambda expressions. One example is when making a method assertion using Rhino Mocks.

Less readable:

_userRepository.AssertWasCalled
(
x => x.Save(newUser),
o => o.IgnoreArguments()
);

With that assertion we have two arbitrary letters, what the heck does ‘x’ and ‘o’ represent. Are we talking about hugs and kisses. I don’t think so.

So to remedy this, lets change ‘x’ to ‘userRepository’ and ‘o’ to ‘method assertion’. I believe these terms will make the assertion much more readable and concise.

More readable:

_userRepository.AssertWasCalled
(
userRepository => userRepository.Save(newUser),
assertionOptions => assertionOptions.IgnoreArguments()
);

With that little change it is much easier to understand what is being asserted and what options are being set on that assertion.

The hardest part is breaking the habit of using arbitrary letters. In the long run a more descriptive expression improves the readability of the code. It will also decrease the amount of time it takes a new person to understand the lambda expressions in the code base.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Sean Biefeld

Hello all, my name is Sean Biefeld. I graduated from Baylor University with a BBA in Management Information Systems. I am currently working for McLane Advanced Technologies . The opinions expressed by me, however accurate they may be, are not necessarily those of my employer. I have been a developing software professionally since 2004. My primary development focus is on HTML, CSS, JavaScript and C#. I dabble in a little PHP and Ruby. This blog is a forum for me to post my experiences, ideas, rants, and thoughts as I traverse the mountain of life.
This entry was posted in C#, Lambda Expressions, Rhino Mocks, Unit Test. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://blog.decayingcode.com Maxime Rouiller

    Without being too critical, if developers don’t do that… they are really trying to put a feet inside their mouth.

    The only time where it is acceptable to use single letter variable is when you are looping a single “for” loop. As soon as you get something more complex than that… you MUST pick something more descriptive.

    Lambdas are no exceptions. No excuses accepted.

  • http://OdeToCode.com/blogs/scott/ Scott Allen
  • http://imws.com/ Zach Shewmaker

    I’ll admit I have done the one letter variable in lambda expressions. I don’t know what it is about lambda that makes us forget what we learned about readable code (maybe all the early examples from Microsoft?).

    I had a moment where I was trying to present LINQ to developers at my company and I was using a useless single character variable name. They were confused… Luckily, a meeting conflict gave me a chance to give the presentation a second time later. Using a valuable name that second time made a HUGE difference in their understanding of what was happening.

  • The Dude

    Sorry dude but writing readable code has to got everything to do good practice not specificly with lambda expressions. It’s not even worth a post anymore.

  • Jerry Nixon

    Nope. Don’t agree. Sorry.

    _List.Find(x => x.Id == 1);

    That’s freakin’ readable.

  • Stephney
  • http://ohwovthfsntf.com/ ielmwjtw

    JgCjcg ewoixxvgbnmr, [url=http://yyddvwmtaldx.com/]yyddvwmtaldx[/url], [link=http://sgdovgkvumfy.com/]sgdovgkvumfy[/link], http://nknniviplmke.com/

  • http://pulse.yahoo.com/_6RRBJRRZARTKQLNFDIQTTNF4X4 Somesh

    Thanks for the information or tips you provide to us about lambda expression in C#. I have found another nice post related to this post which also explain nicely, you may check out that post by visiting following link.

    http://www.mindstick.com/Blog/181/Lambda%20Expression%20in%20c

    so lastly thanks to everyone for their nice post which help me lot and save my time.

  • http://www.facebook.com/profile.php?id=577075530 Chris Nevill

    I cannot get why people are using single letters. Even reading books that introduce Lambda expressions I’m seeing this.
    Really bizarre… Good post.

  • http://wisentechnologies.com/it-courses/.net-training.aspx .Net Training in Chennai

    There are two types of lambda expression

    1. expression lambda
    2. statement lambda