<?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>I See Dead Code &#187; java</title>
	<atom:link href="http://shlomme.diotavelli.net/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://shlomme.diotavelli.net</link>
	<description>… as sounding brass, or a tinkling cymbal.</description>
	<lastBuildDate>Sun, 11 Dec 2011 00:53:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Rescuing Generics</title>
		<link>http://shlomme.diotavelli.net/2008/01/12/rescuing-generics/</link>
		<comments>http://shlomme.diotavelli.net/2008/01/12/rescuing-generics/#comments</comments>
		<pubDate>Sat, 12 Jan 2008 15:54:04 +0000</pubDate>
		<dc:creator>shlomme</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[lang:en]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://shlomme.diotavelli.net/2008/01/12/rescuing-generics/</guid>
		<description><![CDATA[This is the first part in what is planned to be a loosely-coupled series of articles on current developments in mainstream programming languages. Topics include: Evolution of Java New abstractions in programming languages The functional turn Scala: „The next programming language™” Generics in Java When I started to program Java 5 professionally after some years [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first part in what is planned to be a loosely-coupled series of articles on current developments in mainstream programming languages.</p>
<p>Topics include:</p>
<ul>
<li>Evolution of Java</li>
<li>New abstractions in programming languages</li>
<li>The functional turn</li>
<li>Scala: „The next programming language™”</li>
</ul>
<h2>Generics in Java</h2>
<p>When I started to program Java 5 professionally after some years of blissful absence from the Java world, I thought myself to be well-prepared for generics. After all, I had done metaprogramming in both C++ and Python for several years.</p>
<p>Of course, experience never saves you from the perils of learning. It took some time until I finally <i>got</i> generics, including the common misunderstandings about covariance and the like. Fortunately, in the project I was working on at the time, we were allowed to go wild and try out all new features in Java 5 at length. We were the first ones to work with the new version and also carrying out the internal training, so we really had to understand what generics were about, and why all tutorials usually contain more <i>don&#8217;t</i>s then <i>do</i>s.</p>
<p>When I finally understood them, I was really disappointed. The type system wasn&#8217;t generic at all! Type annotations are just some sugary coating stripped out by the compiler after the program passes the type checks. Still, generics proved to be useful from time to time. Some problems just kept coming back, and I will briefly outline them there.</p>
<h5>The <i>ugly cast</i></h5>
<p>There is going to be an <i>ugly cast</i> at some point, where you (the programmer) know more about the static or runtime type of an object than the compiler. Our strategy was to isolate <i>ugly casts</i> in minimally small methods with <tt>@SuppressWarnings("unchecked")</tt> annotations. One prominent example is serialization:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> foo<span style="color: #009900;">&#40;</span><span style="color: #003399;">ObjectInputStream</span> i<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> i.<span style="color: #006633;">readObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h5>Generic types + class objects</h5>
<p>A lot of generics pain is remedied when you always hand around <tt>Class&lt;V&gt;</tt> objects whenever you create instances of classes with generic type arguments. This is often cumbersome, as it tends to make your APIs larger, but at least provides some kind of runtime type safety.</p>
<p>We used this often enough to call it a pattern, though I think we never gave it a proper name.</p>
<h5>Sun&#8217;s Java compiler</h5>
<p>We were in for some hard lessons when we found out that Eclipse&#8217;s Java compiler was much better at type inference than Sun&#8217;s <tt>javac</tt>. These kinds of errors were especially hard to track down, and some of them were unfixed at least up to 1.5.0 Update 12.<sup><a href="http://shlomme.diotavelli.net/2008/01/12/rescuing-generics/#footnote_0_80" id="identifier_0_80" class="footnote-link footnote-identifier-link" title="most of them are fixed in Java 6">1</a></sup> </p>
<h2>The backlash</h2>
<p>Now, after Java generics have been in the wild for a little more than three years and presumably have seen a wider adoption, a backlash is forming. While early coverage was mostly apologetic of all the oddities that had to be introduced to keep bytecode compatibility<sup><a href="http://shlomme.diotavelli.net/2008/01/12/rescuing-generics/#footnote_1_80" id="identifier_1_80" class="footnote-link footnote-identifier-link" title="see Generics gotchas for a good example">2</a></sup>, a lot of complaints about the (perceived) complexity of generics is heard.</p>
<p>Before I&#8217;m going to dive into an example, let me state the following:</p>
<ul>
<li>Yes, Java code does get uglier and less readable with generics,<br /> <i>…but a lot of that could be addressed with typedef&#8217;s.</i></li>
<li>Yes, generics have a lot of gotchas,<br /> <i>…but most of them are due to backwards compatibility. I would have liked to hear the millions of IDE monkeys cry in horror if BC had been broken.</i></li>
<li>Yes, generics are difficult to grasp.<br /> <i>Get over it. Seriously.</i></li>
</ul>
<h2>Killing Wildcards</h2>
<p>In his recent article <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=222021">„Simplifying Java Generics by Eliminating Wildcards”</a>, Robert Lovatt argues that Java generics could be simplified by removing wildcards altogether and make covariant generic types the default behavior, similar to arrays.</p>
<p>Please note that the following code examples assume that you have read the article.</p>
<p>In arrays, we observe the following behavior:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">List</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> listArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">List</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Collection</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> collArray <span style="color: #339933;">=</span> listArray<span style="color: #339933;">;</span>
collArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashSet</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;">// will result in an ArrayStoreException</span></pre></div></div>

<p>His argument is that this behavior could simply be adopted for generics, making this code compile:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">List<span style="color: #339933;">&lt;</span>List<span style="color: #339933;">&gt;</span> listList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>List<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
List<span style="color: #339933;">&lt;</span>Collection<span style="color: #339933;">&gt;</span> collList <span style="color: #339933;">=</span> listList<span style="color: #339933;">;</span>
collList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashSet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, suppose the compiler would accept this piece of code (which it doesn&#8217;t), how should an exception similar to <tt>ArrayStoreException</tt> be thrown? The generic types are not known at runtime, in contrast to arrays<sup><a href="http://shlomme.diotavelli.net/2008/01/12/rescuing-generics/#footnote_2_80" id="identifier_2_80" class="footnote-link footnote-identifier-link" title="see the documentation of Class.getComponentType()">3</a></sup> , since they couldn&#8217;t be added without braking backwards compatibility. The only way to ensure the type safety is to have the class object inside <tt>List</tt> and check if newly added objects have the correct type, laying the burden of type checking on the class designer. While this may be acceptable for the standard library, it is certainly not acceptable for general usage.</p>
<p>An example taken from Lovatt&#8217;s article to display the lacking power of generics in Java (and Scala) is:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"> ListScalaStyle<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> iList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ListScalaStyle<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 ListScalaStyle<span style="color: #339933;">&lt;</span>Number<span style="color: #339933;">&gt;</span> nList <span style="color: #339933;">=</span> iList.<span style="color: #006633;">prepend</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Number</span><span style="color: #009900;">&#41;</span><span style="color: #cc66cc;">2.0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// OK</span>
 ListScalaStyle<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> iList2 <span style="color: #339933;">=</span> nList.<span style="color: #006633;">tail</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;">// Error, still a Number list</span></pre></div></div>

<p>This is exactly the pathological case of the <i>ugly cast</i>. You, the programmer, know the static type of something and expect the compiler to be able to infer it as well.</p>
<p>To show why this cannot work in general, I&#8217;ll use a trick I&#8217;ve found to be very helpful: adding a little bit of randomization.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> ListScalaStyle<span style="color: #339933;">&lt;</span>Number<span style="color: #339933;">&gt;</span> getListWithTail<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;">if</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> .5<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ListScalaStyle<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> iList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ListScalaStyle<span style="color: #339933;">&lt;</span>Integer<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: #000000; font-weight: bold;">return</span> iList.<span style="color: #006633;">prepend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Number</span><span style="color: #009900;">&#41;</span><span style="color: #cc66cc;">2.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    ListScalaStyle<span style="color: #339933;">&lt;</span>Double<span style="color: #339933;">&gt;</span> iList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ListScalaStyle<span style="color: #339933;">&lt;</span>Double<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: #000000; font-weight: bold;">return</span> iList.<span style="color: #006633;">prepend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Number</span><span style="color: #009900;">&#41;</span><span style="color: #cc66cc;">2.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #666666; font-style: italic;">// can never work</span>
ListScalaStyle<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> iList2 <span style="color: #339933;">=</span> nList.<span style="color: #006633;">tail</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;">// Error, still a Number list</span></pre></div></div>

<p>This little example is of course not a total refutation &#8211; having the compiler being able to infer more type information statically might always be useful. However, it will always be limited to small pieces of code. It also forces the compiler to actually examine the bytecode of functions in order to see the flow of objects, because <tt>prepend</tt> might do something wildly different. This removes many advantages of polymorphism, a technique at the very heart of Java.</p>
<h2>Conclusion and Outlook</h2>
<p>With generics, Java gets more complicated. It allows programmers to make interesting abstractions, but also freely hands out all kinds of guns for shooting yourself in the foot. This is definitely a deviation from Java&#8217;s original design principles and, ironically, makes it a bit more like C++ &#8211; something which the designers tried to avoid as hard as possible.</p>
<p>In the upcoming articles, we will examine the question why having generics this way still might be a good idea (though for totally different reasons), why the Java designers did it in the first place, and what the generics <i>disaster</i> (Tim Bray) teaches us about the design and evolution of programming languages.</p>
<ol class="footnotes"><li id="footnote_0_80" class="footnote">most of them are fixed in Java 6</li><li id="footnote_1_80" class="footnote">see <a href="http://www.ibm.com/developerworks/java/library/j-jtp01255.html">Generics gotchas</a> for a good example</li><li id="footnote_2_80" class="footnote">see the documentation of <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#getComponentType()"><tt>Class.getComponentType()</tt></a></li></ol>]]></content:encoded>
			<wfw:commentRss>http://shlomme.diotavelli.net/2008/01/12/rescuing-generics/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Not exactly a free lunch, but maybe a discounted dinner?</title>
		<link>http://shlomme.diotavelli.net/2007/08/30/not-exactly-a-free-lunch-but-maybe-a-discounted-dinner/</link>
		<comments>http://shlomme.diotavelli.net/2007/08/30/not-exactly-a-free-lunch-but-maybe-a-discounted-dinner/#comments</comments>
		<pubDate>Thu, 30 Aug 2007 19:59:42 +0000</pubDate>
		<dc:creator>shlomme</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[lang:de]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://shlomme.diotavelli.net/2007/08/30/not-exactly-a-free-lunch-but-maybe-a-discounted-dinner/</guid>
		<description><![CDATA[Im Licht der neulichen Python-Experimente muss man die Concurrency-Bibliothek von Java 1.5 [↪] sehr lobend erwähnen. Wenn man das manuelle Erstellen von Threads und das mühsame Kommunizieren über Locks oder blocking queues gewöhnt ist, dann ist der CompletionService zusammen mit Callables eine sehr elegante und codesparende API, um parallele Programme im producer-consumer scheme zu implementieren. [...]]]></description>
			<content:encoded><![CDATA[<p>Im Licht der neulichen <a href="http://shlomme.diotavelli.net/2007/08/26/i-have-two-cores-wheres-my-free-lunch/">Python-Experimente</a> muss man die Concurrency-Bibliothek von Java 1.5 [<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-summary.html">↪</a>] sehr lobend erwähnen. Wenn man das manuelle Erstellen von Threads und das mühsame Kommunizieren über Locks oder <i>blocking queues</i> gewöhnt ist, dann ist der <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/CompletionService.html">CompletionService</a> zusammen mit <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Callable.html">Callables</a> eine sehr elegante und codesparende API, um parallele Programme im <i>producer-consumer scheme</i> zu implementieren.</p>
<p>Der per-thread overhead ist natürlich so hoch, dass man keine Verdopplung der Geschwindigkeit erreicht, noch dazu ist der Code, den wir parallelisiert haben, zum Teil i/o-bound. Aber trotzdem hinterlässt die Benutzung doch das Gefühl, mit einer gut designten Bibliothek den eigenen Code eleganter gemacht zu haben. Und die Experimente mit den „richtigen” Mehrprozessormaschinen (anstatt meines schmalbrüstigen Core-Duo-Systems) stünden ja noch aus.</p>
]]></content:encoded>
			<wfw:commentRss>http://shlomme.diotavelli.net/2007/08/30/not-exactly-a-free-lunch-but-maybe-a-discounted-dinner/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

