<?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>Schauderhaft &#187; clean code</title>
	<atom:link href="http://blog.schauderhaft.de/tag/clean-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.schauderhaft.de</link>
	<description>Softwaredevelopment, Projectmanagement, Qualitymanagement and all things &#34;schauderhaft&#34;</description>
	<lastBuildDate>Sun, 22 Jan 2012 19:13:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The One Correct Way to do Dependency Injection</title>
		<link>http://blog.schauderhaft.de/2012/01/01/the-one-correct-way-to-do-dependency-injection/</link>
		<comments>http://blog.schauderhaft.de/2012/01/01/the-one-correct-way-to-do-dependency-injection/#comments</comments>
		<pubDate>Sun, 01 Jan 2012 05:17:02 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[dependency injection]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=996</guid>
		<description><![CDATA[A couple of weeks ago a coworker told me that they have a little utility in their projects in order to set private fields for tests. He kind of claimed they needed that since they are using Spring, which in production sets the dependency. I was a little confused. But after looking into it I [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago a coworker told me that they have a little utility in their projects in order to set private fields for tests. He kind of claimed they needed that since they are using Spring, which in production sets the dependency. I was a little confused. But after looking into it I realized that there is an (anti)pattern going on that needs some fighting against. Lets have a look at the pattern.</p>
<p>Spring (and other DI frameworks) can inject dependencies into private fields. It looks like people like to use this because it combines some compelling properties:</p>
<ul>
<li>Very little boilerplate code. All you need is a simple annotation on the field.</li>
<li>Since the field is private nobody outside the class can change it.</li>
</ul>
<p>But now when you want to test your class and want to assign e.g. a mock to the field you have to either setup a Spring context in your tests or use reflection in order to access the field (just as Spring does).</p>
<p>Now something is really starting to smell bad here. One of the purposes of Dependency Injection is to decouple code, which in turn should make the code easier to test. Now that didn&#8217;t work out to well.</p>
<p>So how can we clean up this mess? We can write a setter for the field. That solves the testing problem. It adds a little boiler plate code, but hey we are talking Java here, so you should be used to that. But now we have a setter which anybody can call at any time. At the very best that doesn&#8217;t make any sense at all in the production environment. In the worst case somebody actually uses it and creates some ugly bug.</p>
<p>So what do you do when you don&#8217;t want anybody to change a field? Correct you make it final. Great, now you have a compile time error in the setter.You can only set a final field in a constructor. This leads naturally to the solution of all our problems (and possibly to world peace): <strong>Make your dependency a constructor argument!</strong></p>
<p>This is the only place where dependencies really belong. If you try to stick to that rule I&#8217;d expect you&#8217;ll encounter the following two problems.</p>
<ol>
<li>You&#8217;ll find classes with lots and lots of constructor arguments. I&#8217;ll bet that class does a lot of different things and really needs to get broken down into smaller pieces.</li>
<li>You&#8217;ll find cases where a class A needs an instance of B and B needs an instance of  A. This is a typical case of a circular dependency and is obviously bad. In my experience the solution is either to make B a part of A when the two are so strongly dependent that they really should be one class. More often though there is at least one more class C hiding in there so that B doesn&#8217;t need A but only C.</li>
</ol>
<p>So really the problems you&#8217;ll find when doing constructor based dependency injection are problems that are present in your current code already. So don&#8217;t shoot the messenger, but fix the problems in your code and go for constructor arguments for dependency injection.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2012/01/01/the-one-correct-way-to-do-dependency-injection/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>The Boiling Frog</title>
		<link>http://blog.schauderhaft.de/2011/11/13/the-boiling-frog/</link>
		<comments>http://blog.schauderhaft.de/2011/11/13/the-boiling-frog/#comments</comments>
		<pubDate>Sun, 13 Nov 2011 11:57:04 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[boiling frog]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[technical debt]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=963</guid>
		<description><![CDATA[I hope everybody among my readers is familiar with the concept of technical debt: If you do a quick hack to implement a feature it might be faster to implement in the short run, but you have to pay interest for the technical debt in the form of higher development and maintenance effort. If you [...]]]></description>
			<content:encoded><![CDATA[<p>I hope everybody among my readers is familiar with the concept of technical debt: If you do a quick hack to implement a feature it might be faster to implement in the short run, but you have to pay interest for the technical debt in the form of higher development and maintenance effort. If you don&#8217;t pay back you technical debt by refactoring your code, sooner or later your quick hack will have turned in a quick and highly expensive hack.</p>
<p>This metaphor works well to communicate the need for refactorings if at least one person realized the need for it.</p>
<p>But in various cases nobody in the project realizes that there is a problem until the team face a huge debt which seems impossible to pay back.</p>
<p>I see two possible reasons:</p>
<p><strong>Being blind</strong>: There is a different level of cleanliness people consider clean enough. In interviews I have repeatedly talked to people considering a 20 line method a good thing and don&#8217;t have problem with nested control structures 5 levels deep. Those developers wouldn&#8217;t notice anything smelly when looking at code which I would consider CMD (Code of Mass Destruction). This problem is most of the time fairly easy to fix by teaching and coaching. But there is a more insidious possible reason:</p>
<p><strong>Being a frog in boiling water</strong>: There is a theory that a frog which sits in cold water doesn&#8217;t jump out of the water if you heat it realy slow until it dies.<a href="http://en.wikipedia.org/wiki/Boiling_frog"> Wikipedia isn&#8217;t decisive</a> if this is actually true, but I definetly see this effect in software development. It looks like this: At some point you find something in your code that looks like it has an easy abstraction. So you build a little class encapsulating that behaviour and use that class instead. It works great, so that little class gets used a lot. Sometime later the class gets a new feature to handle a variation of the original case. And it goes on like this, until one day what was a simple abstraction has turned in a complex library, possibly even a framework. And now that framework is a problem on its own. Its getting hard to understand how to use it.</p>
<p>And you realize you are a poor little frog sitting in boiling water with no idea how he got there. Hint: It&#8217;s a good idea to jump even when it is a little late.</p>
<p><strong>Why does this happen?</strong> Just as the frog has a problem sensing the small change in temperatur and realizing he is getting into trouble the developer doesn&#8217;t see he is hurting the quality of the code base until it is to late.</p>
<p><strong>Again: Why?</strong> Let&#8217;s make the example a little more specific. You have blocks of 10 lines of simple repetitive code in N places in your code base. You replace it with a call to a simple class of 40 lines of code. So you save (9*N &#8211; 40) lines. On the call site your code gets significantly simpler, of course the class is a little more complex but thats ok. Now while implementing a new feature you are about to create another of those 10 line blocks. Obviously you want to use the helper class. But it&#8217;s not fit for the job. You need to add a feature to it. That&#8217;s ok. You also have to add something to the public API to turn that feature on or of. Maybe it&#8217;s a new constructor, a new method or an additional parameter. That&#8217;s not ok. Until you changed the API of your class the changes where local to the helper class and its usage at the new site. But when you changed the API, you added complexity to all the call sites of your class. Whenever you now call that helper you have to think a little more about the correct API to use. This unfortunatly isn&#8217;t easy to see. So it can easily happen that your API turns slowly so complex that using it is more painfull then just writing the 10 lines it replaces down.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2011/11/13/the-boiling-frog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clean Code is just a Crutch</title>
		<link>http://blog.schauderhaft.de/2011/09/04/clean-code-is-just-a-crutch/</link>
		<comments>http://blog.schauderhaft.de/2011/09/04/clean-code-is-just-a-crutch/#comments</comments>
		<pubDate>Sun, 04 Sep 2011 20:46:26 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[software craftmanship]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=917</guid>
		<description><![CDATA[A last week I was at the completely awesome SoCraTes conference. It was the first meeting of the German Software Craftsmanship Community. Some of the attendees argued that some day in the future there might be a way to measure the quality of software which in turn would help to argue with management about the [...]]]></description>
			<content:encoded><![CDATA[<p>A last week I was at the completely awesome <a href="http://socrates2011.pbworks.com/w/page/36608238/SoCraTes%202011">SoCraTes conference</a>. It was the first meeting of the German Software Craftsmanship Community. Some of the attendees argued that some day in the future there might be a way to measure the quality of software which in turn would help to argue with management about the need for refactoring and similar actions. I strongly disagree. What amounts to quality in one environment is a lack of quality or even a bug in another environment. So there can&#8217;t be a single metric.</p>
<p>Apart from being utopian the idea of a single quality measurement might actually hide a dangerous attitude: The attitude that clean code has a value on its own! It doesn&#8217;t. Even if you don&#8217;t like it: The only things that count in the end are measured in dollars, euro and yen. </p>
<p>In this sense clean code is only a crutch. We do it in order to get somewhere where we can&#8217;t go without, or at least not as easy. </p>
<p>We are convinced that improving our code, and writing tests </p>
<ul>
<li>
makes us faster (and therefore our work cheaper) in the not to long run.
</li>
<li>
makes the code easier to maintain.
</li>
<li>
makes it easier to add features tomorrow.
</li>
</ul>
<p>And as long as I&#8217;m convinced it is helping with these aims I&#8217;ll continue to do so, and so should you.</p>
<p>But if your are doing it just because it is in some fuzzy sense the &#8216;right thing to do&#8217; you are doing it for the wrong reason and you might be doing the wrong thing.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2011/09/04/clean-code-is-just-a-crutch/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Use More Cut&#8217;n&#039;Paste</title>
		<link>http://blog.schauderhaft.de/2011/08/07/use-more-cutnpaste/</link>
		<comments>http://blog.schauderhaft.de/2011/08/07/use-more-cutnpaste/#comments</comments>
		<pubDate>Sun, 07 Aug 2011 19:38:25 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[code reuse]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=879</guid>
		<description><![CDATA[Cut&#8217;n'Paste is frowned upon by most developers. And rightly so. If you copy code around you also copy bugs and missing features around. If you later need to make any change to that source code you have to copy the change to every single place where you copied the original snippet. Not a good thing. [...]]]></description>
			<content:encoded><![CDATA[<p>Cut&#8217;n'Paste is frowned upon by most developers. And rightly so. If you copy code around you also copy bugs and missing features around. If you later need to make any change to that source code you have to copy the change to every single place where you copied the original snippet. Not a good thing.</p>
<p>So the natural thing to do is to factor out common code into separate methods, classes and finally libraries. Great. But there is a downside to it.</p>
<p>Often code doesn&#8217;t get copied around as it is. It gets tweaked and adapted to the new environment in order to do its work. If you have factored out the code into some kind of library this library needs to support all the various ways of tweaking via some kind of API. This of course makes the library more complex.</p>
<p>As long as we consider only a single project where everybody has access to all the code and at least some basic understanding of it this is all fine. And I vote for extracting almost all duplicated code in a project. The story changes a lot though when we are talking about code reuse between projects. Now you have to maintain a library that is used by multiple independent groups of people. People that you might not even know. This implies that you have to combine way more requirements in your library. At the same time it gets harder to change, because every breaking change will cause work for projects no matter if they needed that new feature that caused the change. Also for a given project which might have a problem with the library it gets way slower to fix the problem, since they  can&#8217;t do it on their own. They have to consult with the owner of the library which has to weigh the issue at hand against the complexity of the library and the requirements of other projects.</p>
<p>These additional costs only pay of if the reduction of work by reusing the library is pretty big. Or if it is a real stable library without many requests for change.</p>
<p>In typical enterprise settings very often these criteria aren&#8217;t met. So my recommendation is: Just use cut and paste, i.e. make the actual source code available to projects. These projects can use the code as it is or modify to their tasting. If they have problems with their code, they have everything they need to fix it. But they also have to do it on their own. No mommy to run to.</p>
<p>If you are really lucky you can get some kind of feedback from the users of the code. This would allow to improve it so the next project grabbing it gets an improved version. And if finally thing solidify you might turn it into a library which gets distributed in binary form and maintained by a central team.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2011/08/07/use-more-cutnpaste/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Clean Code with Swing and Scala</title>
		<link>http://blog.schauderhaft.de/2011/06/26/clean-code-with-swing-and-scala/</link>
		<comments>http://blog.schauderhaft.de/2011/06/26/clean-code-with-swing-and-scala/#comments</comments>
		<pubDate>Sun, 26 Jun 2011 13:23:23 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[moreThanProperties]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[swing]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=821</guid>
		<description><![CDATA[I guess everybody who knows Java and Swing also knows the Swing Tutorial. It is a great source of information if you want to learn Swing. It is also a major catastrophy when it comes to structuring code. The problem is: Lots of people miss critical information contained in the tutorial like &#8216;Everything concerned with [...]]]></description>
			<content:encoded><![CDATA[<p>I guess everybody who knows Java and Swing also knows the <a href="http://download.oracle.com/javase/tutorial/uiswing/">Swing Tutorial</a>. It is a great source of information if you want to learn Swing. It is also a major catastrophy when it comes to structuring code. The problem is: Lots of people miss critical information contained in the tutorial like &#8216;Everything concerned with Swing must happen in the EDT&#8217; but they seem to suck up the messy way to structure code like a sponge.</p>
<p>This might result in code like the one below. It is actually Scala code, but that shouldn&#8217;t matter much. The only piece that is a little special are the calls to <tt>Binder</tt>, which is a little <a href="/2011/05/01/binding-scala-objects-to-swing-components/">Swing Binding Framework</a> which I introduced a couple of weeks ago.</p>
<div class="geshi no scala">
<div class="head">private def createPersonPanel(p : PersonEditor) = {</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val panel = new JPanel()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val layout = new GridBagLayout()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val c = new GridBagConstraints()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.gridx = 0
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.gridy = 0
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel.setLayout(layout)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.fill = 0
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel.add(new JLabel(&quot;firstname&quot;), c)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.fill = 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.gridx += 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.weightx = 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val firstnameTF = new JTextField()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel.add(firstnameTF, c)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Binder.bind(p.firstname, firstnameTF)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.fill = 0
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.weightx = 0
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.gridx = 0
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.gridy += 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel.add(new JLabel(&quot;lastname&quot;), c)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.fill = 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.gridx += 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val lastnameTF = new JTextField()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Binder.bind(p.lastname, lastnameTF)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel.add(lastnameTF, c)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.fill = 0
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.weightx = 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.gridy += 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.anchor = GridBagConstraints.SOUTHEAST
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val button = new JButton(&quot;save&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Binder.bind(p.save, button)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel.add(button, c)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }</div>
</li>
</ol>
</div>
<p>What is so bad about this piece of crap &#8230; ähm &#8230; code?</p>
<p>The method is long. 38 lines is about 10 times longer than healthy for a method.</p>
<p>There is tons of code duplication.</p>
<p>The method does lots of different things: creating components, adding them to a panel, configuring the layout.</p>
<p>There is a strong dependency on the order of commands. We can&#8217;t just move stuff up or down in the method and still hope the result will be something reasonable, even if we stick to rearrangements allowed by the compiler.</p>
<p>All this together makes the method extremely hard to understand. How long does it take to understand what kind of GUI results? Don&#8217;t bother to much, I help. It looks like this:</p>
<p><a href="http://blog.schauderhaft.de/wp-content/uploads/2011/06/small.png"><img class="alignnone size-full wp-image-824" title="Image of the Frame in packed state." src="http://blog.schauderhaft.de/wp-content/uploads/2011/06/small.png" alt="" width="132" height="104" /></a></p>
<p>and if you resize it, it looks like this</p>
<p><a href="http://blog.schauderhaft.de/wp-content/uploads/2011/06/large.png"><img class="alignnone size-full wp-image-825" title="Resized Frame" src="http://blog.schauderhaft.de/wp-content/uploads/2011/06/large.png" alt="" width="315" height="214" /></a></p>
<p>Arguably the result looks just as ugly as the code, but once the code is clean we might be able to improve on the visual design as well.</p>
<p>If you don&#8217;t see it in the code, you might see it in the images: There are three different ways JComponents are handled by the method: <tt>JLabels</tt> are in the left column and don&#8217;t resize. The <tt>JTextField</tt>s are in the right column and do resize and the JButton doesn&#8217;t resize and is in the buttom right. In the code this is completely hidden in the manipulation of the <tt>GridBagConstraint</tt>. So lets make it explicit in the code:</p>
<div class="geshi no scala">
<div class="head">private def addToLabelColumn(</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel : JPanel,
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; component : JComponent,
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; row : Int) {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val c = new GridBagConstraints()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.gridx = 0
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.gridy = row
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.weightx = 0
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.fill = 0
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel.add(component, c)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; private def addToComponentColumn(
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel : JPanel,
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; component : JComponent,
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; row : Int) {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val c = new GridBagConstraints()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.gridx = 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.gridy = row
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.weightx = 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.fill = 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel.add(component, c)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; private def addButton(
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel : JPanel,
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; component : JButton,
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; row : Int) {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val c = new GridBagConstraints()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.weightx = 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.gridx = 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.gridy = row
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.fill = 0
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c.anchor = GridBagConstraints.SOUTHEAST
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel.add(component, c)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; private def createPersonPanel(p : PersonEditor) = {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val panel = new JPanel()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val layout = new GridBagLayout()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel.setLayout(layout)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; addToLabelColumn(panel, new JLabel(&quot;firstname&quot;), 0)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val firstnameTF = new JTextField()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Binder.bind(p.firstname, firstnameTF)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; addToComponentColumn(panel, firstnameTF, 0)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; addToLabelColumn(panel, new JLabel(&quot;lastname&quot;), 1)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val lastnameTF = new JTextField()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Binder.bind(p.lastname, lastnameTF)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; addToComponentColumn(panel, lastnameTF, 1)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val button = new JButton(&quot;save&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Binder.bind(p.save, button)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; addButton(panel, button, 2)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }</div>
</li>
</ol>
</div>
<p>I introduced three methods. One for adding a JLabel, one for adding a JComponent and one for adding JButtons. These handle the arrangement of components on a JPanel. The total length of the code increased because we create the GridBagConstraints insided the methods and have to set all properties and don&#8217;t rely anymore on the previous step to leave the constraint in a specific state.</p>
<p>If we now look at the <tt>createPersonPanel</tt> we&#8217;ll get a strong fealing of repetition in various places:</p>
<ul>
<li>each call to add* methods takes the same JPanel as an argument. We can improve on this by creating a Builder wich crates the panel, contains the add* methods and can return the fully configured panel at the end.</li>
<li>for each property we create a <tt>JLabel</tt>, a <tt>JTextField</tt>, bind the property to the later and add both to the <tt>JPanel</tt>. We can fix this by encapsulating it in a seperate method.</li>
</ul>
<p>The result might look like this:</p>
<div class="geshi no scala">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; case class PanelBuilder() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val panel = new JPanel()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val layout = new GridBagLayout()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; panel.setLayout(layout)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; def add(components : (JLabel, JComponent), row : Int) {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addToLabelColumn(components._1, row)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addToComponentColumn(components._2, row)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; def add(
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; component : JButton,
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row : Int) {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val c = new GridBagConstraints()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.weightx = 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.gridx = 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.gridy = row
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.fill = 0
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.anchor = GridBagConstraints.SOUTHEAST
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panel.add(component, c)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; private def addToLabelColumn(
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; component : JComponent,
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row : Int) {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val c = new GridBagConstraints()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.gridx = 0
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.gridy = row
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.weightx = 0
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.fill = 0
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panel.add(component, c)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; private def addToComponentColumn(
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; component : JComponent,
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row : Int) {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val c = new GridBagConstraints()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.gridx = 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.gridy = row
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.weightx = 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.fill = 1
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panel.add(component, c)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; private def create(name : String, property : Property[String]) : (JLabel, JComponent) = {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val textField = new JTextField()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Binder.bind(property, textField)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; (new JLabel(name), textField)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; private def create(name : String, action : =&amp;gt; Unit) = {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val button = new JButton(&quot;save&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Binder.bind(action, button)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; button
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; private def createPersonPanel(p : PersonEditor) = {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; val builder = PanelBuilder()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; builder.add(create(&quot;firstname&quot;, p.firstname), 0)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; builder.add(create(&quot;lastname&quot;, p.lastname), 1)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; builder.add(create(&quot;save&quot;, p.save), 2)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; builder.panel
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }</div>
</li>
</ol>
</div>
<p>The <tt>PanelBuilder</tt> has now two simple public <tt>add</tt> methods. In order to imitate the method signitures in Java we would have to create a couple of helper classes and interfaces. It would make the code less compact but this shouldn&#8217;t be a serious problem. Creation of the various components is just as the binding extracted in two create Methods. The <tt>createPersonPanel</tt> has now only 5 lines of code. Adding another property to the form should be trivial. Changing the extremely simplistic layout should be trivial and is at least limited to a single small class. I think this is pretty much OK for a first step toward clean swing code. So I leave it like it is right now. Although I do have further plans for this.</p>
<p>I hope most of you agree that the code is much easier to understand and maintain in the form it is right now. But is it really worth the effort? Some might say no. If the whole application would consist of only this little panel. I would agree. But a Swing application typically does not have a single panel with two textfields and a button. But tens or even hundreds of panels. Many consisting of large collections of components. If you have to maintain such a monster, would you prefer createPersonPanel methods like the last one, or would you prefer the first version? What if the customer if finally fed up with your crappy layout and insists on proper spacing between the labels and the <tt>JTextFields</tt> or other components?</p>
<p>All this is pretty nice when you start a new Swing application. But what if you have an existing Swing application? One with convoluted code just as the Swing Tutorial taught you? Well &#8230; start changing it now. Don&#8217;t sit down for two months and rewrite all your code, but find pieces of code duplication and extract them. It will be a long way, but you wont reach the end if you don&#8217;t start walking.</p>
<p>But what if you are not using Swing, but writing a web application? Well it really shouldn&#8217;t matter much. Your PanelBuilder might be written in JavaScript, or create HTML, but the principle is the same: Seperate creation of components, layout of components and binding of components to properties and actions.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2011/06/26/clean-code-with-swing-and-scala/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>New Project&#8217;s Resolution</title>
		<link>http://blog.schauderhaft.de/2010/01/10/new-projects-resolution/</link>
		<comments>http://blog.schauderhaft.de/2010/01/10/new-projects-resolution/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 10:41:07 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[resolution]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[toxicity]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=372</guid>
		<description><![CDATA[In a couple of days I&#8217;ll start working on a new project. Actually it is an project that I worked one or two years ago. I think I did decent job last time. But there is always lots of room for improvement. So today I want to list a couple of things I want to [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_374" class="wp-caption alignleft" style="width: 310px"><a href="http://www.sxc.hu/photo/1216962"><img class="size-medium wp-image-374" title="Fireworks" src="http://blog.schauderhaft.de/wp-content/uploads/2010/01/1216962_13713831-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Fireworks</p></div>
<p>In a couple of days I&#8217;ll start working on a new project. Actually it is an project that I worked one or two years ago. I think I did decent job last time. But there is always lots of room for improvement. So today I want to list a couple of things I want to do bettern than the last time.</p>
<p><strong>More Tests</strong> &#8211; When working on that project I was the only person with real experience in automated testing. I am proud that we had a test suite and that this test suite still exists and put to good use and actually grew during the last months. Yet I consider the coverage of the tests not sufficient by far. So for every code I write, I will add an extensive set of test. I&#8217;ll also add tests for existing code, when refactoring.</p>
<p><strong>Earlier Tests</strong> &#8211; One reason we didn&#8217;t produce as many tests as I would wish today is that we didn&#8217;t do TDD. It probably was a correct decission the last time, since it would have been just to much. But this time I will practice TDD as good as I can.</p>
<p><strong>Cleaner Code</strong> &#8211; One of the first tests I wrote back then was a test to ensure that we don&#8217;t have circular dependencies. We succeeded in this, but still parts of the system a pretty tangled. We gonna improve that. Appart from the knowledge I took from <a href="http://www.amazon.com/gp/product/0132350882?ie=UTF8&amp;tag=schauderhaft-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0132350882">Clean Code</a> I&#8217;ll plan to use the <a href="http://erik.doernenburg.com/2008/11/how-toxic-is-your-code/">toxicity metric</a> as an important tool for that.</p>
<p><strong>Dependency Injection</strong> &#8211; We considered using Spring and decided against it, because we already had about 20 libraries we hardly knew in the project. Again at that time it was a reasonable decision. But in the meantime I learned what we could have gained by using Spring. I&#8217;ll propose using it, but even when the team declines again, I&#8217;ll be able to use the concept in order to improve the code base.</p>
<p>So these are the things I want to improve on in the next project. What are the things you want to improve on in your next project?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/01/10/new-projects-resolution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

