<?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>A Concurrent Affair &#187; Code Pranger</title>
	<atom:link href="http://www.concurrentaffair.org/category/code-pranger/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.concurrentaffair.org</link>
	<description>a blog about Mathias&#039; work and play.</description>
	<lastBuildDate>Tue, 07 Feb 2012 07:11:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>String Comparisons for Common Words Are Bad</title>
		<link>http://www.concurrentaffair.org/2009/10/05/string-comparisons-for-common-words-are-bad/</link>
		<comments>http://www.concurrentaffair.org/2009/10/05/string-comparisons-for-common-words-are-bad/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 23:11:33 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Code Pranger]]></category>
		<category><![CDATA[DrJava]]></category>

		<guid isPermaLink="false">http://www.concurrentaffair.org/?p=1211</guid>
		<description><![CDATA[It has been a long time since the last item went on the code pranger. I&#8217;ve pulled my hair out over many things during the last months, but this is the first time in a long while that I slapped &#8230; <a href="http://www.concurrentaffair.org/2009/10/05/string-comparisons-for-common-words-are-bad/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It has been a long time since the last item went on the code pranger. I&#8217;ve pulled my hair out over many things during the last months, but this is the first time in a long while that I slapped my forehead and wanted to run away.</p>

<p>The issue was first discovered by one of our users and submitted as a <a href="https://sourceforge.net/tracker/index.php?func=detail&#038;aid=2872797&#038;group_id=44253&#038;atid=438935">bug report</a>.  Let me first emphasize that this is clearly a bug in our DrJava code base, not in the submitted sample:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CompilerErrors <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">int</span> shared <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sampleMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> x <span style="color: #339933;">=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This is sampleMethod. x is &quot;</span> <span style="color: #339933;">+</span> x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>Note in particular that the closing brace is missing, so we would expect something like &#8220;} expected&#8221; or &#8220;reached end of file while parsing&#8221;. The user noticed, however, that when he compiled the following file with Java 6, he got an internal DrJava error instead.</p>

<p>The problem is that we are using <code class="codecolorer text mac-classic"><span class="text">String</span></code> messages to pass the compiler errors and exceptions thrown in the compiler, and then we try to separate these two categories again: Compiler errors are displayed in the &#8220;Compiler Output&#8221; pane, exceptions are reported as an error in the &#8220;DrJava Errors&#8221; window.</p>

<p>How did we decide to distinguish errors and exceptions? Using a very unstructured string comparison:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>message <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>message.<span style="color: #006633;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;CompilerError&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aunexpectedexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">UnexpectedException</span></a><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>

<p>If the word &#8220;CompilerError&#8221; occurs in the error message, it is treated as exception. Since the position of the &#8220;CompilerError&#8221; substring isn&#8217;t restricted, any silly compiler error in a file <i>named</i> CompilerError.java, or CompilerErrorModel.java, or CompilerErrorModelTest.java, or CompilerErrorPanel.java &#8212; all of which exist in the DrJava code base! &#8212; will be treated as serious unexpected exception in the compiler.</p>

<p>It is a really bad idea to use common words that could occur in user-generated text, like file names, as a marker for error conditions.</p>

<p>The solution for this problem, of course, was to analyze the structure of the error messages more closely: The class name of the exception, fully qualified as <code class="codecolorer text mac-classic"><span class="text">sun.tools.java.CompilerError</span></code> had to appear as part of the string <code class="codecolorer text mac-classic"><span class="text">&quot;Compile exception: sun.tools.java.CompilerError&quot;</span></code> at the beginning of the message. Spaces are not allowed in Java class names, so this cannot clash with a user generated file name. I changed the conditional above to:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// need to precisely match the CompilerError message, otherwise a file name containing</span><br />
<span style="color: #666666; font-style: italic;">// &quot;CompilerError&quot; may trigger an UnexpectedException (see bug 2872797)</span><br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>message <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> message.<span style="color: #006633;">startsWith</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Compile exception: sun.tools.java.CompilerError&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aunexpectedexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">UnexpectedException</span></a><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>

<p>Two additional lessons to be learned from this are that we need to run our tests with both Java 5 and Java 6, and that we need to eat our dog food even more: We should compile DrJava inside DrJava.</p>

<p>Fixed as of revision 5105. I have made a new jar available: <a href="http://www.cs.rice.edu/~javaplt/drjavarice/weekly/files/drjava-weekly-20091006-r5105.jar" class="broken_link">drjava-weekly-20091006-r5105.jar</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2009/10/05/string-comparisons-for-common-words-are-bad/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Dumbest DrJava Bug Report Yet</title>
		<link>http://www.concurrentaffair.org/2008/04/22/the-dumbest-drjava-bug-report-yet/</link>
		<comments>http://www.concurrentaffair.org/2008/04/22/the-dumbest-drjava-bug-report-yet/#comments</comments>
		<pubDate>Tue, 22 Apr 2008 06:06:36 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Code Pranger]]></category>
		<category><![CDATA[DrJava]]></category>
		<category><![CDATA[Ramblings]]></category>

		<guid isPermaLink="false">http://www.concurrentaffair.org/?p=520</guid>
		<description><![CDATA[Looking at DrJava bug reports often makes me up... <a href="http://www.concurrentaffair.org/2008/04/22/the-dumbest-drjava-bug-report-yet/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Looking at DrJava bug reports often makes me upset: Either because there is a bug in DrJava, or because there might be a bug in DrJava, but I can&#8217;t know for sure because the user neglected to provide crucial information about the system DrJava was running on and the source code that was edited.</p>

<p>Then there is a completely different category: Bug reports filed by users who are so unbelievably ignorant that they report a bug in a &quot;Hello World&quot;-level program of theirs as a bug in DrJava. That really makes me lose my cool.</p>

<p>In order to vent a little, I herewith present you the dumbest DrJava bug report yet: <a href="http://sourceforge.net/tracker/index.php?func=detail&#038;aid=1914390&#038;group_id=44253&#038;atid=438935">[ 1914390 ] Unhandled exception: java.lang.NullPointerException</a>.</p>

<p>Check out this masterpiece of software engineering:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Scanner</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ScanFajlli <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> arg<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IOException</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Scanner s <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">// String f = null;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; s.<span style="color: #006633;">useDelimiter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; s <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Scanner<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abufferedreader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">BufferedReader</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afilereader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">FileReader</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>s.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>s.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>s <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; s.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>It&#8217;s difficult to put the <code class="codecolorer text mac-classic"><span class="text">null</span></code> value and the method call any closer together. Truly stunning.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2008/04/22/the-dumbest-drjava-bug-report-yet/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Builder Pattern in DrJava</title>
		<link>http://www.concurrentaffair.org/2008/04/16/the-builder-pattern-in-drjava/</link>
		<comments>http://www.concurrentaffair.org/2008/04/16/the-builder-pattern-in-drjava/#comments</comments>
		<pubDate>Wed, 16 Apr 2008 22:57:29 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Code Pranger]]></category>
		<category><![CDATA[DrJava]]></category>

		<guid isPermaLink="false">http://www.concurrentaffair.org/?p=519</guid>
		<description><![CDATA[I'm currently in the process of dealing with a ... <a href="http://www.concurrentaffair.org/2008/04/16/the-builder-pattern-in-drjava/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently in the process of dealing with a confusing error message that one of our users got when he tried to save his files in <a href="http://www.drjava.org">DrJava</a>. The problem is actually pretty simple: The files were read-only, and he got a &#8220;Permission denied&#8221; message that, and I agree, was too broad to nail down the exact nature of the problem.</p>

<p>What I&#8217;m planning to do now is display a better error message, and ask the user if he wants to overwrite the files even though they are read-only. Generally, this transfers to failed attempts to save. Here, I want to present the user with a list of files that could not be saved and ask the user if he wants to retry saving them. For read-only files, there is a clear solution available: make the files writable (even though Sun, in their inexplicable enterprise to keep Java development challenging and interesting has provided the <code class="codecolorer text mac-classic"><span class="text">File.setReadOnly()</span></code> method, but no method to make a file writable). For writes that failed for other reasons, there is no clear panacea, but a &#8220;Retry&#8221; option seems useful.</p>

<p>There already was a dialog that does much of what I need, <code class="codecolorer text mac-classic"><span class="text">ScrollableListDialog</span></code>. It has been used to display a list of files that could not be found and have therefore been removed from the project. Now I&#8217;m going to extend that dialog so it supports more than just one button (&#8220;OK&#8221;), lets the programmer know which button was pressed, and let the user select files in the list. So it&#8217;s getting a bit more complicated.</p>

<p>When I looked at the constructor, I decided that it was already way too complicated. It was badly designed and as a result error-prone and time-consuming to deal with. Here is the main constructor of the class:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> ScrollableListDialog<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aframe+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Frame</span></a> owner,<br />
&nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> dialogTitle,<br />
&nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> leaderText,<br />
&nbsp; Collection<span style="color: #339933;">&lt;?&gt;</span> listItems,<br />
&nbsp; <span style="color: #000066; font-weight: bold;">int</span> messageType,<br />
&nbsp; <span style="color: #000066; font-weight: bold;">int</span> width,<br />
&nbsp; <span style="color: #000066; font-weight: bold;">int</span> height,<br />
&nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aicon+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Icon</span></a> icon,<br />
&nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> fitToScreen<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">/* ... */</span> <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>There were additional constructors that supplied default values so that not all values had to be set manually. Still, extending this structure even further meant adding even more constructors. I decided this was the wrong path. Instead, I chose the <a href="http://java.dzone.com/news/design-pattern-builder-pattern">Builder design pattern</a>.</p>

<p>The constructor for <code class="codecolorer text mac-classic"><span class="text">ScrollableListDialog</span></code> is private, so instances cannot be created directly. Instead, there is a static class <code class="codecolorer text mac-classic"><span class="text">ScrollableListDialog.Builder</span></code> that starts out with the default values in its fields. <code class="codecolorer text mac-classic"><span class="text">Builder</span></code> has setters to change these values, and a factory method <code class="codecolorer text mac-classic"><span class="text">ScrollableListDialog build()</span></code> that creates the <code class="codecolorer text mac-classic"><span class="text">ScrollableListDialog</span></code> so configured. All of the setters return <code class="codecolorer text mac-classic"><span class="text">this</span></code>, so setting different values can be chained together. Consider this explicit constructor call (using one of the other constructors that I haven&#8217;t shown):</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ScrollableListDialog dialog <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ScrollableListDialog<span style="color: #009900;">&#40;</span>MainFrame.<span style="color: #000000; font-weight: bold;">this</span>,<br />
&nbsp; <span style="color: #0000ff;">&quot;Files Not Found&quot;</span>,<br />
&nbsp; <span style="color: #0000ff;">&quot;The following files could not be found and have been removed from the project.&quot;</span>,<br />
&nbsp; filePaths,<br />
&nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajoptionpane+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JOptionPane</span></a>.<span style="color: #006633;">ERROR_MESSAGE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>

<p>This isn&#8217;t even the full-blown constructor shown above, but it already isn&#8217;t clear which of the strings is the title and which the text for the dialog body. If we add width and height, it isn&#8217;t clear which comes first. Type systems do not help here. Using the Builder pattern, the above becomes a bit more verbose, but much clearer:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ScrollableListDialog dialog <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ScrollableListDialog.<span style="color: #006633;">Builder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; .<span style="color: #006633;">setOwner</span><span style="color: #009900;">&#40;</span>MainFrame.<span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><br />
&nbsp; .<span style="color: #006633;">setTitle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Files Not Found&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; .<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The following files could not be found and have been removed from the project.&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; .<span style="color: #006633;">setItems</span><span style="color: #009900;">&#40;</span>filePaths<span style="color: #009900;">&#41;</span><br />
&nbsp; .<span style="color: #006633;">setMessageType</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajoptionpane+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JOptionPane</span></a>.<span style="color: #006633;">ERROR_MESSAGE</span><span style="color: #009900;">&#41;</span><br />
&nbsp; .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>

<p>Not only is this type-safe, but it also pairs up the value being set with its name. Using the Builder pattern, Java can achieve something similar to named variables, where the name of the variable has to be stated when passing an argument. The order in which the values are set is arbitrary, and only those values need to be bothered with that are actually important in the specific situation.</p>

<p>I should mention two little issues: First, some people may ask why everything in the <code class="codecolorer text mac-classic"><span class="text">Builder</span></code> class can&#8217;t be static. Then I would not have to <code class="codecolorer text mac-classic"><span class="text">new</span></code> the builder class and create another object. In single-threaded code, that would be fine, but if the <code class="codecolorer text mac-classic"><span class="text">Builder</span></code> class is accessed from two different threads simultaneously, their settings would interfere with each other. Second, the builder pattern makes it slightly harder to subclass <code class="codecolorer text mac-classic"><span class="text">ScrollableListDialog</span></code>: In addition to subclassing that class, the <code class="codecolorer text mac-classic"><span class="text">Builder</span></code> class has to be subclassed as well so that it actually builds an instance of the new subclass.</p>

<p>Now that the Builder is in place, I can easily extend <code class="codecolorer text mac-classic"><span class="text">ScrollableListDialog</span></code> without having to worry that other users do not know what to pass in for the <code class="codecolorer text mac-classic"><span class="text">canSelect</span></code> flag, for example.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2008/04/16/the-builder-pattern-in-drjava/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Awful Project File Subsystem of DrJava Will Be Gone!</title>
		<link>http://www.concurrentaffair.org/2008/03/31/the-awful-project-file-subsystem-of-drjava-will-be-gone/</link>
		<comments>http://www.concurrentaffair.org/2008/03/31/the-awful-project-file-subsystem-of-drjava-will-be-gone/#comments</comments>
		<pubDate>Tue, 01 Apr 2008 04:29:01 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Code Pranger]]></category>
		<category><![CDATA[DrJava]]></category>

		<guid isPermaLink="false">http://www.concurrentaffair.org/index.php/2008/03/31/the-awful-project-file-subsystem-of-drjava/</guid>
		<description><![CDATA[Hurray, we're finally getting rid of the awfull... <a href="http://www.concurrentaffair.org/2008/03/31/the-awful-project-file-subsystem-of-drjava-will-be-gone/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Hurray, we&#8217;re finally getting rid of the awfully over-engineered, needlessly complex, brittle and confusing project file subsystem of <a href="http://www.drjava.org/">DrJava</a>. We&#8217;ll keep the files in the current state and retain the ability to read old project files, but we&#8217;ll never ever touch them again, and we will always save in the new format.</p>

<p>The subsystem is too big to completely show here on the Code Pranger, but I found instructions on what must be done to add a property to the project file:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000; font-style: italic; font-weight: bold;">/** This parser uses the s-expression parser defined<br />
&nbsp;* &nbsp;in the util pacakge. &nbsp;The SExp tree given by the parser is <br />
&nbsp;* &nbsp;interpreted into a ProjectFileIR that is given to the user.<br />
&nbsp;* &nbsp;This class must also deal with different versions of the project file.<br />
&nbsp;* <br />
&nbsp;* &nbsp;If at some point new information is to be stored in the project<br />
&nbsp;* &nbsp;file, the following places in the code that need to changed:<br />
&nbsp;* &nbsp;-- If the new information pertains to a document, the<br />
&nbsp;* &nbsp; &nbsp;DocFile class should be augmented to store the new info.<br />
&nbsp;* &nbsp;-- The interface for the DocumentInfoGetter should be<br />
&nbsp;* &nbsp; &nbsp;expanded to allow for the new data to be retrieved.<br />
&nbsp;* &nbsp;-- Add a new clause to the else-if ladder in the FilePropertyVisitor.<br />
&nbsp;* &nbsp;-- Add the new information to the DocFile from the<br />
&nbsp;* &nbsp; &nbsp;DocumentInfoGetter in the ProjectFileBuilder's <br />
&nbsp;* &nbsp; &nbsp;addSourceDocument method.<br />
&nbsp;* &nbsp;-- If the change is at the top level, you must modify the<br />
&nbsp;* &nbsp; &nbsp;evaluateExpression method in this parser and add the <br />
&nbsp;* &nbsp; &nbsp;corresponding methods to the ProjectFileIR, ProjectFileIRImpl,<br />
&nbsp;* &nbsp; &nbsp;and ProjectFileBuilder.<br />
&nbsp;*/</span></div></td></tr></tbody></table></div>

<p>So let&#8217;s see: If you are only adding some more data that describes a document, you just have to change three classes, and one method. That&#8217;s not true at all. You have to change the <code class="codecolorer text mac-classic"><span class="text">OpenDefinitionsDocument</span></code> class, the inner classes <code class="codecolorer text mac-classic"><span class="text">ConcreteOpenDefDoc</span></code> in both <code class="codecolorer text mac-classic"><span class="text">AbstractGlobalModel</span></code> and <code class="codecolorer text mac-classic"><span class="text">DefaultGlobalModel</span></code>, and perhaps one or more of the interfaces it implements as well, for instance <code class="codecolorer text mac-classic"><span class="text">INavigatorItem</span></code>.</p>

<p>And if the data you are storing is of some new form, in a format for which we have no visitors (sub-parsers), then you have to write your own visitors. For example, for a list of breakpoints, I had to write this very short snippet, just to parse<sup><a href="#footnote-1-517" id="footnote-link-1-517" title="See the footnote.">1</a></sup>:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/** Parses out a list of breakpoint nodes. */</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> BreakpointListVisitor <span style="color: #000000; font-weight: bold;">implements</span> SEListVisitor<span style="color: #339933;">&lt;</span><br />
&nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>DebugBreakpointData<span style="color: #339933;">&gt;&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>DebugBreakpointData<span style="color: #339933;">&gt;</span> forEmpty<span style="color: #009900;">&#40;</span>Empty e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>DebugBreakpointData<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>DebugBreakpointData<span style="color: #339933;">&gt;</span> forCons<span style="color: #009900;">&#40;</span>Cons c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>DebugBreakpointData<span style="color: #339933;">&gt;</span> list <span style="color: #339933;">=</span> c.<span style="color: #006633;">getRest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">accept</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; DebugBreakpointData tmp <span style="color: #339933;">=</span> ProjectFileParser.<span style="color: #006633;">ONLY</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">parseBreakpoint</span><span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">getFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, _srcFileBase<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// add to the end</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> list<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/** Parses out the labeled node (a non-empty list) into<br />
&nbsp; &nbsp;* &nbsp;a breakpoint. The node must have the &quot;breakpoint&quot; label on it.<br />
&nbsp; &nbsp;* &nbsp;@param s the non-empty list expression<br />
&nbsp; &nbsp;* &nbsp;@return the breakpoint described by this s-expression<br />
&nbsp; &nbsp;*/</span><br />
&nbsp; DebugBreakpointData parseBreakpoint<span style="color: #009900;">&#40;</span>SExp s, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> pathRoot<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name <span style="color: #339933;">=</span> s.<span style="color: #006633;">accept</span><span style="color: #009900;">&#40;</span>NameVisitor.<span style="color: #006633;">ONLY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>name.<span style="color: #006633;">compareToIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;breakpoint&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> PrivateProjectException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expected a breakpoint tag, &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;found: &quot;</span> <span style="color: #339933;">+</span> name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> <span style="color: #009900;">&#40;</span>s <span style="color: #000000; font-weight: bold;">instanceof</span> Cons<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> PrivateProjectException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expected a labeled node, &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;found a label: &quot;</span> <span style="color: #339933;">+</span> name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; SEList c <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>Cons<span style="color: #009900;">&#41;</span>s<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getRest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// get parameter list</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; BreakpointPropertyVisitor v <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BreakpointPropertyVisitor<span style="color: #009900;">&#40;</span>pathRoot<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> c.<span style="color: #006633;">accept</span><span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/** Traverses the list of expressions found after &quot;breakpoint&quot;<br />
&nbsp; &nbsp;* &nbsp;tag and returns the Breakpoint described by those properties. */</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> BreakpointPropertyVisitor <span style="color: #000000; font-weight: bold;">implements</span><br />
&nbsp; &nbsp; SEListVisitor<span style="color: #339933;">&lt;</span>DebugBreakpointData<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> fname <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Integer</span></a> offset <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Integer</span></a> lineNumber <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> isEnabled <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> pathRoot<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> BreakpointPropertyVisitor<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> pr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> pathRoot <span style="color: #339933;">=</span> pr<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> DebugBreakpointData forCons<span style="color: #009900;">&#40;</span>Cons c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name <span style="color: #339933;">=</span> c.<span style="color: #006633;">getFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">accept</span><span style="color: #009900;">&#40;</span>NameVisitor.<span style="color: #006633;">ONLY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>name.<span style="color: #006633;">compareToIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; fname <span style="color: #339933;">=</span> ProjectFileParser.<span style="color: #006633;">ONLY</span>.<span style="color: #006633;">parseFileName</span><span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">getFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>name.<span style="color: #006633;">compareToIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;offset&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; offset <span style="color: #339933;">=</span> ProjectFileParser.<span style="color: #006633;">ONLY</span>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">getFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>name.<span style="color: #006633;">compareToIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;line&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; lineNumber <span style="color: #339933;">=</span> ProjectFileParser.<span style="color: #006633;">ONLY</span>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">getFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>name.<span style="color: #006633;">compareToIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;enabled&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; isEnabled <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> c.<span style="color: #006633;">getRest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">accept</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> DebugBreakpointData forEmpty<span style="color: #009900;">&#40;</span>Empty c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>fname <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>offset <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>lineNumber <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> PrivateProjectException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Breakpoint information &quot;</span><span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;incomplete, need name, offset and line tags&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>pathRoot <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a><span style="color: #009900;">&#40;</span>fname<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isAbsolute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a> f <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a><span style="color: #009900;">&#40;</span>fname<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> DebugBreakpointData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a> getFile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> f<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getOffset<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> offset<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getLineNumber<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> lineNumber<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isEnabled<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> isEnabled<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a> f <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a><span style="color: #009900;">&#40;</span>pathRoot, fname<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> DebugBreakpointData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a> getFile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> f<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getOffset<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> offset<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getLineNumber<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> lineNumber<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isEnabled<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> isEnabled<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>And when the data that you want to add does not pertain to one particular document, but is rather a property of the entire project, then you need to change <code class="codecolorer text mac-classic"><span class="text">ProjectFileIR</span></code>, <code class="codecolorer text mac-classic"><span class="text">ProjectFileIRImpl</span></code> and <code class="codecolorer text mac-classic"><span class="text">ProjectFileBuilder</span></code>. At least that&#8217;s what the description says. But you&#8217;ll also have to change the <code class="codecolorer text mac-classic"><span class="text">AbstractGlobalModel</span></code>, its subclass <code class="codecolorer text mac-classic"><span class="text">DefaultGlobalModel</span></code>, perhaps one or more of its superclasses or implemented interfaces, and definitely also the inner class <code class="codecolorer text mac-classic"><span class="text">ProjectFileGroupingState</span></code> in <code class="codecolorer text mac-classic"><span class="text">AbstractGlobalModel</span></code>. And since we want to be able to treat both project mode and flat-file mode the same, you then modify the interface <code class="codecolorer text mac-classic"><span class="text">ProjectFileGroupingState</span></code> is implementing, and that forces you to change the inner class <code class="codecolorer text mac-classic"><span class="text">FlatFileGroupingState</span></code> in <code class="codecolorer text mac-classic"><span class="text">AbstractGlobalModel</span></code> as well. Oh, and of course you may have the parsing problem again and have to write that lil&#8217; bit of code up there again.</p>

<p>Once you&#8217;ve done all that, you finally get to the easy part of adding GUI components to the &#8220;Project Properties&#8221; dialog. I hate GUI work, but after the ordeal described above, it felt like a vacation.</p>

<p>I&#8217;m the only developer that has <em>added</em><sup><a href="#footnote-2-517" id="footnote-link-2-517" title="See the footnote.">2</a></sup> anything to the project file format during the last <em>three years and eight months</em>. I&#8217;m the only one who had to go through this pain since July 2004. In fact, I have suffered more than the person who initially created this complex construct, because after it was in place, only three small pieces of data were added, and then the project file format remained unchanged for <em>a year and eight months</em>, until I came along.</p>

<p>I have since added persistent breakpoints and watches from the debugger to the project file, persistent bookmarks and information about where the project&#8217;s jar file should be created, and using which options. I can&#8217;t exactly tell who came up with this code that has tortured me for hours because the SourceForge repository history doesn&#8217;t reach far enough back, and I wouldn&#8217;t mention the name here anyway, but it must have been written by someone who completely misunderstood fundamental principles of object-oriented design. The code is generally rather well written, assuming that nothing changes: It is correct and robust. However, the developer paid no attention at all to extensibility and flexibility, and made it a nightmare to extend the system.</p>

<p>This is a clear example of over-engineering, of thinking of doing it &#8220;the right way&#8221;, of creating a large object-oriented design that incorporates nearly all design patterns the developer partially understands. This is an example of code that made the original developer feel good about himself, because he came up with something he considered &#8220;pretty&#8221;, even though something much simpler and much less pretty would have sufficed, and in the long term prevented a lot of silent anger and premature aging.</p>

<p>Of course my <code class="codecolorer text mac-classic"><span class="text">XMLConfig</span></code> class has about 600 lines of code and 500 lines of unit tests, and it relies on Java&#8217;s classes for dealing with XML files, but there&#8217;s no reason to reinvent the wheel. In fact, writing an S-expression parser for the project files is another mark of a bad developer who overestimates himself: Instead of looking around and using code that already exists and that has been tested, the macho programmer decides he can do better and without any reason writes the code himself all over again. And because this programmer isn&#8217;t as good as he thinks he is, and because he is human, he makes mistakes. The repository history shows several commits that had to fix bugs in the project file code.</p>

<p><code class="codecolorer text mac-classic"><span class="text">XMLConfig</span></code> is actually very simple code. It just takes a string, which describes a path, follows that path into the AST defined by the XML file, and then returns the value found at the end of the path. And once I got past writing and testing <code class="codecolorer text mac-classic"><span class="text">XMLConfig</span></code>, which did not take long, I have been using it in almost every project that needs to save data. Instead of requiring code like the 80-line extract above, my code using <code class="codecolorer text mac-classic"><span class="text">XMLConfig</span></code> can load a list of file names in a single line.</p>

<p>Soon, two of our current DrJava developers taking the COMP 312 &#8220;Production Programming&#8221; course will add a complex data structure to the project file, and I don&#8217;t want them to go through what I had to go through. I don&#8217;t want them to write something like the 80 lines above. And I want to get to a point where adding information to the project file is really easy, because right now the current project file subsystem acts as a huge deterrent against any kind of change.</p>
<br />Footnotes:<br /><ol class="footnotes"><li id="footnote-1-517">Please don&#8217;t criticize the coding style. I wrote this code, but I mimicked the style the project file subsystem was written in.  [<a href="#footnote-link-1-517">back</a>]</li><li id="footnote-2-517">One developer changed the format slightly by changing the name of a property.  [<a href="#footnote-link-2-517">back</a>]</li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2008/03/31/the-awful-project-file-subsystem-of-drjava-will-be-gone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mutate and Return</title>
		<link>http://www.concurrentaffair.org/2008/02/08/mutate-and-return/</link>
		<comments>http://www.concurrentaffair.org/2008/02/08/mutate-and-return/#comments</comments>
		<pubDate>Fri, 08 Feb 2008 23:47:05 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Code Pranger]]></category>
		<category><![CDATA[DrJava]]></category>
		<category><![CDATA[Ramblings]]></category>

		<guid isPermaLink="false">http://www.concurrentaffair.org/index.php/2008/02/08/mutate-and-return/</guid>
		<description><![CDATA[I've decided to start a new category: The Code ... <a href="http://www.concurrentaffair.org/2008/02/08/mutate-and-return/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve decided to start a new category: The <a href="http://www.concurrentaffair.org/index.php/category/code-pranger/">Code Pranger</a>. Maybe it&#8217;s a bit redundant, because most of it will be some form of <a href="http://www.concurrentaffair.org/index.php/category/ramblings/">rambling</a>, and <a href="http://thedailywtf.com">The Daily WTF</a> probably does a much better and much more public job at this, but this pranger will be under <em>my</em> control.</p>

<p>To clarify, a <a href="http://en.wikipedia.org/wiki/Pranger">pranger</a> is a medieval device for torture and public humiliation, similar to stocks. Someone who had done something shameful was locked up in a pranger, usually by restraining the neck and both hands between two pieces of metal or wood, and exposed to the public. I am not going to expose the writers of bad code here (even though sometimes I might want to; let this be a reminder to myself: don&#8217;t!), I&#8217;ll just put pieces of code, which I find ugly, on the pranger.</p>

<p>The purpose, of course, is to provide anti-patterns, perhaps create some discussion, and increase the overall quality of code being written. I realize that I will probably won&#8217;t achieve the last of those goals. I also realize that I probably won&#8217;t even fix the examples of bad code, because I believe in the maxim of &#8220;don&#8217;t fix what&#8217;s not broken&#8221;, where broken and ugly aren&#8217;t the same thing.</p>

<p>Anyway, here we go. I absolutely hate this piece of code summarized below:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ProjectPropertiesFrame <span style="color: #000000; font-weight: bold;">extends</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajframe+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JFrame</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> DirectorySelectorComponent _workDirSelector<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajpanel+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JPanel</span></a> _workDirectoryPanel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; _workDirSelector <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DirectorySelectorComponent<span style="color: #009900;">&#40;</span><span style="color: #666666; font-style: italic;">/* ... */</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> _workDirSelector<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> _setupPanel<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajpanel+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JPanel</span></a> panel<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ajpanel+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">JPanel</span></a> wdPanel <span style="color: #339933;">=</span> _workDirectoryPanel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gridbag.<span style="color: #006633;">setConstraints</span><span style="color: #009900;">&#40;</span>wdPanel, c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; panel.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>wdPanel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>The method <code class="codecolorer text mac-classic"><span class="text">\_workDirectoryPanel()</span></code> mutates the <code class="codecolorer text mac-classic"><span class="text">_workDirSelector</span></code> field and returns the new value of the field at the same time. At the call site, the return value is used for further processing.</p>

<p>Why mutate <em>and</em> return? Either mutate the field, let the method be void, and use the field. It&#8217;s been mutated, so it&#8217;s ready to go. Or don&#8217;t touch the field, don&#8217;t mutate, return the new value to the caller, and let the caller decide what to do: Mutating the field, for example. If you really want to do both, at least return the <em>old</em> value of the field before it was mutated. That makes at least sense in some situations.</p>

<p>But mutating and returning the value of the field? That&#8217;s just a function with an ugly, perverse side effect. I really don&#8217;t see the use at all. Is it convenience? No, it can&#8217;t be, it would be more convenient just to mutate and not create the local variable <code class="codecolorer text mac-classic"><span class="text">wdPanel</span></code>. Why would anyone write this?</p>

<p>I think mutation should be avoided whenever possible. Of course, in most production programming languages, that&#8217;s not possible. So make a void function and be explicit about the mutation you&#8217;re doing. There&#8217;s a programming style that really dislikes void methods; I sometimes subscribe to it. If a method is void, you can&#8217;t chain method calls together. If it returns something, you can. So if, for example, you have a <code class="codecolorer text mac-classic"><span class="text">List&lt;T&gt;</span></code> class and the <code class="codecolorer text mac-classic"><span class="text">add(T element)</span></code> method mutates the internal state of the list, then you could let the method return <code class="codecolorer text mac-classic"><span class="text">this</span></code>:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> List<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> add<span style="color: #009900;">&#40;</span>T element<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>That way, you can quite succinctly write something like <code class="codecolorer text mac-classic"><span class="text">myList.add(1).add(2).add(3);</span></code>. But that&#8217;s a completely different situation.</p>

<p>Can anyone find an explanation why a method should mutate a field <em>and</em> return the new value of the field? I can&#8217;t.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2008/02/08/mutate-and-return/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

