<?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>Xebia Blog &#187; Indexing access syntax for Lists and Maps</title>
	<atom:link href="http://blog.xebia.com/tag/indexing-access-syntax-for-lists-and-maps/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xebia.com</link>
	<description>Software development done right!</description>
	<lastBuildDate>Wed, 01 Feb 2012 00:30:00 +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>Dangerous new Language Features: Indexing access syntax for Lists and Maps</title>
		<link>http://blog.xebia.com/2009/06/20/dangerous-new-language-features-indexing-access-syntax-for-lists-and-maps/</link>
		<comments>http://blog.xebia.com/2009/06/20/dangerous-new-language-features-indexing-access-syntax-for-lists-and-maps/#comments</comments>
		<pubDate>Sat, 20 Jun 2009 16:19:44 +0000</pubDate>
		<dc:creator>Maarten Winkels</dc:creator>
		<br />
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/home/blog.xebia.com/www/wp-content/plugins/autometa/autometa.php</b> on line <b>303</b><br />
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Indexing access syntax for Lists and Maps]]></category>
		<category><![CDATA[jdk7]]></category>
		<category><![CDATA[project coin]]></category>

	<!-- AutoMeta Start -->
	<!-- AutoMeta End -->
	
		<guid isPermaLink="false">http://blog.xebia.com/?p=2081</guid>
		<description><![CDATA[In this blog I&#8217;ll talk about a new language feature proposed for Java7 by project coin and the problems I see with it. Maybe it is time to buy those Scala books and take a deep dive&#8230; Who has ever seen this construct before? int i, j; i = j = 10; Will this even [...]]]></description>
			<content:encoded><![CDATA[<p>In this blog I&#8217;ll talk about <a href="http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/001108.html">a new language feature</a> proposed for Java7 by <a href="http://openjdk.java.net/projects/coin/">project coin</a> and the problems I see with it.<br />
Maybe it is time to buy those Scala books and take a deep dive&#8230;<br />
<span id="more-2081"></span><br />
Who has ever seen this construct before?</p>
<pre lang="java">
  int i, j;
  i = j = 10;
</pre>
<p>Will this even compile? Ow, of course it will: the value of an assignment is the right hand side of the assignment and thus it can be reassigned to another variable. In my professional career I have only ever encountered such statements in frameworks written by people that are too smart for their own good. It takes a few seconds to sink in and then you realize that both variables will be set to the same value, no problem!</p>
<p>I don&#8217;t see the value of this &#8220;feature&#8221; of the Java language: splitting it over two lines will not harm the compiler, the JVM and definitely not the poor programmer that needs to read the lines of code after the genius that originally wrote them has left the building.</p>
<p>Recently I read a the proposal for a new feature in Jdk7 that will provide indexing access for Lists and Maps. The normal examples are quite easy to read or write:
<pre lang="java">
  List<String> names = ...
  names[0] = "Maarten Winkels";              // -> names.set(0, "Maarten Winkels");
  String you = names[1];                     // -> names.get(1);

  Map<String, String> residence = ...
  residence["Maarten Winkels"] = "India";    // -> residence.put("Maarten Winkels", "India");
  String r = residence["Koningin Beatrix"];  // -> residence.get("Koningin Beatrix");
</pre>
<p>This looks good at first sight. It is all syntax sugar, so nothing really fancy going on, but the code is shorter and easier to read, while it still adheres to a lot of good things like type safety (the list is still a list) and polymorphism (the index operators are defined on core Java interfaces and will work on every implementation).</p>
<p>The I read the more advanced example and something began to stir in my mind.</p>
<pre lang="java">
  residence["Maarten Winkels"] = residence["Koningin Beatrix"] = "Palace";
</pre>
<p>So what does this line of code tell you? You might have to look it over just one more time before you give your answer.</p>
<p>At first, your conclusion might be that the Queen (&#8220;=Koningin&#8221;) and I are going to share a Palace. This is what the normal &#8220;double&#8221; assignment means, right? Both variables will have the same value. Think again! The queen is going to move to a Palace and I&#8217;m left with wherever she lived before (which might be still oke&#8230;)! The is because the <span style="font-family: monospace">put</span> operation on a <span style="font-family: monospace">Map</span> actually returns the previously stored value. The code translates to:</p>
<pre lang="java">
  residence.put("Maarten Winkels", residence.put("Koningin Beatrix", "Palace"));
</pre>
<p>Which should make it clear that I&#8217;m going to have a different residence than the Queen. If there was no value associated to the Queen in the <span style="font-family: monospace">Map</span>, put returns <span style="font-family: monospace">null</span> and I might even be homeless!</p>
<p>I think this is a very bad feature. One might argue that the &#8220;double&#8221; assignment feature is not used very often, but my feeling is that a language should be as consistent to a reader as it can be and this to me clearly deviates from that path.</p>
<p>How can we solve this problem while maintaining consistency <i>and</i> backwards compatibility? I think that is really hard: We could make the assignment operator from the proposal <b>not</b> return a value (and return <span style="font-family: monospace">void</span> in stead, making it impossible to do a &#8220;double&#8221; assignment. This would, however, not be consistent with a normal assignment operator.</p>
<p>Should this caveat prevent this otherwise nice new feature to come to the Java language? I think we should really look at what we are trying to achieve with Java7. For me it would be more important to enable Java developers to easily read and fix existing code bases. If they can save a few characters, that is fine, but overall understandability of the language and ability to fix common bugs with new constructs (e.g. using closures for save resource usage) is far more important. Java (the language) should not add features just to be able to bring down the LoC of a program to the same level as Groovy or Ruby: For a large application, those are very short term goals. There are many options to integrate with these languages on the JVM anyway, so why do we need the Java language to compete with them?</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin-left: 10px;"><g:plusone size="small" count="1" href="http://blog.xebia.com/2009/06/20/dangerous-new-language-features-indexing-access-syntax-for-lists-and-maps/"></g:plusone></div><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.xebia.com%2F2009%2F06%2F20%2Fdangerous-new-language-features-indexing-access-syntax-for-lists-and-maps%2F&amp;title=Dangerous%20new%20Language%20Features%3A%20Indexing%20access%20syntax%20for%20Lists%20and%20Maps" id="wpa2a_2"><img src="http://blog.xebia.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.xebia.com/2009/06/20/dangerous-new-language-features-indexing-access-syntax-for-lists-and-maps/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  blog.xebia.com/tag/indexing-access-syntax-for-lists-and-maps/feed/ ) in 0.52129 seconds, on Feb 9th, 2012 at 7:44 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 9th, 2012 at 8:44 pm UTC -->
