Interface naming convention [closed]
Asked Answered
N

21

67

This is a subjective thing of course, but I don't see anything positive in prefixing interface names with an 'I'. To me, Thing is practically always more readable than IThing.

My question is, why does this convention exist then? Sure, it makes it easier to tell interfaces from other types. But wouldn't that argument extend to retaining the Hungarian notation, which is now widely censured?

What's your argument for that awkward 'I'? Or, more importantly, what could be Microsoft's?

Novanovaculite answered 25/3, 2009 at 13:58 Comment(4)
What about using grammar instead? Fruit is an Object in English so it should be a Class in programming. Iterable and Comparable are obviously interfaces. In the fruit example you could have Sliceable, JuiceExtraction etcPickaback
What's the "Hungarian notation" you mention?Tonetic
@Tonetic en.wikipedia.org/wiki/Hungarian_notationNovanovaculite
@FrederickTheFool Excelent reference. I didn't think about going to Wikipedia. The quotes at the bottom of the entry are remarkable.Tonetic
S
84

Conventions (and criticism against them) all have a reason behind them, so let's run down some reasons behind conventions

  • Interfaces are prefixed as I to differentiate interface types from implementations - e.g., as mentioned above there needs to be an easy way to distinguish between Thing and its interface IThing so the convention serves to this end.

  • Interfaces are prefixed I to differentiate it from abstract classes - There is ambiguity when you see the following code:

    public class Apple: Fruit

    Without the convention one wouldn't know if Apple was inheriting from another class named Fruit, or if it were an implementation of an interface named Fruit, whereas IFruit will make this obvious:

    public class Apple: IFruit

    Principle of least surprise applies.

  • Not all uses of hungarian notation are censured - Early uses of Hungarian notation signified a prefix which indicated the type of the object and then followed by the variable name or sometimes an underscore before the variable name. This was, for certain programming environments (think Visual Basic 4 - 6) useful but as true object-oriented programming grew in popularity it became impractical and redundant to specify the type. This became especially issue when it came to intellisense.

    Today hungarian notation is acceptable to distinguish UI elements from actual data and similarly associated UI elements, e.g., txtObject for a textbox, lblObject for the label that is associated with that textbox, while the data for the textbox is simply Object.

    I also have to point out that the original use of Hungarian notation wasn't for specifying data types (called System Hungarian Notation) but rather, specifying the semantic use of a variable name (called Apps Hungarian Notation). Read more on it on the wikipedia entry on Hungarian Notation.

Swear answered 25/3, 2009 at 14:27 Comment(4)
Also, interfaces probably feel bad because they don't really do anything. Adding the I makes up for that by making them feel a lot cooler. Same thing is done with IPod, IMac, IPhone, etc. The I makes them look cooler.Concoff
The I is also a useful memonic for the HAS-A property that most interfaces represent - IHasData, IHasCookie, IHasCheezburger...Siclari
The only real reason I see why c# has this convention is point two.Stiles
@Concoff There is no such thing as IPod, IMac or IPhone, they start with the lower case I; i instead.Paletot
T
30

The reason I do it is simple: because that's the convention. I'd rather just follow it than have all my code look different, making it harder to read and learn.

