views:

437

answers:

12

Many years ago when I was at uni they said to put a capital i (I) in front of interfaces. Is this still a convention because I see many interfaces that do not follow this.

+11  A: 

That is not typically done in Java - it's a C#/.NET thing.

Personally, I dislike it since I think it leaks information that should not be leaked. Code should be written agnostic as to whether an object is being handled via an interface or directly via a class API.

Software Monkey
It's likely that the lecturers picked up that convention from a language they used previously and just assumed it was a Java convention too and taught it as such.
David
It's funny that Microsoft did away with Hungarian notation for almost everything but still feel they need to tell you by looking at the name that it's an interface.
Eric J.
It's quite common in some Java libraries (e.g., Eclipse's SDK)
Uri
This information must necessarily be "leaked" to allow people to consider whether to _extend_ or _implement_ the type, don't you think?
zneak
JavaDoc does still separate out interfaces, but the list frame doesn't tell you whether a class is, say, abstract.
Tom Hawtin - tackline
@zneak: JavaDoc already adequately makes this distinction... it's leaking it into the name that doesn't sit well with me. But that may be just because I have always detested systems-hungarian notation.
Software Monkey
@zneak: It leaks when what is returned to a caller is the interface class instead of the implementing class. I really don't think that a caller of getInputStream() and getOutputStream() should see IInputStream and IOutputStream in a world where InputStream and OutputStream had been correctly defined as interfaces.
Software Monkey
@Uri No, it's not common. There are just some exceptions, like Eclipse.
Pascal Thivent
You may call that unnecessary information—but please, that's not "leaked" information. There are thousands of reasons you could invoke to tell me prefixing interface names with an I is bad; but leaking information makes no sense here. How can it be leaked if it's otherwise publicly available?
zneak
@zneak "leak" in this context makes sense to me. The letter is 'spilling' information at a time when that information should be withheld. When my water bottle leaks, it is also spilling its water at a time it should not be spilled, even though the water in the bottle is available for me when i request it. eh? OK maybe "leak" has a connotation of 'reveal a secret' in some contexts, but as a general purpose metaphor here I approve.
Segfault
A: 

This is certainly the .NET convention, and Microsoft does this with their own interfaces in the .NET base class library. It was the Java convention when I did Java, and can't imagine it has changed, though I am not up to date with Java.

As an aside in C++ we also always used to prefix with an 'I' and indeed we always used to prefix classes with 'C'. We didn't carry this 'C' convention over to .NET.

MrLane
This is incorrect - it has never even been close to being a general convention among Java programmers... perhaps you meant "It was [not] the Java convention..."??
Software Monkey
Then I stand corrected. I guess it was something we always did and I assumed it was convention.
MrLane
+1  A: 

It is a familiar programming convention, but its prevalence depends on the API. For example, it is generally not followed by the Java standard library, where things like collections are interfaces, but are named as their mathematical concepts. On the other hand, some important APIs like Eclipse use it consistently.

One argument I heard against using the prefix is that one is essentially placing a language issue (i.e., the dicothomy of interfaces and classes) into the naming scheme. Another is that "everything in a public API should be an interface and not a class anyway". Since many classes that implement interface are named "XImpl", one could argue that it may be superfluous. However, using the prefix may make sense if the type is merely a marker.

Uri
the eclipse API?
Woot4Moo
@Woot4Moo: Are you arguing that it is not important, or that it is not an API? They certainly refer to it as an API.
Uri
I have never heard eclipse called an API. I have heard it called an IDE and even an SDK(which is suspect at best)
Woot4Moo
@Woot4Moo: In my view, Eclipse is a platform; it comes packaged with plug-ins that make it an IDE for specific languages, but it has other applications. Either way, in many places the community refers to an Eclipse API. e.g.,: http://wiki.eclipse.org/API_Central
Uri
@Woot4M00 I assume he means SWT and eclipse plugin SDK.
KitsuneYMG
+7  A: 

No, this is not convention. At least it isnt within the JDK. That said, if your shop has this as a convention, even though it might not be practice on the outside, I would suggest that you follow suit. Keeping consistency within a team is more important with regard to conventions.

akf
+1  A: 

Don't! Think about others when they read your code!

glebm
plus you should not be using interfaces, don't be elitist.
01
A: 

In Java the convention is to try and end your interfaces in "able". Serializable, Cloneable, and so on. In .NET they begin with "I".

I'd stick with the approach that is standard within your language (i.e. try "able" extension). I disagree with Software Monkey that it "leaks information that should not be leaked". It's perfectly fine to have a name that is indicative of what type of thing it is, IMHO.

Noon Silk
Just like Listable... oh wait
Woot4Moo
I don't think that there's really an "able" extension. It's more about making it an adjective or a role rather than an operation (e.g., Listener, Visitor, etc.)
Uri
based on the naming convention of interfaces, something that is able to be listed would be listable or ible not sure which but it ends with ble :)
Woot4Moo
Not every interface in the JDK ends with "able". Consider `Collection`, `Map`, `Set`, `List`, `Iterator`, `ListIterator`, `CharSequence`, every interface in the `java.sql` package. I could go on...
Asaph
@Asaph: Are you for real? When did I say that every interface would end with that? I agree with @Uri though, his explanation is better.
Noon Silk
@silky: Forgive my apparent misinterpretation of the first line of your answer. BTW. I was not the one who downvoted. I only commented.
Asaph
I think cooler thing about Java is using surfix. like BlueListener and RedListener. So later its harder to find under ctrl+space :) harder is better.
01
+4  A: 

It's used quite a lot in the Eclipse Framework. It depends on individual project styles, but it is not standard convention. However, it certainly can help code maintenance and searching in some cases.

Chris Dennett
A: 

I, err, dislike this sort of thing. I worked with one Java product that did it and it was maddening.

Ultimately this kind of thing dates from FORTRAN-I in the 1950s when I,J,K,... up to I forget where were automatically integer and the rest of the alphabet was reals (floating-point).

EJP
I see I have at least 3 silent fans.Any *reasons* for your choice?
EJP
I think this is the "hate" word and the fact it is off topic and subjective. (PS : i did not downvote you)
chburd
Blimey. 4 downvotes for one word and zero attention to the other 46 which are historical fact. And I dispute that it's off-topic.
EJP
Some people.., yes.
BalusC
+2  A: 

Using an "I" prefix on interfaces is something COM founded (.NET just inherited this convention) and is not a standard in Java. Look at any of the JDK or other code developed by Sun and you won't see an I prefix. And it's not only Sun, most Java projects don't use the I prefix. Far from being a Java standard, the I prefix is an aberration adopted in some corners of the Java world though.

Pascal Thivent
LOL at the downvote. Care to explain why?
Pascal Thivent
+1  A: 

I wanted to mention that in the other way, a real implementation of an interface, you may find the postfix Impl or a whole package of name impl (example).

But this is'nt a standard anyway.

PeterMmm
@jax: +1... In a world where you translate from OOA/OOD to OOP, then all that matters are abstractions. I happen to be very lucky and work somewhere where *every* abstraction is defined by an interface (it stems in part -but not only- from the fact that being OOA/D translationists, we're using multiple inheritance all the time, and under Java that means interfaces+delegation). We don't put 'I' in front of interface names, however we put 'Impl' after *every* class implementation, to be sure to always remember that these are implementation details and should never be "programmed to".
Webinator
A: 

I wouldn't recommend using it. You never know if your interface become one day abstract class. Then you'd have to rename every single usage or just stick with ugly named abstract class with prefix.

(Source: Robert C. Martin - Agile Software Development: Principles, Patterns and Practices)

Ondrej Slinták
it cant become abstract class, because it would break the code. you cant do "implements AbstractClass". Since you cant rename the class you cant change "implements" to "extend".
01
Right, my fault. Got bit confused with C# where you use ":" for both extends and implements.
Ondrej Slinták
@01 The other way around is equally important. If he has an abstract class which he decides to make an interface out of, he will either have to step away from the chosen convention and have one interface without the I notation (well, one of his own and all the standard ones) and the rest with the I notation (or the code will break).
Fredrik
A: 

This sort of coding style is called Hungarian Notation, because it was invented by Charles Simonyi at Microsoft, who happens to be Hungarian. The purpose of Hungarian Notation is to encode semantic information that cannot be expressed inside the type system into identifier names.

However, Java's type system is perfectly capable of distinguishing between interfaces (just try to extend one with a class), abstract classes (just try to instantiate one) and classes (just try to implement one), and so are most IDEs. So, using Hungarian Notation in this way is completely useless.

It has never been a convention as far as I know, and it certainly isn't now. At least in the Java community. (There are some Java projects that use it, though. It's also sometimes used in C++, but there it makes sense, because there is no such thing as an interface in C++, so you have to have a way to mark them. It is also used on the CLI, where it makes absolutely no sense for the same reason as in Java.)

Jörg W Mittag
Do you have a reference where they make this part of HN? In my world HN is mostly used on variable names, not type names.
Fredrik