<?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>Everything old is new again &#187; list-comprehension</title>
	<atom:link href="http://www.svendtofte.com/tag/list-comprehension/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.svendtofte.com</link>
	<description>rantings &#38; scraps on code and web development</description>
	<lastBuildDate>Thu, 27 Aug 2009 22:38:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>C# Generics and anonymous methods</title>
		<link>http://www.svendtofte.com/csharp/c-generics-and-anonymous-methods/</link>
		<comments>http://www.svendtofte.com/csharp/c-generics-and-anonymous-methods/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 20:11:59 +0000</pubDate>
		<dc:creator>Svend</dc:creator>
				<category><![CDATA[CSharp]]></category>
		<category><![CDATA[anonymous-methods]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[list-comprehension]]></category>

		<guid isPermaLink="false">http://www.svendtofte.com/?p=155</guid>
		<description><![CDATA[I&#8217;ve never been much of a C# maven or anything, but I&#8217;ve really fallen in love with this construct in the language. One thing which I guess almost anyone does alot of is building a string, from a list of business objects. Let&#8217;s say you have a list of GUIDs, and you either want to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve never been much of a C# maven or anything, but I&#8217;ve really fallen in love with this construct in the language. One thing which I guess almost anyone does alot of is building a string, from a list of business objects. Let&#8217;s say you have a list of GUIDs, and you either want to display them, or build an SQL string from them for use in a query.</p>
<pre>List&lt;Guid&gt; gs;
string s;

foreach (Guid g in gs) {
    s += "Id='" + g.ToString() + "' OR ";
}

if (result.Length &gt; 0) {
    s = s.Substring(0, s.Length-3);
}</pre>
<p>That&#8217;s some good ole fashioned string fucking, and since I&#8217;ve come over from HTML/JavaScript/(Classic) ASP/PHP, that&#8217;s bussiness as usual by all standards. But I&#8217;ve never liked several aspects of it.</p>
<p><span id="more-155"></span></p>
<ul>
<li>Since you&#8217;re appending to the string always, you need to take the last bit off, or the string turns malformed. That&#8217;s what the <code>if</code> construct does. It can sometimes be hard to connect a large <code>foreach/for</code> block with a succedding <code>if</code> block.</li>
<li>Contains magic numbers, in this case, 3. Can be avoided of course, if the bit that you&#8217;re appending is saved as a variable, and then you take the length of it. But that&#8217;s just more work.</li>
</ul>
<p>Another approach, using generics and anonymous methods is the following</p>
<pre>s = string.Join(" OR ",
        gs.ConvertAll(
            new Converter&lt;Guid, string&gt;(
                delegate(Guid g) {
                    return string.Format("Id='{0}'", g.ToString());
                }
            )
        ).ToArray()
    );</pre>
<p>or, if you&#8217;re running C# 3.0 (my work is still using 2.0), you can do away with most of the syntax for the <code>delegate</code> construction.</p>
<pre>s = string.Join(" OR ",
        gs.ConvertAll(
            new Converter&lt;Guid, string&gt;(
                g => string.Format("Id='{0}'", g.ToString())
            )
        ).ToArray()
    );</pre>
<p>Which is a radical departure from the <code>for</code> loops of yore. Probably debatable if it&#8217;s an improvement in actual performance (<code>string.Format</code> is faster then old string concatenation, but you could use <code>string.Format</code> with the first approach too of course). It&#8217;s a much more functional approach, with no side-effects. But in terms of what is pleasing to the eye, I have no doubts, even if it&#8217;s spanning more lines, it just feels more right. You never end up over doing it, so there&#8217;s never any string to remove. The act of creating the strings you need, and then joining them is also seperated, which feels more right.</p>
<p>The downside is that it does become harder to debug. If you have a line like for instance</p>
<pre>bool b = SomeMethod(GetSomeValue(param1, param2), param3);</pre>
<p>Using either a normal debugger it&#8217;s not easy to break on the method call to <code>GetSomeValue</code>. The same would be the case with the list comprehension at work here. With no assigned value, it&#8217;s a little harder for the debugging. But given the simplicity, it usually proves to be a non-issue for myself.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.svendtofte.com/csharp/c-generics-and-anonymous-methods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
