Los Techies : Blogs about software and anything tech!

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.

Kick It on DotNetKicks.com
Posted Feb 19 2009, 07:35 PM by Sean Biefeld

Comments

Maxime Rouiller wrote re: Making c# lambda expressions more readable
on 02-19-2009 8:28 PM

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.

DotNetShoutout wrote Making c# lambda expressions more readable - Sean Biefeld's Blog -
on 02-19-2009 9:05 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Scott Allen wrote re: Making c# lambda expressions more readable
on 02-19-2009 11:17 PM
Zach Shewmaker wrote re: Making c# lambda expressions more readable
on 02-20-2009 12:39 AM

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.

igorbrejc.net » Fresh Catch For February 20th wrote igorbrejc.net » Fresh Catch For February 20th
on 02-20-2009 8:01 AM

Pingback from  igorbrejc.net » Fresh Catch For February 20th

Excel Formulas and the DLR – Part 1 « Chris Cavanagh’s Blog wrote Excel Formulas and the DLR – Part 1 « Chris Cavanagh’s Blog
on 02-20-2009 11:14 AM

Pingback from  Excel Formulas and the DLR – Part 1 « Chris Cavanagh’s Blog

Arjan`s World » LINKBLOG for February 20, 2009 wrote Arjan`s World » LINKBLOG for February 20, 2009
on 02-20-2009 3:07 PM

Pingback from  Arjan`s World    » LINKBLOG for February 20, 2009

The Dude wrote re: Making c# lambda expressions more readable
on 02-21-2009 9:32 AM

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.

Making c# lambda expressions more readable - Sean Biefeld’s Blog - « Head.SmackOnTable(); wrote Making c# lambda expressions more readable - Sean Biefeld’s Blog - « Head.SmackOnTable();
on 02-27-2009 1:45 PM

Pingback from  Making c# lambda expressions more readable - Sean Biefeld’s Blog - « Head.SmackOnTable();

jack johnson: LINKBLOG for February 20, 2009 « HopeyArd wrote jack johnson: LINKBLOG for February 20, 2009 « HopeyArd
on 02-28-2009 3:58 PM

Pingback from  jack johnson: LINKBLOG for February 20, 2009 «  HopeyArd

Jerry Nixon wrote re: Making c# lambda expressions more readable
on 08-15-2009 1:25 AM

Nope. Don't agree. Sorry.

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

That's freakin' readable.

Stephney wrote re: Making c# lambda expressions more readable
on 09-25-2009 3:59 PM

Good Post. Check this out as well.

savitav.spaces.live.com/.../cns!858DCC9E51C3ADD!136.entry

ielmwjtw wrote re: Making c# lambda expressions more readable
on 12-06-2009 5:39 AM

JgCjcg  <a href="ewoixxvgbnmr.com/.../a>, [url=http://yyddvwmtaldx.com/]yyddvwmtaldx[/url], [link=http://sgdovgkvumfy.com/]sgdovgkvumfy[/link], http://nknniviplmke.com/

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems