Java Forum / General / September 2006
Interface naming conventions
Daniel Dyer - 07 Sep 2006 22:01 GMT Increasingly I am seeing people prefixing interface names with an 'I', in open source apps and in example code on the web. So instead of this:
public interface MyInterface { }
we have this:
public interface IMyInterface { }
Personally, I really dislike this naming convention. Now, some of the people who are doing this aren't stupid, so I was wondering if anybody has a good explanation for why this is a good idea? Is it symptomatic of an addiction to Hungarian notation, or this there some more sensible rationale?
Dan.
 Signature Daniel Dyer http://www.dandyer.co.uk
Stefan Ram - 07 Sep 2006 22:10 GMT >public interface IMyInterface I often declare interfaces with one method, and then it is the most natural convention for me to name the interface like the method, e.g.,
interface Accept<T>{ void accept( T t ); }
Everything else makes you guess more often (»Closeable« or »Closable«?)
Sun does not seem to prefer »-able« anymore:
»Interfaces
Interface names should be capitalized like class names.
interface RasterDelegate;
interface Storing;«
http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html
parkerc - 07 Sep 2006 22:11 GMT > Increasingly I am seeing people prefixing interface names with an 'I', in > open source apps and in example code on the web. So instead of this: [quoted text clipped - 20 lines] > Daniel Dyer > http://www.dandyer.co.uk I think that it comes from C#. Personally, I really don't see a problem with a little Hungarian Notation in regard to interfaces.
Daniel Dyer - 07 Sep 2006 22:42 GMT > I think that it comes from C#. Personally, I really don't see a > problem with a little Hungarian Notation in regard to interfaces. OK, but what advantages does it have over not prefixing the interface name with an 'I'? I'm assuming that there must be some perceived advantage otherwise it would be completely pointless.
Dan.
 Signature Daniel Dyer http://www.dandyer.co.uk
AndrewMcDonagh - 07 Sep 2006 23:32 GMT >> I think that it comes from C#. Personally, I really don't see a >> problem with a little Hungarian Notation in regard to interfaces. [quoted text clipped - 7 lines] > --Daniel Dyer > http://www.dandyer.co.uk Its a hang over from the days when Microsoft introduced COM in C++. and then languages like Delphi took it up also.
As there is no such thing as an interface in C++, it was deemed 'a good idea' at the time as it differentiated or at least highlighted that there was a complete set of Pure Abstract Classes in the API, rather than the traditional Abstract Base classes.
Aside from C++, where I can just about see how it might help some people see that they should not put implementation inside the (interface) classes, for language like Delphi that support Interfaces - its a complete waste of time.
Andrew
Arne Vajhøj - 08 Sep 2006 02:28 GMT >> OK, but what advantages does it have over not prefixing the interface >> name with an 'I'? I'm assuming that there must be some perceived >> advantage otherwise it would be completely pointless.
> Its a hang over from the days when Microsoft introduced COM in C++. and > then languages like Delphi took it up also. [quoted text clipped - 8 lines] > classes, for language like Delphi that support Interfaces - its a > complete waste of time. .NET uses it too.
I can actually see one usage for it in C#, because the syntax for extends and implements are the same in C# it can make a class easier to read.
Arne
Martin Gregorie - 08 Sep 2006 00:09 GMT > I think that it comes from C#. Personally, I really don't see a > problem with a little Hungarian Notation in regard to interfaces. IMO its one of the silliest ideas ever to infect programming.
The problem is that it completely destroys information hiding by forcing implementation details to be included as part of all variable and function names. Change the internal representation of a variable or the return value of a function or method and you have to change its name and then churn through all your source, changing all references to the name. Stupid. It can just about be made to work with ANSI C, but it is completely at odds with any language that supports overloading.
The best explanation for its existence is that M$ came up with the idea to paper over deficiencies in its early compilers' type checking. If you use "Hungarian notation' in its full rigor *you* are the type checker, not the compiler. This seems like a perverse role reversal to me.
For sensible ideas about naming, source layout, and programming in general you can't do much better than get a copy of "The Practice of Programming" by Brian Kernighan and Rob Pike. Its ideas are applicable to just about any programming language and it contains reasonably substantial examples in C, C++ and Java to prove it.
 Signature martin@ | Martin Gregorie gregorie. | Essex, UK org |
parkerc - 08 Sep 2006 03:28 GMT > > > > I think that it comes from C#. Personally, I really don't see a [quoted text clipped - 25 lines] > gregorie. | Essex, UK > org | Great book.
Hungarian notation makes for ugly code - anyone who did any Win32 C++ programming should remember this (any a good deal of people still doing WinForms C# coding). I still think that adding an "I" to an interface is not all that bad if it helps the person coding the app to produce code that he or she can more easily read.
http://web.umr.edu/~cpp/common/hungarian.html - this is evil.
typedefs can also get pretty nasty in C. Especially typedefs of structs that have typedefs of structs that have typedefs...
Dale King - 08 Sep 2006 02:10 GMT >> Increasingly I am seeing people prefixing interface names with an 'I', in >> open source apps and in example code on the web. So instead of this: [quoted text clipped - 18 lines] > I think that it comes from C#. Personally, I really don't see a > problem with a little Hungarian Notation in regard to interfaces. A point of clarification, this is *NOT* Hungarian notation. This is an example of type-based naming, which, Micro$oft incorrectly labelled as Hungarian.
True Hungarian naming defines prefixes for names based on what the item represents. Type-based naming defines prefixes based on the data type in the programming language.
So for example if we had a variable that contained the height of a screen whose data type in C were unsigned long, then possible names in the two conventions would be:
Hungarian - hgtScreen Type naming - ulScreenHeight
The problem is that there is little value in type-based naming. Imagine if you also had an unsigned long screen color. If you tried to assign this to the above variable in type based-naming it might look like:
ulScreenHeight = ulScreenColor;
The statement makes no logical sense, but the type-based naming prefix didn't tell you anything. Compare thiw with Hungarian:
hgtScreen = clrScreen;
One of the main problems with type-based naming is that types can change. For example in Windoze there is wParam which ceased being a word ages ago, but they couldn't change the name without breaking users.
Hungarian is not nearly as usefull in an OO language, but it is not as bad as you were lead to believe from M$'s misuse of the term.
The I convention is relatively harmless, but offers little, if any, benefit. In addition to the origins in COM as others have mentioned, Eclipse also uses this convention. According to their convention guidelines, "This convention aids code readability by making interface names more readily recognizable." I don't find it necessary myself.
One reason it is used is to simplify naming of interfaces and concrete implementations of that interface so you can have IFoo interface and the concrete implementation could be called just Foo. I'll leave it up to you to decide if this is a good thing or not.
 Signature Dale King
Arne Vajhøj - 08 Sep 2006 02:32 GMT > A point of clarification, this is *NOT* Hungarian notation. This is an > example of type-based naming, which, Micro$oft incorrectly labelled as [quoted text clipped - 10 lines] > Hungarian - hgtScreen > Type naming - ulScreenHeight Wikipedia actually mentions both as being hungarian (system and apps respectively).
http://en.wikipedia.org/wiki/Hungarian_notation
I would say that the MS way of using the word is so common, that it is the meaning of the word today.
Arne
Dale King - 08 Sep 2006 05:14 GMT >> A point of clarification, this is *NOT* Hungarian notation. This is an >> example of type-based naming, which, Micro$oft incorrectly labelled as [quoted text clipped - 15 lines] > > http://en.wikipedia.org/wiki/Hungarian_notation I don't look to wikipedia as an authority on the subject. The important thing I was trying to point out (which the Wikipedia article concurs with) is that there is a difference between the Hungarian naming originally described by Simonyi and the later misuse of the term by Micro$oft.
> I would say that the MS way of using the word is so > common, that it is the meaning of the word today. I don't let M$ redefine terms for me. It is important to make the distinction. On the same note there is also what is called scope-based naming which is using prefixes to indicate local vs. member vs. global variables.
 Signature Dale King
mikeboggs - 08 Sep 2006 03:44 GMT The "I" prefix comes from COM most likely, where it is very popular to prefix Interfaces with the "I". I found this post through a google search: http://blogs.msdn.com/brada/archive/2004/02/03/67033.aspx
It's a pretty interesting read if you have the time to look through the comments.
Michael Boggs
Chris Brat - 08 Sep 2006 07:00 GMT Hi,
I'm not a fan of the "I" prefix and Hungarian notation simply because it disrupts intelisense in IDEs (Eclipse specifically, which lists options in alphabetical order).
This is going to add a whole extra keystroke!! ;-)
Chris
Chris Smith - 08 Sep 2006 09:08 GMT > Hi, > [quoted text clipped - 3 lines] > > This is going to add a whole extra keystroke!! ;-) I'd say the more important effect is that it prevents code from reading nicely. A piece of code is written once; read long after you have retired.
 Signature Chris Smith
Chris Brat - 08 Sep 2006 09:41 GMT Chris Uppal - 08 Sep 2006 08:28 GMT > Now, some of the > people who are doing this aren't stupid, so I was wondering if anybody has > a good explanation for why this is a good idea? I don't believe there is one; I don't believe there can be one. And just because someone isn't stupid doesn't mean they can't have stupid habits.
The only, and partial, exception I'd make is for example code where the names don't mean anything in themselves -- "Foo" and the like. There the names need as much help as they can get if they are not to obscure the example. But even in that case, I'd use something longer like "FooInterface".
There are btw quite a lot of programming practises which are accepted as normal, but which don't make any sense when you think about them.
-- chris
Daniel Dyer - 08 Sep 2006 11:45 GMT > There are btw quite a lot of programming practises which are accepted as > normal, but which don't make any sense when you think about them. Agreed, there are too many examples of people doing things, like this, because somebody told them it was a good idea. The justification usually sounds something like "because it's considered good practice...". By who? And more importantly, why? If a developer can't explain why something is a good idea, perhaps they shouldn't be doing it in the first place? This is kind of the point I was trying to get to in the "Design Question" thread the other day.
Dan.
 Signature Daniel Dyer http://www.dandyer.co.uk
olle.sundblad@gmail.com - 08 Sep 2006 15:33 GMT I am quite sure that the Java Code Style Guidelines tells you not to put it there. Also Java syntax differ between classes and interfaces and all decent IDEs makes it even easier.
So no do not put an I prefix (unless you have a stupid CTO who forces you, even then it might be a good idea to begin looking for a better place to work).
Just my 2 cents
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|