Tynishatynwald answered 25/3, 2009 at 14:1 Comment(10)
How about following the I-less convention in all your code? (I think I'm gonna do precisely that in my next project.)Novanovaculite
I'd be sorely pissed off if I was looking for interfaces in your project and spent a good ten minutes longer than needed.Blearyeyed
You could do that, but I'd like all my code to look like everyone else's code (including MS). Everyone already knows that "I" means interface, so it makes it easier for others to understand my code.Tynishatynwald
Add to that parity with the BCL...Raab
Conventions aren't generally enforcible by compilers; they're usually enforced by your fellow developers cursing you when you don't follow them.Blearyeyed
By the way, my question is more like, why did this convention come into being in the first place, not why people should follow it now. Eventual critical mass of followers can hardly justify initial institution of the idea.Novanovaculite
@Frederick - We know why it's useful and why we should follow it, but unless we can find the person who invented it, I think we can only guess at why it came about.Tynishatynwald
"Eventual critical mass of followers can hardly justify initial institution of the idea" - true, but on this occasion I suspect it genuinely is useful, not just a legacy.Raab
That's circular logic Jon. "Because that's the way it's done" might be good enough reason for a lot of folks, but some of us like to believe in what we do.Gauge
@ted - my logic is that you should follow this convention because it will improve code readability and maintainability. I don't think that's circular.Tynishatynwald
R
23

Well, one obvious consideration would be the (very common) IFoo and Foo pair (when abstracting Foo), but more generally it is often fundamental to know whether something is an interface vs class. Yes it is partly redundant, but IMO is is different from things like sCustomerName - here, the name itself (customerName) should be enough to understand the variable.

But with CustomerRepository - it that a class, or the abstract interface?

Also: expectation; the fact is, right or wrong, that is what people expect. That is almost reason enough.

Raab answered 25/3, 2009 at 14:3 Comment(15)
Frankly I'm sceptical about the "almost" :)Chrysoprase
Yeah. It is reason enough.Khalkha
"But with CustomerRepository - it that a class, or the abstract interface?" Well, in Visual Studio at least, just hover over it. Isn't this the case where coding tools can help us better then redundant conventions? Wasn't the Hungarian notation, too, killed by the Visual-Studio-hover?Novanovaculite
@Frederick - I read with my eyes, not the mouse... if I can't undestand a line by looking at it, then the line is automatically wrong.Raab
Well then Why do you care whether it's an interface or a class, especially if you are writing client code? Not caring: isn't that the very purpose of abstraction, and hence OOP?Novanovaculite
Knowing whether I am coding against the concrete or the abstract is an important for the abstraction to be useful.Raab
And you should care. At least, between interface and class.Raab
The hover will tell you. The 'Go to declaration' will gleefully assist. The compiler will eventually correct you. So there's no question of a something going wrong. The question really is do I need to know the difference in the very first glance?Novanovaculite
Between an interface type and class type? Then yes.Raab
What is specific about Foo that makes it different from any other possible implementation of IFoo? If Foo is the abstract type, name the concrete implementation(s) to reflect that they are a specific, concrete type.Gentlemanfarmer
The point, though, is that if you are trying to write an API that works with interfaces, any classes are a problem, since .NET doesn't allow multiple (class) inheritance. For interfaces this isn't an issue... (cont)Raab
... As such, there is a fundamental difference between SomeMethod(Foo foo) and SomeMethod(IFoo) if (as is often desirable) you are trying to write an API that works with the interface. This is easy enough to get wrong, without ambiguity over what is an interface / what is a class. Keep it obvious.Raab
I think SomeMethod(FlatFileFoo foo), SomeMethod(XMLFoo foo), etc.; vs. SomeMethod(Foo foo) makes it pretty obvious.Gentlemanfarmer
Okay, what if Visual Studio color coded interfaces differently from classes? Wouldn't that be enough to make the distinction evident? If so, isn't that what Microsoft should've offered in the first place instead of propagating communication-hindering conventions?Novanovaculite
I don't think we're ever going to agree here. I don't see a "communication-hindering convention"...Raab
L
23

Thing is more readable name than IThing. I'm from the school of thought that we should program to interfaces rather than specific implementations. So generally speaking, interfaces should have priority over implementations. I prefer to give the more readable name to the interface rather than the implementation (i.e., my interfaces are named without the 'I' prefix).

Lanam answered 26/8, 2009 at 12:24 Comment(1)
Let's put I as an implementation prefix! ;)Sou
R
22

I'm sure your question was the topic of many lengthy discussions within the Microsoft team that worked on the .NET Framework and its standards.

I think the most telling example comes from the source itself. Below, I transcribe extracts from Framework Design Guidelines, a book I highly recommend.

From Krzysztof Cwalina, CLR program manager:

The only prefix used is "I" for interfaces (as in ICollection), but that is for historical reasons. In retrospect, I think it would have been better to use regular type names. In a majority of the cases developers don't care that something is an interface and not an abstract class, for example.

From Brad Abrams, CLR and .NET Framework program manager:

On the other hand, the "I" prefix on interfaces is a clear recognition of the influence of COM (and Java) on the .NET Framework. COM popularized, even institutionalized, the notation that interfaces begin with "I." Although we discussed diverging from this historic pattern we decided to carry forward the pattern as so many of our users were already familiar with COM.

From Jeffrey Richter, consultant and author:

Personally, I like the "I" prefix and I wish we had more stuff like this. Little one-character prefixes go a long way toward keeping code terse and yet descriptive. [...] I use prefixes for my private type fields because I find this very useful.

My point is, it WAS on the discussion table. An advantage I see is that it helps avoid name collisions between classes and interfaces, so your names can be both descriptive and compact

Personally--and perhaps out of habit--I like the I prefix, because it succinctly flags interfaces, allowing me to have one-to-one naming correspondence with implementing types. This shines in cases when you want to provide a base implementation: IThing is the interface, Thing is the base (perhaps abstract) type. Derived types can be SomeThing. I love being able to use such crystal clear shorthand notation.

Ruffo answered 14/5, 2013 at 18:5 Comment(0)
B
12

I think it is better than adding a "Impl" suffix on your concrete class. It is a single letter, and this convention is well established. Of course you are free to use any naming you wish.

Baluchistan answered 25/3, 2009 at 14:2 Comment(4)
An possible exception to this is where the interface class is exposed to the outside and the implementation is internal. For example in WCF. In that case I prefer exposing a service 'MyService' and having a 'MyServiceImpl' class than exposing something call 'IMyService' to the outsideMishnah
@Rob Walker - you can do that in the [ServiceContract(Name="MyService)]Raab
I think "Impl" is a Java convention, not C#, but some shops do carry this over into C#. Not a bad idea though, just as long as it's consistent.Swear
I like the Impl convention as in all places I am actually using my interface and the only place the *Impl is used in my ioc setup.Valerle
M
10

In my opinion this 'I' is just visual noise. IDE should show class and interface names differently. Fortunately Java standard library doesn't use this convention.

Melinamelinda answered 7/7, 2010 at 17:41 Comment(3)
Responses starting with "In my opinion" are not really good responses. There are official guidelines to follow in many cases, as is the case with .NET and Microsoft. msdn.microsoft.com/en-us/library/8bc1fexb(v=vs.71).aspxOrganography
Isn't the "Impl" postfix just a visual noise in Java? @Isaac is right concerning the official guidelines.Zareba
Fortunately not, the "Impl" postfix is much better than the "I" prefix... /sYahrzeit
J
8

There is nothing wrong with NOT using I convention for interfaces - just be consistent and make sure it works not just for you but for whole team (if there is one).

Jackpot answered 25/3, 2009 at 14:7 Comment(0)
K
5

Naming an interface should have much deeper meaning than just whether or not you put an "I" at the front of the name.

Neither "Fruit" nor "IFruit" would have a whole lot of meaning for me as an interface. This is because it looks a lot more like a class. Classes define things, whereas interfaces should define functionality.

The "I" naming convention does help differentiate between classes and interfaces so that development is a little bit easier. And while it is not required, it certainly helps avoid common object oriented coding headaches. C#, like Java, only allows for inheritance from a single base class. But you can implement as many interfaces as you want. The caveat is, if you inherit from a class and implement one or more interfaces, the base class has to be named first (i.e. class Trout: Fish, ISwimmer, IDiver ... ).

I really like to name my interfaces both based based on what functionality they provide as well as what type of interface they are (i.e. animate or inanimate interfaces).

If you focus on in functionality that the interface provides you can quickly determine a name for the interface. It also helps you to quickly see if your interface defines unrelated functions.

Interfaces that define inanimate objects (i.e. things that can't act on their own)... I like to name them with ...able at the end IPrintable (such as Document, Invoice) IPlayable (such as Instrument, MediaPlayer) ISavable (such as Document, Image) IEdible (such as Fruit, Beef) IDrivable (such as Car) IFlyable (such as Plane)

Interfaces that define animate objects (i.e. things that act on their own)... I like to name them with ...er at the end ISwimer (such as Fish, Dog, Duck) IDiver (such as Fish, Duck) IFlyer (such as Pilot) IDriver (such as NascarDriver)

In the end, the "I" naming convention helps differentiate between interfaces and classes. But it may make sense to add additional naming logic besides just the "I" at the beginning.

Krone answered 21/8, 2014 at 2:9 Comment(0)
Q
3

Because you usually have an IThing and a Thing. So instead of letting people come with their own "conventions" for this recurring situation, a uniform one-size-fits all convention was chosen. Echoing what others say, the de facto standardness is reason enough to use it.

Quizmaster answered 25/3, 2009 at 14:7 Comment(0)
I
3

It's just a convention that's intent is to prevent name collisions. C# does not allow me to have a class and an interface named Client, even if the file names are Client and IClient, respectively. I'm comfortable using the convention; if I had to offer a different convention I'd suggest using "Contract" as a suffix, e.g. ClientContract.

Infamous answered 25/3, 2009 at 14:13 Comment(0)
P
2

Do prefix interface names with the letter I to indicate that the type is an interface.

The guideline doesn't explain why you should use the I prefix, but the fact that this is now an established convention should be reason enough.

What do you have to gain by dropping the I prefix?

Pontianak answered 25/3, 2009 at 14:8 Comment(3)
"What do you have to gain by dropping the I prefix?" Readability.Novanovaculite
@Frederick, You lose readability by dropping the I. You can no longer see at-a-glance whether something is an interface or a class.Pontianak
The only reason you need to know whether something is an interface or not is if you're going to try to new something up. Of course, if you're adhering to the Dependency Inversion Principle and/or using Dependency Injection, then it shouldn't really matter whether or not something is an interface because you won't be newing much of anything. Besides that, the IDE is well able to tell you whether or not something is an interface.Robertroberta
G
2

It looks Hungarianish to me. Hungarian is generally considered a menace in strongly-typed languages.

Since C# is a Microsoft product and Hungarian notation was a Microsoft invention, I can see where C# might be susceptible to its influence.

Gauge answered 25/3, 2009 at 14:25 Comment(2)
Using hungarian notation for data type is pretty useless in a strongly typed language. However the original intention for hungarian notation wasn't even to be used to indicate data type, but to indicate other, more crucial properties of the data, like horisontal vs. vertical coordinates. :)Maintop
Since classes and interfaces are interchangeable in the syntax except for the new statement, I don't think the argument that the language is strongly typed holds up. You want to use the interface type almost everywhere except the new statement (or else your interface type is redundant) but it's easy to slip up and use the implementation class if it's not clear which is which.Newland
M
2

I don't know exactly why they chose that convention, perhaps partly thinking of ensouling the class with "I" as in "I am Enumerable".

A naming convention that would be more in the line of the rest of the framework would be to incorporate the type in the name, as for example the xxxAttribute and xxxException classes, making it xxxInterface. That's a bit lengthy though, and after all the interfaces is something separate, not just another bunch of classes.

Maintop answered 25/3, 2009 at 14:27 Comment(0)
F
2

I know the Microsoft guidelines recommends using the 'I' to describe it as an interface. But this comes from IBM naming conventions if I'm not remember wrong, the initiating 'I' for interfaces and the succeeding *Impl for the implementations.

However, in my opinion the Java Naming Conventions is a better choice than the IBM naming convention (and not only in Java, for C# as well and any OO programming language). Interfaces describes what an object can be able to do if it implements the interface and the description should be in verb form. I.e Runnable, Serializable, Invoiceable, etc. IMHO this is a perfect description of what the interface represents.

Fistulous answered 25/3, 2009 at 15:16 Comment(0)
H
1

It's popular for an OS GUI to use different icons for files and folders. If they all shared the same icons, but folders were prefixed with "F", it would be acceptable to use the same icon. But, since humans' image recognition speed is faster than their word recognition speed, we have settled on icons.

Computer programs like IDEs are fully capable of making a file's interface-ness apparent. This would free the namespace of different levels of abstraction happening in the same name. E.g. in "ICare", "I" describes the implementation and "Care" describes the interface's capabilities.

I'm guessing the "I" convention is so popular because we haven't been able to think of anything better, but it's existence is important because it points out a need we have for knowing this kind of information.

Hasty answered 25/3, 2009 at 13:58 Comment(0)
B
0

To separate interfaces from classes.

Also (this is more of a personal observation than dictated from upon high), interfaces describe what a class does. The 'I' lends itself to this (I'm sure it is a construct in grammar which would be great to whip out right now); an interface that describes classes that validate would be "IValidate". One that describes matching behavior would be "IMatch".

Blearyeyed answered 25/3, 2009 at 14:3 Comment(3)
Even though an interface describes what a class "does", it shouldn't be described as a verb. It's still a thing. It's IValidatable or IMatchable.Korrie
Well, suit yourself. But if you look at the .NET Framework, you'll know what IMean.Korrie
IUnderstand and ICouldDoThisAllDayBlearyeyed
Y
0

The fact of the matter is that everyone understands it and part of writing better code is making it easy to read and understand.

Yaupon answered 25/3, 2009 at 14:4 Comment(0)
G
0

I don't really like this convention. I understand that it helps out with the case when you have an interface and an implementation that would have the same name, but I just find it ugly. I'd still follow it if it were the convention where I am working, of course. Consistency is the point of conventions, and consistency is a very good thing.

I like to have an interface describe what the interface does in as generic a way as possible, for example, Validator. A specific implementation that validates a particular thing would be a ThingValidator, and an implementation with some abstract functionality shared by Validators would be an AbstractValidator. I would do this even if Thing is the only... well... thing that I'm validating, and Validator would be generic.

In cases where only one concrete class makes sense for an interface, I still try to describe something specific about that particular implementation rather than naming the interface differently to prevent a names collision. After all, I'm going to be typing the name of the interface more often than the name of the implementation.

Gentlemanfarmer answered 25/3, 2009 at 14:55 Comment(0)
W
0

You may add the word "Interface" as a suffix to your customized interface for example "SerializerInterface". For abstract class, "Fruit", for instance, "FruitAbstract" or you can make it "AbstractFruit", just be consistent all through out. That is readable enough, or follow the usual naming convention.

Weak answered 27/12, 2010 at 22:31 Comment(0)
N
0

Just my 2 cents:

The reason why I personally append the suffix "Interface" is because it is easier to read than the "I" prefix and because the files in the system are listed "grouped". For example:

Not so good:

Alien.php
Host.php
IAlien.php
IHost.php
IXenomorph.php
Xenomorph.php

Better:

Alien.php
AlienInterface.php
Host.php
HostInterface.php
Xenomorph.php
XenomorphInterface.php

But that's just personal taste. I think as long as one uses a consistent naming convention throughout the entire project, nothing speaks against using your own naming convention.

Nubbin answered 14/9, 2015 at 18:5 Comment(1)
True, but by that notion wouldn't you name classes/files AlienClass and AlienClass.php ;)Ioves

© 2022 - 2024 — McMap. All rights reserved.