<?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>22 idea street &#187; Ruby</title>
	<atom:link href="http://22ideastreet.com/blog/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://22ideastreet.com/blog</link>
	<description></description>
	<lastBuildDate>Thu, 17 May 2012 13:00:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>RR for Test Doubles Presentation</title>
		<link>http://22ideastreet.com/blog/2012/01/13/rr-for-test-doubles-presentation/</link>
		<comments>http://22ideastreet.com/blog/2012/01/13/rr-for-test-doubles-presentation/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 14:00:40 +0000</pubDate>
		<dc:creator>Anthony Panozzo</dc:creator>
				<category><![CDATA[External]]></category>
		<category><![CDATA[doubles]]></category>
		<category><![CDATA[mocks]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[rr]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[spies]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://22ideastreet.com/blog/?p=1243</guid>
		<description><![CDATA[Here is a presentation that I gave to the Indy.rb Ruby user group in Indianapolis. It covers the advantages of using RR (double Ruby) for concise mocking and stubbing and gives some real-life use cases to inspire thinking about testing using test doubles. (Having trouble seeing the slides? Try here.) Original article: RR for Test [...]<p><br/><br/>Original article:  <a href="http://22ideastreet.com/blog/2012/01/13/rr-for-test-doubles-presentation/">RR for Test Doubles Presentation</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Here is a presentation that I gave to the <a href="http://indyrb.org/">Indy.rb</a> Ruby user group in Indianapolis. It covers the advantages of using RR (double Ruby) for concise mocking and stubbing and gives some real-life use cases to inspire thinking about testing using test doubles.</p>
<p><iframe src="https://docs.google.com/present/embed?id=dhbvg2sf_110hck33xc6" frameborder="0" width="410" height="342"></iframe></p>
<p>(Having trouble seeing the slides? <a href="https://docs.google.com/present/embed?id=dhbvg2sf_110hck33xc6">Try here</a>.)</p>
<p><br/><br/>Original article:  <a href="http://22ideastreet.com/blog/2012/01/13/rr-for-test-doubles-presentation/">RR for Test Doubles Presentation</a></p>
]]></content:encoded>
			<wfw:commentRss>http://22ideastreet.com/blog/2012/01/13/rr-for-test-doubles-presentation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The State of Ruby and Testing</title>
		<link>http://22ideastreet.com/blog/2011/06/02/the-state-of-ruby-and-testing/</link>
		<comments>http://22ideastreet.com/blog/2011/06/02/the-state-of-ruby-and-testing/#comments</comments>
		<pubDate>Thu, 02 Jun 2011 19:54:00 +0000</pubDate>
		<dc:creator>Anthony Panozzo</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[jruby]]></category>
		<category><![CDATA[mocking]]></category>
		<category><![CDATA[ree]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[stubbing]]></category>
		<category><![CDATA[survey]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://22ideastreet.com/blog/?p=1135</guid>
		<description><![CDATA[At the May 2011 Indy.rb meetup, I suggested creating a survey to figure out what versions of Ruby people were using, and what testing stacks they use and would like to use. I created this survey and tweeted it out, and was impressed with the results! Over a hundred people filled out the information, from [...]<p><br/><br/>Original article:  <a href="http://22ideastreet.com/blog/2011/06/02/the-state-of-ruby-and-testing/">The State of Ruby and Testing</a></p>
]]></description>
			<content:encoded><![CDATA[<p>At the May 2011 <a href="indyrb.org">Indy.rb</a> meetup, I suggested creating a survey to figure out what versions of Ruby people were using, and what testing stacks they use and would like to use. I created this survey and tweeted it out, and was impressed with the results! Over a hundred people filled out the information, from several continents and numerous countries. Thanks to everyone who participated!</p>
<h2>The questions and their results</h2>
<ul>
<li>What versions of Ruby have you ever tried out?</li>
<li>What versions of Ruby do you currently use in production or for real apps?</li>
<li>What testing frameworks are your active projects using?</li>
<li>If you were starting a new Rails project right now, what testing frameworks would you use?</li>
<li>What mocking/stubbing frameworks are your active projects using?</li>
<li>If you were starting a new Rails project right now, what mocking/stubbing frameworks would you use?</li>
<li>What do your active projects use to populate testing data?</li>
<li>If you were starting a new Rails project right now, what would you use for populating testing data?</li>
</ul>
<hr/>
<p><b>What versions of Ruby have you ever tried out?</b><br />
<img src="http://d3r8zbeodql2nc.cloudfront.net/ruby_version_used.png" width="385" height="337"/><br />
Summary: a wide variety of Ruby versions used. What the heck is kiji, you might ask? This was a useful <a href="http://engineering.twitter.com/2011/05/faster-ruby-kiji-update.html">post on kiji</a>.</p>
<hr/>
<p><b>What versions of Ruby do you currently use in production or for real apps?</b><br />
<img src="http://d3r8zbeodql2nc.cloudfront.net/ruby_version_production.png" width="385" height="337"/><br />
Summary: Mostly 1.8.7 and 1.9.2 in production use right now. REE is production-ready. <a href="http://twitter.com/share" class="twitter-share-button" data-text="What are the most popular production Ruby environments?" data-count="none" data-via="panozzaj" data-related="indyrb:The Indianapolis Ruby Brigade">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></p>
<hr/>
<p><b>What testing frameworks are your active projects using?</b><br />
<img src="http://d3r8zbeodql2nc.cloudfront.net/active_testing_frameworks.png" width="385" height="337"/></p>
<hr/>
<p><b>If you were starting a new Rails project right now, what testing frameworks would you use?</b><br />
<img src="http://d3r8zbeodql2nc.cloudfront.net/wanted_testing_frameworks.png" width="385" height="337"/><br />
Conclusion: Expect to see MiniTest in more production apps in the future. <a href="http://twitter.com/share" class="twitter-share-button" data-text="MiniTest will be in more future production apps" data-count="none" data-via="panozzaj" data-related="indyrb:The Indianapolis Ruby Brigade">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></p>
<hr/>
<p><b>What mocking/stubbing frameworks are your active projects using?</b><br />
<img src="http://d3r8zbeodql2nc.cloudfront.net/active_mocking_frameworks.png" width="385" height="337"/></p>
<hr/>
<p><b>If you were starting a new Rails project right now, what mocking/stubbing frameworks would you use?</b><br />
<img src="http://d3r8zbeodql2nc.cloudfront.net/wanted_mocking_frameworks.png" width="385" height="337"/><br />
Conclusion: RSpec mocks are here to stay. <a href="http://twitter.com/share" class="twitter-share-button" data-text="Why RSpec mocks are here to stay" data-count="none" data-via="panozzaj" data-related="indyrb:The Indianapolis Ruby Brigade">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></p>
<hr/>
<p><b>What do your active projects use to populate testing data?</b><br />
<img src="http://d3r8zbeodql2nc.cloudfront.net/active_test_data.png" width="385" height="337"/></p>
<hr/>
<p><b>If you were starting a new Rails project right now, what would you use for populating testing data?</b><br />
<img src="http://d3r8zbeodql2nc.cloudfront.net/wanted_test_data.png" width="385" height="337"/><br />
Conclusion: Factory Girl and Machinist are going to remain popular. Rails fixtures are hanging around. <a href="http://twitter.com/share" class="twitter-share-button" data-text="Why Rails fixtures aren't going anywhere soon" data-count="none" data-via="panozzaj" data-related="indyrb:The Indianapolis Ruby Brigade">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></p>
<hr/>
<h4>The Data Source</h4>
<p>Here is the <a href="https://spreadsheets.google.com/spreadsheet/ccc?key=0AoI8YzLXy1J0dDQtN3pZOHFEZDhuQTVnTVpGTkJ4N1E&#038;hl=en_US">spreadsheet of results</a> (with contact information and bad rows stripped). I didn&#8217;t spend very much time making killer visualizations, and there are some great comments in there. Highly recommended to get a feel for what people are using. Someone else could probably create a correlation table of common tool sets. For example, showing that people who use RSpec commonly use a certain mocking framework. Also, there is geographic info there, so we can see which region is on the cutting edge&#8230; <img src='http://22ideastreet.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><b>Edit:</b> A note on the charts. People could respond with multiple answers, and I just tallied the answers for each category. For example, if 50 people said they used 1.8.6 and 1.8.7 and 50 people said 1.8.7, 1.8.7 would get 2/3 of the chart (100 &#8220;votes&#8221;), and 1.8.6 would get 1/3 (50 &#8220;votes&#8221;). The graphs could have been clearer, feel free to create a better visualization with the data. Thanks to the <a href="http://news.ycombinator.com/item?id=2662216">Hacker News commenters</a> for bringing this up.</p>
<h4>Logistics</h4>
<p>I took the data, and filtered it with this script:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
&nbsp;
types = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span>
  line.<span style="color:#9900CC;">strip</span>!
  types <span style="color:#006600; font-weight:bold;">+</span>= line.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;, &quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> line.<span style="color:#9900CC;">length</span> == <span style="color:#006666;">0</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
counts = <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
types.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>type<span style="color:#006600; font-weight:bold;">|</span>
  counts<span style="color:#006600; font-weight:bold;">&#91;</span>type.<span style="color:#9900CC;">downcase</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
counts.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>key, value<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{key}<span style="color:#000099;">\t</span>#{value}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Then, I put it in an Open Office spreadsheet, cleaned up the data a bit and sorted by the number descending. I should have probably outsourced this task, as it took awhile to get things looking right. It was terrible getting the chart images out of OO though. Had to copy to Draw, then export selection to an image file&#8230;</p>
<h4>What did I miss?</h4>
<p>Is your favorite testing framework not represented here? I left out some frameworks that only one person said. Check <a href="https://spreadsheets.google.com/spreadsheet/ccc?key=0AoI8YzLXy1J0dDQtN3pZOHFEZDhuQTVnTVpGTkJ4N1E&#038;hl=en_US">the spreadsheet</a> for all of the data and some great comments. If you want to fill out the survey after having read this, <a href="https://spreadsheets.google.com/spreadsheet/viewform?hl=en_US&#038;formkey=dHJIaFVmVnVrT0lhbnY3LVZ4Y0NoZkE6MQ#gid=0"> go ahead and do so here</a>. I might update this blog post at some point in the future or create a second one with updates&#8230;</p>
<p>What else could I have done, and do you want to be notified when future surveys take place? I&#8217;d imagine something in my survey process could have been improved. Leave a comment or email me at <a href="mailto:panozzaj@gmail.com">panozzaj@gmail.com</a>!</p>
<p><br/><br/>Original article:  <a href="http://22ideastreet.com/blog/2011/06/02/the-state-of-ruby-and-testing/">The State of Ruby and Testing</a></p>
]]></content:encoded>
			<wfw:commentRss>http://22ideastreet.com/blog/2011/06/02/the-state-of-ruby-and-testing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Introduction to Theories</title>
		<link>http://22ideastreet.com/blog/2008/10/20/introduction-to-theories/</link>
		<comments>http://22ideastreet.com/blog/2008/10/20/introduction-to-theories/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 15:00:25 +0000</pubDate>
		<dc:creator>Anthony Panozzo</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Specification]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Theories]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://22ideastreet.com/blog/?p=107</guid>
		<description><![CDATA[It seems like the more I read about different development models, the more I see overlap between the different paradigms. Even if you aren&#8217;t using one of them for a given project, just knowing about all of them makes your code better because you realize how to make your code more testable. One thing that [...]<p><br/><br/>Original article:  <a href="http://22ideastreet.com/blog/2008/10/20/introduction-to-theories/">Introduction to Theories</a></p>
]]></description>
			<content:encoded><![CDATA[<p>It seems like the more I read about different development models, the more I see overlap between the different paradigms.  Even if you aren&#8217;t using one of them for a given project, just knowing about all of them makes your code better because you realize how to make your code more testable.</p>
<p>One thing that has piqued my interest of late is theories in <a href="http://junit.sourceforge.net/doc/ReleaseNotes4.4.html">JUnit 4.4+</a>.</p>
<h4>Overview</h4>
<p>In JUnit, the still experimental theory functionality is derived from the open-source <a href="http://popper.tigris.org/">Popper project</a>.  The name comes from <a href="http://en.wikipedia.org/wiki/Karl_Popper">Karl Popper&#8217;s</a> work on the philosophy of science.  He was a major proponent of saying that something isn&#8217;t scientific unless it is falsifiable, and that theories cannot be proven correct, merely shown to have exceptions.  If someone can provide an exception, it means that the theory does not match all of our observations.  Theories are models of the world, so while a model may not be correct, it can still be useful.</p>
<p>So what are theories in software good for?  When using test-driven development, the tests serve as a form of code documentation.  However, there are certain properties of code that are known or assumed by the developers that are difficult to codify as simple tests.  Theories provide a simple and expressive means for at least the following cases:</p>
<li>identities and data conversion</li>
<li>globally disallowed behavior</li>
<li>data that should be preserved after performing an operation or set of operations</li>
<p>For example, you might have a theory that data that is serialized into XML and then unserialized should always result in the original object.  This seems to make sense, but it might be time-consuming or error-prone to specify all of the different tests that you have to consider.  A single theory can codify this assumption.</p>
<p>In JUnit, theories are a special case of parameterized test.  So instead of using the standard test that has no parameters, you add parameters to make testing various combinations of values shorter and easier.  You can then take these parameterized tests further by specifying data points to test for by default in an expressive manner.</p>
<h4>Example</h4>
<p>Alright enough theory.  What does a JUnit theory actually look like?  Given that you have a theory about two methods on a Currency object that should be inverses of each other, one nice example is:</p>
<pre>
    @Theory public void multiplyIsInverseOfDivide(
            @TestedOn(ints={0, 5, 10}) int amount,
            @TestedOn(ints={1, 2, 3})  int multiplier) {
        assertThat(new Currency(amount).times(multiplier).divideBy(multiplier).getAmount(), is(amount));
    }
</pre>
<p>On the first line, we declare that we are going to postulate a theory, and give it a unique name.  This method would typically live in a test suite or test file of some sort.  The next two lines are the parameters to the test method.  The parameters are prefixed by annotations to put in sample test values.  All permutations of these values will be attempted when running the theory, and the values will be reported for any failures so we know what happened when the theory failed.  On the last line, we test that the functions are indeed inverses by invoking them and making sure that the original value is the same as the end value.</p>
<h4>Going further</h4>
<p>So big deal, we could easily just create a helper test method that does something similar and we could pass whatever values to the method that we wanted.  Well, hold on a second.  One thing I haven&#8217;t mentioned yet is that there are some nifty tools that support automatically inserting values in to test theories.  The tools automate searching the theory space for members that fit the theory assumptions but cause the theory to fail.  Something like this might be useful in detecting an off-by-one error or corner case that you hadn&#8217;t considered while implementing.  Typically for development you would just run the test cases presented with the method, and then would search the theory parameter space with spare cycles.  This enables you to have great power as well as having blazing unit tests, which is key for any test-driven development.</p>
<p>If we allow automated searching of the theory space, there is one small problem with the example theory listed above.  Can you see it?</p>
<p>The assert attempts to multiply by the multiplier and then divide by the multiplier, so a zero value for the multiplier will cause a divide by zero error.  When we use automated tools for searching the theory space, we want to say that this theory only holds when the multiplier is not zero.  We can easily do this:</p>
<pre>
    @Theory public void multiplyIsInverseOfDivide(
            @TestedOn(ints={0, 5, 10}) int amount,
            @TestedOn(ints={1, 2, 3})  int multiplier) {
        <b>assumeThat(multiplier, not(0));</b>
        assertThat(new Currency(amount).times(multiplier).divideBy(multiplier).getAmount(), is(amount));
    }
</pre>
<p>Similar expressions exist for ensuring the value is positive, and so forth.  You might create a different test or theory to show the expected behavior when the multiplier is zero.</p>
<p>I really liked the use of <a href="http://www.developer.com/java/other/article.php/3556176">Java annotations</a> to make this code considerably more readable.  You would probably need a runner of some sort to specify the values if you didn&#8217;t do it right there in the method signature.</p>
<p>It seems like there is some overlap between theories and contracts, but I won&#8217;t go into that at this time.  Theories exert positive design pressures like tests do, in that they tend to lead to leaner code that just does what the theory asks for.</p>
<h4>Extensions</h4>
<p>In Ruby on Rails, it would be nice to be able to easily test methods with multiple models that you have defined.  For example, you might have a theory:</p>
<pre>
  fixtures :users

  def test_admin_user_can_access_this_page
    login(users(:admin_user_1))
    get :this_page
    assert_response :success
  end

  # the previous test, but better
  def theory_that_admin_users_can_access_this_page(User user)
    assume(user.is_a? AdminUser)
    login(user)
    get :this_page
    assert_response :success
  end
</pre>
<p>So, instead of using just one user or manually going through a set of users, you could just specify a whole bunch of fixtures and go to town.  This might make fixtures less brittle by pointing out subtle interactions between parameters that would cause the theory to fail.  I&#8217;m not sure how the syntax would look, and I&#8217;m not sure how you could use something better than annotations in Ruby.</p>
<p>Another thing that might be nice would be to say:  I have a theory that any strings we put to the page from the database will always be escaped.  The automated theory explorer could step through the pages that you define and try once with some basic data and then start generating HTML and JavaScript garbage and ensure that nothing gets through to the users.  This might be a nice way of preventing cross-site scripting attacks (although I would imagine that there are tools that already exist for just this purpose.)</p>
<p>I looked, but could not find a Ruby plugin to handle theories.  Hmmm&#8230;  <img src='http://22ideastreet.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h4>Further reading</h4>
<p>Some excellent resources are:<br />
<a href="http://popper.tigris.org/tutorial.html">The Popper tutorial</a><br />
<a href="http://shareandenjoy.saff.net/2006/12/new-paper-practice-of-theories.html">A great paper with examples of use and benefits</a> (see the pdf on that page)<br />
<a href="http://groups.csail.mit.edu/pag/pubs/test-theory-demo-oopsla2007.pdf">General overview of Theories in JUnit [pdf]</a></p>
<p><br/><br/>Original article:  <a href="http://22ideastreet.com/blog/2008/10/20/introduction-to-theories/">Introduction to Theories</a></p>
]]></content:encoded>
			<wfw:commentRss>http://22ideastreet.com/blog/2008/10/20/introduction-to-theories/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Anti-Pattern</title>
		<link>http://22ideastreet.com/blog/2008/09/25/java-anti-pattern/</link>
		<comments>http://22ideastreet.com/blog/2008/09/25/java-anti-pattern/#comments</comments>
		<pubDate>Fri, 26 Sep 2008 02:29:19 +0000</pubDate>
		<dc:creator>Anthony Panozzo</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://22ideastreet.com/blog/?p=13</guid>
		<description><![CDATA[I was doing some Java programming today, and I noticed an anti-pattern that was in an existing code base. It went something like the following: public Class Klass &#123; ... &#160; public Klass&#40;int foo, boolean flag, String s1, String s2, String s3, String s4&#41; &#123; initialize&#40;foo, flag, s1, s2, s3, s4&#41;; &#125; &#160; public Klass&#40;int [...]<p><br/><br/>Original article:  <a href="http://22ideastreet.com/blog/2008/09/25/java-anti-pattern/">Java Anti-Pattern</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I was doing some Java programming today, and I noticed an anti-pattern that was in an existing code base.  It went something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">Class</span> Klass <span style="color: #009900;">&#123;</span>
    ...
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Klass<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> foo, <span style="color: #000066; font-weight: bold;">boolean</span> flag, <span style="color: #003399;">String</span> s1, <span style="color: #003399;">String</span> s2, <span style="color: #003399;">String</span> s3, <span style="color: #003399;">String</span> s4<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        initialize<span style="color: #009900;">&#40;</span>foo, flag, s1, s2, s3, s4<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Klass<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> foo, <span style="color: #000066; font-weight: bold;">boolean</span> flag, <span style="color: #003399;">String</span> s1, <span style="color: #003399;">String</span> s2, <span style="color: #003399;">String</span> s3<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        initialize<span style="color: #009900;">&#40;</span>foo, flag, s1, s2, s3, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Klass<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> foo, <span style="color: #000066; font-weight: bold;">boolean</span> flag, <span style="color: #003399;">String</span> s1, <span style="color: #003399;">String</span> s2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        initialize<span style="color: #009900;">&#40;</span>foo, flag, s1, s2, <span style="color: #000066; font-weight: bold;">null</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;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Klass<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> foo, <span style="color: #000066; font-weight: bold;">boolean</span> flag, <span style="color: #003399;">String</span> s1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        initialize<span style="color: #009900;">&#40;</span>foo, flag, s1, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</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;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Klass<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> foo, <span style="color: #000066; font-weight: bold;">boolean</span> flag<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        initialize<span style="color: #009900;">&#40;</span>foo, flag, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</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;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> initialize<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> foo, <span style="color: #000066; font-weight: bold;">boolean</span> flag, <span style="color: #003399;">String</span> s1, <span style="color: #003399;">String</span> s2, <span style="color: #003399;">String</span> s3, <span style="color: #003399;">String</span> s4<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        _foo <span style="color: #339933;">=</span> foo<span style="color: #339933;">;</span>
        _flag <span style="color: #339933;">=</span> flag<span style="color: #339933;">;</span>
        _s1 <span style="color: #339933;">=</span> s1<span style="color: #339933;">;</span>
        _s2 <span style="color: #339933;">=</span> s2<span style="color: #339933;">;</span>
        _s3 <span style="color: #339933;">=</span> s3<span style="color: #339933;">;</span>
        _s4 <span style="color: #339933;">=</span> s4<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    ...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Immediately, my <a href="http://c2.com/cgi/wiki?DontRepeatYourself">Don&#8217;t Repeat Yourself</a> sensor went off.  It felt like the previous coder was essentially trying to use some default values for a method.  You can do something like this in other languages, even other statically typed ones.  In Ruby, it would be pretty simple:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> klass
  ...
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>foo, flag, s1 = <span style="color:#0000FF; font-weight:bold;">nil</span>, s2 = <span style="color:#0000FF; font-weight:bold;">nil</span>, s3 = <span style="color:#0000FF; font-weight:bold;">nil</span>, s4 = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    ...
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  ...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>However, I looked online and saw several instances of people saying &#8220;you can&#8217;t do that in Java.&#8221;  So how else can we reduce this duplication?</p>
<p>One way that was suggested was to make better use of the object itself:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">Class</span> Klass <span style="color: #009900;">&#123;</span>
    ...
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Klass<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> foo, <span style="color: #000066; font-weight: bold;">boolean</span> flag, <span style="color: #003399;">String</span> s1, <span style="color: #003399;">String</span> s2, <span style="color: #003399;">String</span> s3, <span style="color: #003399;">String</span> s4<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        _foo <span style="color: #339933;">=</span> foo<span style="color: #339933;">;</span>
        _flag <span style="color: #339933;">=</span> flag<span style="color: #339933;">;</span>
        _s1 <span style="color: #339933;">=</span> s1<span style="color: #339933;">;</span>
        _s2 <span style="color: #339933;">=</span> s2<span style="color: #339933;">;</span>
        _s3 <span style="color: #339933;">=</span> s3<span style="color: #339933;">;</span>
        _s4 <span style="color: #339933;">=</span> s4<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Klass<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> foo, <span style="color: #000066; font-weight: bold;">boolean</span> flag, <span style="color: #003399;">String</span> s1, <span style="color: #003399;">String</span> s2, <span style="color: #003399;">String</span> s3<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#40;</span>foo, flag, s1, s2, s3, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Klass<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> foo, <span style="color: #000066; font-weight: bold;">boolean</span> flag, <span style="color: #003399;">String</span> s1, <span style="color: #003399;">String</span> s2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#40;</span>foo, flag, s1, s2, <span style="color: #000066; font-weight: bold;">null</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;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Klass<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> foo, <span style="color: #000066; font-weight: bold;">boolean</span> flag, <span style="color: #003399;">String</span> s1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#40;</span>foo, flag, s1, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</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;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Klass<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> foo, <span style="color: #000066; font-weight: bold;">boolean</span> flag<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#40;</span>foo, flag, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</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;">&#125;</span>
&nbsp;
    ...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This at least takes away the initialization method.  I suppose you could make it shorter by having each constructor call the one immediately prior.  You would remove a few nulls here and there.  However, there is a deeper problem here, which was demonstrated in the code I was actually working with.  What if we wanted to add even more strings?  Why not just store the strings in an array?  But if we have one string, do we need to store it in an array?</p>
<p>Ruby has a solution for this problem&#8211;the splat (*) operator:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>foo, flag, <span style="color:#006600; font-weight:bold;">*</span>rest<span style="color:#006600; font-weight:bold;">&#41;</span>
    foo = foo
    flag = flag
    strings = <span style="color:#006600; font-weight:bold;">*</span>rest
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>If the <code>rest</code> parameter is nil, then <code>strings</code> will be nil.  If the <code>rest</code> parameter is just a single value, like &#8220;hello&#8221;, then <code>strings</code> will be ["hello"].  It follows that if multiple values are passed in, then <code>strings</code> will contain an array of those values.</p>
<p>My first instinct was that Java did not have a construct that would allow us to do the same thing.  However, a search revealed that there was an operator in Java that would allow us to do something like Ruby&#8217;s args or splat.  Enter the ellipsis (&#8230;) feature, first released in Java 1.5:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">Class</span> Klass <span style="color: #009900;">&#123;</span>
    ...
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Klass<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> foo, <span style="color: #000066; font-weight: bold;">boolean</span> flag, <span style="color: #003399;">String</span>... <span style="color: #006633;">strings</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        _foo <span style="color: #339933;">=</span> foo<span style="color: #339933;">;</span>
        _flag <span style="color: #339933;">=</span> flag<span style="color: #339933;">;</span>
        _strings <span style="color: #339933;">=</span> strings<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    ...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ahh, that makes me happy.  We went from about twenty lines to five with a single trick.  Now, if there is some reason that only four strings can be allowed, we can do some additional parameter checking, but it seems like this is considerably more robust already.  Like in Ruby, the varargs feature can only be used at the end of the method signature.</p>
<p>There were other parts of the codebase that had something like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">Class</span> Klass <span style="color: #009900;">&#123;</span>
    ...
    <span style="color: #000000; font-weight: bold;">public</span> Klass<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> string1, <span style="color: #003399;">Shape</span> shape1, <span style="color: #003399;">String</span> string2, <span style="color: #003399;">Shape</span> shape2, <span style="color: #003399;">String</span> string3, <span style="color: #003399;">Shape</span> shape3<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ...
    <span style="color: #009900;">&#125;</span>
    ...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Where each String refers somehow to the associated Shape.  In this case, you could just use a <a href="http://java.sun.com/javase/6/docs/api/java/util/HashMap.html">HashMap with generics</a>, as in <code>HashMap&lt;String, Shape&gt;</code>.</p>
<p>You probably already knew all of this, but I had never used the varargs feature before in Java.  Hope this helped.</p>
<p><br/><br/>Original article:  <a href="http://22ideastreet.com/blog/2008/09/25/java-anti-pattern/">Java Anti-Pattern</a></p>
]]></content:encoded>
			<wfw:commentRss>http://22ideastreet.com/blog/2008/09/25/java-anti-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

