35

What is the rationale behind this naming convention?

I don't see any benefit. The extra prefix just pollutes the API.

My thinking is inline with Konrad's response to this related question; the chosen answer of which is mostly what I am asking for here.

7
  • 1
    I see two votes to close---but I'm not convinced it's really an exactly duplicate of the request for alternatives. If somebody will convince me, I'll vote to close. (Please leave a comment.) Commented Jan 13, 2009 at 1:18
  • 2
    Not duplicated and actually a good question. Commented Jan 13, 2009 at 1:22
  • Not a duplicate, yet Jon Limjap seems to think otherwise. What can I do? Commented Jan 13, 2009 at 2:24
  • 1
    It wasn't just Jon Limjap. 3 people have to vote for it, he just happened to be the 3rd person to do so. Commented Jan 13, 2009 at 2:34
  • @Simucal - Thanks for the clarification. Is it possible to vote to unclose it? Would be helpful if a link the the supposed duplicate was made available by the voters. Commented Jan 13, 2009 at 2:38

18 Answers 18

57

Its the complete opposite, the naming convention clearly identifies an interface.

For example if you have:

public class Dog : IPet, IMammal { .... 

Just from reading it, I can safely assume that IPet and IMammal are probably interfaces.

The .NET CLR allows for single class inheritance. So, if I have a base class..I can only inherit one class from it. Lets change the IPet interface to a base class..our example now becomes

public class Dog : Pet, IMammal { .... 

I am inheriting from the Pet class and implementing the IMammal interface.

If we did it what you are suggesting and removed the letter "I" we have this:

public class Dog : Pet, Mammal { .... 

Which one is the class I am inheriting from? Which is the interface I am implementing? It gets confusing right? (FYI..you are supposed to put the base class always first, so you could argue that point...but if you are arguing to remove the letter I from prefixing interface names I doubt you follow that practice as well)

As you can see that naming convention easily tells me a lot about my object without me having to investigate further. I can easily see what I am inheriting vs what I am implementing.

Sign up to request clarification or add additional context in comments.

12 Comments

It makes sense. So, in C# , Java's "extends and implements" are mixed together?
I follow the "base class always first" convention you mention. Nonetheless, I can't agree with your claim "naming convention easily tells me a lot about my object". Case in point, distinguishing between interface, abstract/concrete class, enum, struts, etc. isn't meaningful to me at the API level.
Why don't we prefix abstract/concrete types with "A"/"C", enumerations with "E", structures with "S", etc.?
It is rather ironic, that java actually got it right. No longer are the interfaces prefixed with an "I", but it is actually considered BAD practice. See "Effective Java" by Joshua Bloch.
What this doesn't address is the more fundamental question as to why do you need to identify a type as being an interface, any more than you need to identify whether it is a class or a struct... or whether it is Abstract or not..., or whether it contains instance members or only static ones... I think we do, but I'm not sure as to what the best answer is as to why ....
|
31

I also like it cause I can read it as "I verb-behavior" as in "ICanSave" or "IDoDoubleEntry" etc...

8 Comments

Perhaps the prefix should be 'Me'.
yeah it could be argued that the "Am" is implied.. "IAmEnumerable" would arguably be clearer, albiet longer.
I always find my interface names sound a bit like lolcats. "ICanHasCheeseburger"
Doesn't this actually go against C#'s naming conventions? ICanSave should be ISaveble.
@CharlesBretana I was just thinking because .NET names their interfaces as IClonable, IDisposable, and IComparable and not ICanClone, ICanDispose or, ICanCompare.
|
26

I think that the IInterface naming convention is silly. It's an example of Hungarian notation, and I subscribe to the school of thought that despises Hungarian notation. If you have an interface with only one implementation that has the same name, consider the possibility that this is a code smell.

However, I still use it, because in this case IInterface is recommended by Microsoft, and "standard is better than better".

1 Comment

I was never a fan of hungarian either, but I came across this old Joel article recently, which explains its origin and how it has been widely misunderstood - at least I can see the reason behind it now! joelonsoftware.com/articles/Wrong.html
17

Why isn't this a function of syntactical highlighting instead of Hungarian notation? Why doesn't the IDE just italicize identifiers that refer to interfaces if it's so important to distinguish between classes and interfaces. I hate putting "" or "m" before fields, "C" before classes, etc. Even worse, it encourages programmers write really bad APIs such as:

public class List : IList 

instead of a more reasonable:

public class LinkedList : List public class ArrayList : List public class HashList : List 

Even the .NET common class authors fell into this trap. A class name should NEVER be the name of the interface with just the "I" removed. The class name should always tell the user how the class differs from other possible implementations of the interface(s). I vote for dropping the stupid "I" for that reason alone.

Also, when I use intellisense, I want to group things by functional area, not whether it's a class or interface. I never think, "I need an interface, not a class." I always think, "I need something that does X".

2 Comments

I'm glad I'm not the only one who sees it this way. It sure seems like I'm in the minority. 4 years later I'm still amazed at how many readers swear "its makes it easy to identify an interface" and completely miss the point.
This is a really good point when designing an interface with extensibility in mind, but another use case of abstracting interfaces (C# and Java) is purely for the benefit of decoupling to allow mocking of dependencies, where no other concrete implementation of the interface has been explicitly 'designed for'. So MyReallySpecificBusinessComponent and IMyReallySpecificBusinessComponent would be Ok. But definitely not List.
11

Actually I find it useful to avoid naming clashes, I might for example create a concrete class called Fred that implements IFred

1 Comment

Naming clashes are avoided by using meaningful/useful names. E.g. Fred extends Person, implements Living, Breathing and other capabilities. Without more context I can't say for sure, but if Fred is a domain/model object, having an interface for it is probably superfluous.
5

I always thought it was fun to use verbs for behavioral interfaces. This is a departure from the class naming convention of using nouns, but it allows the class to "speak" to its behavior.

class Dog: IBark 

This does not work well for structural interfaces like WCF interfaces, but we don't need to have fun all the time.

to answer your question, think of the I as "implements" So...

class DogDataService : Dog, IDataService 

this service class inherits from Dog and implements IDataService

I'm still not really answering your question, but the I is useful because you get naming collisions between namespace, class and interface.

namespace DataService interface DataService class DataService: DataService 

so we end up with

namespace DataServices interface IDataService class DataService : IDataService 

I think in reality, it's a sanity convention.

3 Comments

+1 - generally speaking I think naming conventions indicate a weakness in the development language (and that is probably the case here), but it is nonetheless important to be able to avoid those collisions when you want to use dependency injection and to mock collaborator objects. There's really no alternative to a naming convention when you're creating interfaces for domain objects.
/shrug I use it for WCF services too. I have IServeData and ICanMakePayments for my two services.
@JeffSternal I think you might be applying the "every object must have an interface" anti-pattern. You probably should not create interfaces for domain objects; they are often an expected part of your public API.
4

If you consider the two "best-practice-aphorisms"

clarity is king

and

noise is bad

there is a conflict between these. The question is: when does clarity become noise?

For me it more noisy (but equally clear) to write Person person = new PersonImpl() than IPerson person = new Person().

1 Comment

Or better yet, Person p = new TallPerson(); but in my case, Person is not likely to be an interface. Instead it is a base type which TallPerson extends, that or an abstract type. In either case the "interface" of Person is implied by its public API. I despise FooImpl() as much as IFoo.
4

The need to differentiate between an interface and a class actually indicates a design flaw. In a well designed application, it will always be clear. A subclass should always be a specialization and classes can only be specialized in one subject, never more.

A class should have a single reason for existence. It should never be required to put secondary roles in a base class. E.g.:

public class XmlConfigurationFile : ConfigurationFile, IDisposable { } public class YamlConfigurationFile : ConfigurationFile, IDisposable { } 

The first one is a configuration file that is specialized in Xml, the second one is specialized in Yaml. These are also disposable, but that doesn't matter as much. You didn't create these two classes because of a different disposing processes.

Constrast this with:

public class XmlConfigurationFile : IDisposable, ConfigurationFile { } 

This will tell you that the main purpose a XmlConfigurationFile has, is that it is disposable. That you can use it as a way to represent configuration files is nice, but is secondary.

The problem starts when you create classes that have multiple reasons for existence:

public class MyConfigurationFile : XmlConfigurationFile, YamlConfigurationFile { } 

Even if XmlConfigurationFile and YamlConfigurationFile would have been interfaces, it still indicates bad design. How can your configuration file be Xml and Yaml at the same time?

If you read through the examples given (here and elsewhere), people always struggle to find a good example of when the I-prefix matters. One of the answers here is:

public class Dog : Pet, Mammal { } 

This is how this class will look like in an application about pets. A dog's main purpose is being a specialized pet, that can do pet-related things, not that it is a mammal.

public class Dog : Mammal, Pet { } 

This is how the same class will look like in an application about animal classifications. It is nice to know a dog is a pet, but it's main purpose is being a specialized mammal, that can do mammal-related things.

I think your classes should tell you the correct story about the architecture and domain of your application. Requiring an interface to be prefixed with an 'I' is a technical requirement and doesn't help you to tell your application's story better.

Once you start writing small, dedicated, single-purpose classes, the need for knowing if it implements or extends will automatically vanish.

1 Comment

This needs to be the approved answer. Well written and informative. Thank you.
3

It's either that or add "Impl" to the implementation of the interface (argh). I don't have a problem with the "I", it is the simplest and most straightforward naming for an interface.

4 Comments

I disagree with the "Impl" suggestion - it would be wasted noise just as the "I" prefix is. An implementation should be named with its implementation in mind. For example, an interface "Database" might be implemented with "RelationalDatabase" and "InMemoryDatabase".
It's a convention, you are not required to follow it if you don't like it.
Not a matter of me liking it. I'm questioning its value. I like to adhere to conventions not solely for the sake of following the pack. I want to understand the rational. Hopefully, someone can articulate that rational beyond "it lets you know its an interface" (which the IDE does w/ pretty icons).
Not every IDE does this, and there was a time [when this 'pattern' was devised in the first place] when IDEs didn't at all, you had to go find the information on 'X' in order to know what it was, or name things in a way that tells users EXACTLY what it is just by reading it's name which is probably the easiest, especially if you are like me and tend to skim code to try to understand it as opposed to hovering over every little thing to find that 'var p = SomeMethod()' where SomeMethod returns an int, I have to then check SomeMethod to see what it is. [note I HATE 'var/auto...']
3

The "I" convention seems to be an old convention that wouldn't be relevant today. Current code editor provides lots of insight about the type you are using, so arguing that It's easier to identify the interface is like asking for a namespace to be prefixed by a "N" because you want to be sure that you will not confound it with a concrete class (prefix with a "C"?).

A convention doesn't mean that It's a good convention. Sometimes, It's just because people get to use it...

Take for example the C# documentation generator: It doesn't care about it... if your interface is not prefixed with a "I" you will still see your interface in the interface part of your documentation. Do you really think that having a prefix "I" for all your interfaces inside the interface section of your documentation is a relevant information and help you to better identify interfaces?

1 Comment

I can't speak for anyone else, but I when I am planning types meant to interact with / extend a library (specifically in languages like C# where a class can only inherit from 1 class, and a struct can only implement interfaces) I don't want to have to go look up every type read all of it's documentation to MAYBE determine if it's an interface or a class... if an typename starts with I I can ALWAYS implement it if relevant, but and if it's not then I need to figure out something else if I already know I need to inherit from 'X'.
2

It makes it easily identifiable as an interface.

2 Comments

I don't see any direct value in distinguishing between interfaces and implementations. Further elaboration might help.
To me, at lease, Implementation means a class, which should be defined according to logical entity/ object semantics, whereas Interface means behavior, or contract, which should be organized according to logical groupings of connected behaviors.
1

Naming conventions offer the benefit of telling you something about the object before you use it. Naming conventions have been widely used for many years, going all the way back to fortran's insistence that integer values were restricted (if I remember correctly) to variable names like "i" and "j".

Hungariation notation took naming conventions to a whole new ugly level tha described the variable type, whether or not it was a pointer, etc. Many of us who were exposed to lots of code with Hungarian notation developed nervous twitches and verbal stutters.

Prefixing interface names with I is a relatively low-impact, harmless way of identifying that object.

1 Comment

How does identifying a type is an interface benefit the API? (I do understand the difference between interfaces and classes).
1

As others have pointed out, in purist OO terms, interfaces should always convey the abstraction, never the implementation, and this discipline is commonly observed in languages like Java, where the I prefixing of Interfaces is not generally observed, thus forcing more careful thought in the differentiation in the naming of the interface, and classes implementing it, e.g. class Car implements Vehicle etc, and encouraging adherence to the SOLID Interface Segregation Principle, e.g. class List : Collection, Iterable, Resizable.

However, in C#, where the I prefix convention is common, we often find uncreative implementation names (Dictionary : IDictionary, List : IList, etc), a practice which still occurs in modern code.

Despite the I convention, there is still the underlying benefit of an interface abstraction, including the ability to substitute dependencies abstracted through the interface (as per SOLID implementations), which is useful for unit testing.

When encountering code where exactly one implementation (Foo) exists of an interface (IFoo), i.e. with a matching name (sans the I prefix), to me, this conveys the meaning that:

  • Coupling on a dependency to a Foo should always be through the corresponding IFoo interface (and likely to be injected via an IoC container)
  • The initial design of IFoo is a proprietary, non-reusable interface specifically to allow classes dependent on Foo to mock out this dependency during unit testing.
  • Beyond the above, a reader doesn't need to infer any additional intelligence in the design of the IFoo interface (i.e. the class is either very limited in scope, or the SOLID ISP has not been applied to the class).
  • Conversely, if multiple concrete implementation classes of IFoo are required at a later point, that proper interface segregation design may need to be retrofitted into the hierarchy.

Comments

0

It is just a naming convention so everybody would know if it is an interface or something else it is not mandatory nor by the compiler nor by the IDE but All the interfaces i saw in my entire life starts with the letter I

Comments

0

I seems to traditional convention from Hungarian Notation. Interface Naming Guidelines says "Prefix interface names with the letter I, to indicate that the type is an interface." Framework Design Guidelines also says "DO prefix interface names with the letter I, to indicate that the type is an interface."

It is just a coding convention, So it's to hard to determine good or bad. Important things is consistency.

Comments

-1

Most likely its to make it easily identifiable in intellisense, since all the interfaces will clump together. Similar to how I prefix all my UI controls with btn, tb, lb. When intellisense kicks in everything is clumped together in one easy group.

1 Comment

I believe the IDE can "clump" without use of naming conventions.
-1

Firstly I believe prefixing with I then description is wrong because it means implementations can have a shorter name. IList (intf) -> List. This is an anti-pattern as we all know we should be using intf and probably only concrete types when creating. Don't flame me this is a generalization but the premise is intf only impl rarely. The implementation name should describe how it's implementing the intf or what it's doing. Think intf List, LinkedList which implements List using a linked list. Who cares if it's longer as we should be using List most of the time. If we have a class implementing many intf we probably should not include all the intf as the shadows the real purpose of the class. IN the case something removed without the intf makes sense. Eg ppl call me by name not Person, Sibling, developer etc using my name is the best most descriptive name. I suppose if a class is impl a simple intf then call it Default Intf which makes it on ious this is the default implementation of Intf. Names of classes sHould in the end be human readable and almost a short phrase describing their purpose. Prefix codes etc are not great as we communicate with words not codes. Computers do t cAre what classes are called so why remains is that we name things so the names help us and our colleagues.

Comments

-1

With all of the arguments about naming conventions and giving proper names to variables and methods that actually describe what they do...why not just name your interfaces (e.g. PetInterface, PlayerInterface, etc.) and do away with the prefix "I" all together. So what you have to type an additional 9 letters, at least the "I" is removed and we know it is not a class, because it says "Interface".

2 Comments

This is a terrible idea. Why not name your classes "DogClass"? If someone did this in my code-base, I would be very upset. There is a standard; use it.
@BradleyDotNET I could be very wrong, but this post felt very strongly like sarcasm, I think the point was that most people want to know an interface just by reading the name, and that there are worse ways to distinguish them than an 'I' prefix.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.