<?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>Daniel Jonathan McGreal</title>
	<atom:link href="http://www.djmcgreal.co.uk/feed" rel="self" type="application/rss+xml" />
	<link>http://www.djmcgreal.co.uk</link>
	<description>CV and application materials</description>
	<lastBuildDate>Thu, 19 May 2011 10:56:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Faking method chaining paradigm in Java</title>
		<link>http://www.djmcgreal.co.uk/archives/150</link>
		<comments>http://www.djmcgreal.co.uk/archives/150#comments</comments>
		<pubDate>Thu, 19 May 2011 10:53:32 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.djmcgreal.co.uk/?p=150</guid>
		<description><![CDATA[Method chaining is something I&#8217;m used to seeing in the C++ world, possibly because the programmers that live there are more used to passing around references. In Java I see this used much more infrequently and I&#8217;ve come to miss it. When adding new objects to some kind of array, for example, I find the [...]]]></description>
			<content:encoded><![CDATA[<p>Method chaining is something I&#8217;m used to seeing in the C++ world, possibly because the programmers that live there are more used to passing around references. In Java I see this used much more infrequently and I&#8217;ve come to miss it. When adding new objects to some kind of array, for example, I find the following much more clear and concise than the non-chained alternative.</p>
<pre>List&lt;Thing&gt; list = new List&lt;Thing&gt;();
//chained
list.add(new Thing().setValue(42));
//unchained
Thing thing = new Thing();
thing.setValue(42);
list.add(thing);
</pre>
<p>The major benefit being extra clarity for us humans. Too few Java programmers create their objects this way, which is a shame. The only change would be to return objects instead of voids in your setters, etc.</p>
<p>You can achieve a similar effect using instance initialisation blocks.</p>
<pre>List&lt;Thing&gt; thing = new List&lt;Thing&gt;(){{
    add(new Thing(){{
        setValue(42);
    }});
}};
</pre>
<p>This really shines when you have a lot of setup to do on an object. Not only does this reduce code clutter, it means that you&#8217;re forced (if you stick to it) into keeping your intialisation code all in one place and you get nice hierarchic code style.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.djmcgreal.co.uk/archives/150/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

