<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Musings&#60;Biefeld&#62; &#187; Unit Testing</title>
	<atom:link href="http://sbiefeld.com/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://sbiefeld.com</link>
	<description>- curiosities of development, life, the universe and everything -</description>
	<lastBuildDate>Thu, 17 Nov 2011 04:01:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Making c# lambda expressions more readable</title>
		<link>http://sbiefeld.com/2009/02/making-c-lambda-expressions-more-readable/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=making-c-lambda-expressions-more-readable</link>
		<comments>http://sbiefeld.com/2009/02/making-c-lambda-expressions-more-readable/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 23:44:02 +0000</pubDate>
		<dc:creator>Sean Biefeld</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Lambda Expressions]]></category>
		<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[Rhino Mocks]]></category>

		<guid isPermaLink="false">http://sbiefeld.com/?p=17</guid>
		<description><![CDATA[How often do you use lambda expressions?  I use them a great deal, mostly when I am making method assertions in Rhino Mocks.  If you do the bare minimum, which i see a lot, the expression can be somewhat cryptic. Less readable: Users.Find(x =&#62; x.Id == selectedUserId) I am guilty of doing this as well, [...]]]></description>
			<content:encoded><![CDATA[<p>How often do you use lambda expressions?  I use them a great deal, mostly when I am making method assertions in <a href="http://ayende.com/projects/rhino-mocks.aspx">Rhino Mocks</a>.  If you do the bare minimum, which i see a lot, the expression can be somewhat cryptic.<br />
<br />
Less readable:</p>
<pre class="csharpcode">Users.Find(x =&gt; x.Id == selectedUserId)</pre>
<p>I am guilty of doing this as well, without even realizing.  Maybe I am just being nit picky.<br />
<br />
I think it is much more readable if you use something more descriptive than some arbitrary letter in the alphabet.<br />
<br />
More readable:<br />
</p>
<pre class="csharpcode">Users.Find(user =&gt; user.Id == selectedUserId)</pre>
<p>This becomes much more useful when you are coding more complex lambda expressions.  One example is when making a method assertion using <a href="http://ayende.com/projects/rhino-mocks.aspx">Rhino Mocks</a>.<br />
<br />
Less readable:</p>
<pre class="csharpcode">_userRepository.AssertWasCalled
(
     x =&gt; x.Save(newUser),
     o =&gt; o.IgnoreArguments()
);</pre>
<p>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.<br />
<br />
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.<br />
<br />
More readable:</p>
<pre class="csharpcode">_userRepository.AssertWasCalled
(
     userRepository =&gt; userRepository.Save(newUser),
     methodAssertion =&gt; methodAssertion.IgnoreArguments()
);</pre>
<p>With that little change it is much easier to understand what is being asserted and what parameters are being set on that assertion.<br />
<br />
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.</p>
]]></content:encoded>
			<wfw:commentRss>http://sbiefeld.com/2009/02/making-c-lambda-expressions-more-readable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

