<?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>try {} catch () &#187; OO</title>
	<atom:link href="http://blog.gerardin.info/archives/category/oo/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.gerardin.info</link>
	<description>Java, Agile, the Web and other nice things</description>
	<lastBuildDate>Tue, 14 Jun 2011 09:48:37 +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>Rant: don&#8217;t name interfaces like ISomething</title>
		<link>http://blog.gerardin.info/archives/315</link>
		<comments>http://blog.gerardin.info/archives/315#comments</comments>
		<pubDate>Fri, 24 Jul 2009 12:08:41 +0000</pubDate>
		<dc:creator>Olivier Gérardin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[hungarian notation]]></category>
		<category><![CDATA[naming conventions]]></category>

		<guid isPermaLink="false">http://blog.gerardin.info/?p=315</guid>
		<description><![CDATA[I see a trend lately in Java libraries and code that I thought was only plaguing Microsoft developers: naming interfaces like ISomething. I find this very disturbing so I thought I had to react. First thing: an interface is a type just like a class. What a type does is mainly to define the set [...]]]></description>
			<content:encoded><![CDATA[<p>I see a trend lately in Java libraries and code that I thought was only plaguing Microsoft developers: naming interfaces like <code>ISomething</code>. I find this very disturbing so I thought I had to react.</p>
<p>First thing: an interface is a <strong>type</strong> just like a class. What a type does is mainly to define the set of operations (methods) that are applicable to an instance of a class that implements this type. When dealing with references, the only thing the Java compiler cares about is the type of the reference, so that is knows if a method call is licit or not. It does not care if the type is a class or an instance. Think about the <code>instanceof</code> operator: despite what the awkward name suggests, it works with interfaces as well as classes, because what it actually tests is whether a given reference is of a particular type.</p>
<p>So, why shouldn&#8217;t you name your interfaces beginning with the letter I?</p>
<ul>
<li>it breaks a fundamental OO rule, which is that you should let the users of your code know as little as possible about the implementation. If a method returns a object of type Foo, it doesn&#8217;t make any difference to the caller of this method if Foo is actually a class or en interface.</li>
<li>it is redundant. If you really need to know, the documentation (written or Javadoc), or your IDE&#8217;s lookup mechanism, will tell you if it&#8217;s an interface or not</li>
<li>it makes code harder to read. Which one do you prefer:</li>
</ul>
<p><code><br />
IFoo foo = getFoo();<br />
IBar bar = (IBar) foo.getStuff();<br />
</code><br />
or<br />
<code><br />
Foo foo = getFoo();<br />
Bar bar = (Bar) foo.getStuff();</code></p>
<p>I understand that developpers are often tempted to name interfaces like that because they know there will likely ever be only one implementation of this interface. So they have a Foo class, and it needs to implement an interface, why not name it IFoo?</p>
<p>Two points here:</p>
<ul>
<li>You don&#8217;t always need to define an interface for such a class. If you are configuring Spring beans for example, your bean references don&#8217;t need to be an interface type. Even if it is good practice to do so, there are cases (such as the one above) where it&#8217;s a perfectly valid case to use the class directly.</li>
<li>Why not name your interface Foo, and your implementation something else, like DefaultFoo or SimpleFoo or BaseFoo or whatever. Or maybe just Foo, provided it&#8217;s not in the same package! If you are using a factory, no-one will ever know or care how the concrete class is named anyway.</li>
</ul>
<p>Naming interfaces like ISomething is similar to <a href="http://en.wikipedia.org/wiki/Hungarian_notation" target="_blank" rel="nofollow" class="liwikipedia">including type name or scope in your variables names</a>. Yuck&#8230; Please don&#8217;t bring this to Java.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.gerardin.info/archives/315/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

