views:

519

answers:

8

The .NET standard of prefixing an interface name with an I seems to be becoming widespread and isn't just limited to .NET any more. I have come across a lot of Java code that uses this convention (so it wouldn't surprise me if Java used it before C# did). Also Flex uses it, and so on. The placing of an I at the start of the name smacks of Hungarian notation though and so I'm uncomfortable with using it.

So the question is, is there an alternative way of denoting that Something is an interface, rather than a class and is there any need to denote it like this anyway. Or is it a case its become a standard and so I should just accept it and stop trying to stir up "religious wars" by suggesting it be done differently?

A: 

The coding standard for Symbian has interfaces (pure abstract C++ classes) denoted with an M rather than an I.

Otherwise, the only other way I have seen of denoting interfaces is through context.

workmad3
A: 

For .NET, Microsoft's Framework Design Guidelines book absolutely recommends it, and yes, it is very much standard. I have never seen it done otherwise, and to create a new convention would only serve to confuse people.

I should add that I dislike Hungarian notation too, but this and the case of prefixing class variables with an underscore are good exceptions to me, because they make code so much more readable.

Neil Whitaker
class variables prefixed with _ annoy me in C++. _ is meant to denote system variables and libraries, not class variables.
workmad3
For C++, I agree with you. I like them specifically for C#.
Neil Whitaker
+1  A: 

Its all about style and readability. Prefixing Interfaces with "I" is merely a naming convention and style guideline that has caught on. The compilers themselves couldn't care less.

RandomNoob
That's really true about Hungarian notation in general. It is simply a naming convention for humans reading the code. The compilers don't care what the variables are named as long as they are uniequely named and use only valid symbols.
Scott Dorman
+9  A: 

I would just accept it, to be honest. I know what you mean about being a bit like Hungarian notation (or at least abuse of the same) but I think it gives sufficient value to be worth doing in this case.

With dependency injection being in vogue, often I find I end up with an interface and a single production implementation. It's handy to make them easily distinguishable just with the I prefix.

One little data point: I work with both Java and C# a fair amount, and I regularly find myself having to check which types in Java are actually interfaces, particularly around the collection framework. .NET just makes this simple. Maybe it doesn't bother other people, but it bothers me.

+1 for IFoo from me.

Jon Skeet
+11  A: 

From the Framework Design Guidelines book:

Interfaces representing roots of a hierarchy (e.g. IList) should also use nouns or noun phrases. Interfaces representing capabilities should use adjectives and adjective phrases (e.g. IComparable, IFormattable).

Also, from the annotations on interface naming:

KRZYSZTOF CWALINA: One of the few prefixes 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.

BRAD ABRAMS: 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.

JEFFREY RICHTER: 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. As I said earlier, I use prefixes for my private type fields because I find this very useful.

BRENT RECTOR Note: this is really another application of Hungarian notation (though one without the disadvantages of the notation's use in variable names).

It has very much become a widely adopted standard, and while it is a form of Hungarian, as Brent states, it doesn't suffer from the disadvantages of using Hungarian notation in variable names.

Scott Dorman
Wow, those are some smart guys. I'm a big fan of Brad Abrams particularly.
Robert S.
+5  A: 

As a .NET programmer (for the most part), I actually prefer the Java convention of dropping the I here, for a simple reason: Often, small redesigns require the change from an interface into an abstract base class or vice versa. If you have to change the name, this might require a lot of unnecessary refactoring.

On the other hand, usage for the client should be transparent so they shouldn't care for this type hint. Furthermore, the “able” suffix in `Thingable” should be enough of a hint. It works well enough in Java.

/EDIT: I'd like to point out that the above reasoning had prompted me to drop the I prefix for private projects. However, upon checking one of them against the FxCop rule set, I promptly reverted to the usage of I. Consistency wins here, even though a foolish consistency is the hobgoblin of little minds.

Konrad Rudolph
That sucks... when consistency starts to dictate programming practice, it is a slippery slope.
drozzy
@drozzy: Consistency *always* dictates programming practice. And the “slippery slope” argument is a logical fallacy. In fact, if you want to design a reusable API for a programming language you are catering to expectations. The API should be easy to use and this means that it should be consistent with what the user already knows. There *are* reasons to violate this rule but a stupid `I` in front of the class name is not one of them. I think Microsoft’s naming convention is stupid but the train has left the station.
Konrad Rudolph
Consistency within an API is more to my liking. When you want to be consistent with other API's then I agree, you have to conform. But if you are working on a project independently, and consistently drop the I-prefix, why not? In fact, why not teach NEW programmers to drop it...
drozzy
@drozzy: well, **every** .NET project uses the .NET framework API. And that uses `I` as interface prefix. Furthermore, no real project is completely independent from the rest, sooner or later the project may grow so large that it needs to interact with other existing APIs. And every code should be written future proof since you never know if this code will in the future be used elsewhere or will need to interact with foreign code. The .NET framework basically dictates all the basic conventions to use in other .NET projects.
Konrad Rudolph
I do wander, what will happen if microsoft guys decide not to use it in the .net 5.0.
drozzy
A: 

I've always thought this naming convention is a bit of a dinosaur. Nowadays IDEs are powerful enough to tell us that something is an interface. Adding that I makes the code harder to read so if you really want to have a naming convention that separates interfaces from classes I would append Impl to the name of the implementing class.

public class CustomerImpl implements Customer
willcodejavaforfood
But how do we know Customer is an interface and not, say, an abstract class?
Robert S.
I think the "Impl" suffix makes the code harder to read than an "I" prefix on an interface, especially for public facing APIs.
Scott Dorman
A: 

You asked for an alternative, so here is one I have encountered:

Use no prefix on the interface class, but use a c or C prefix on the corresponding concrete classes. Most of your code will generally reference the interface, so why pollute it with the prefix and not the generally much less used concrete type.

This approach does introduce one inconsistency in that some concrete types will be prefixed (the ones with matching interfaces) and others will not. This may be useful since it reminds developers that an interface exists and its use should be preferred over the concrete type.

To be honest, I use the prefix on the interface, but I think it is more because I have become so accustomed and comfortable with to it.

Mario