Interface Naming Convention

This, of course, is a subjective thing, but I do not see anything positive in the prefixes of interface names with "I". For me, Thing almost always readable than IThing .

My question is why does this agreement exist? Undoubtedly, this simplifies the transfer of interfaces from other types. But will this argument not extend to the preservation of the Hungarian notation, which is now widely condemned?

What is your argument for this inconvenient self? Or, more importantly, what could be Microsoft?

+44
c # interface naming-conventions
Mar 25 '09 at 13:58
source share
21 answers

Conventions (and critics regarding them) have every reason to solve them, therefore we will allow some reasons underlying the agreements

  • Interfaces have a prefix like I to distinguish between interface types from implementations - for example, as mentioned above, there should be an easy way to distinguish between Thing and its IThing interface, therefore the Convention serves to achieve this.

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

    public class Apple: Fruit

    Without an agreement, no one would know that Apple inheriting from another class named Fruit , or if it was an implementation of an interface called Fruit , while IFruit would make it obvious:

    public class Apple: IFruit

    The principle of least surprise applies.

  • Not all uses of Hungarian notation are condemned . The early use of Hungarian notation meant a prefix indicating the type of object, followed by the name of the variable or sometimes underscore before the name of the variable. This was useful for certain software environments (I think Visual Basic 4-6), but as the popular object-oriented programming became popular, it became impractical and unnecessary to specify a type. This became especially relevant when it came to intellisense.

    Today, the Hungarian notation is acceptable in order to distinguish user interface elements from actual data and similarly related user interface elements, for example txtObject for a text field, lblObject for the label associated with this text field, while the data for the text field is just Object .

    I should also point out that the initial use of the Hungarian notation was not intended to indicate data types (the so-called system Hungarian notation), but rather an indication of the semantic use of the variable name (the so-called Hungarian Notation applications). Read more about this in the wikipedia entry on Hungarian notation .

+64
Mar 25 '09 at 14:27
source share

Well, one obvious consideration would be the (very common) pair of IFoo and Foo (when abstracting Foo ), but in general it is often important to know if something is an vs class interface. Yes, this is partially redundant, but IMO is different from things like sCustomerName - here the name itself ( customerName ) should be sufficient to understand the variable.

But with CustomerRepository , is that a class or an abstract interface?

Also: waiting; fact, right or wrong, is what people expect. It is almost enough.

+22
Mar 25 '09 at 14:03
source share

The reason I do this is simple: because it's an agreement. I would rather just follow it than all my codes look different, which makes reading and studying difficult.

+21
Mar 25 '09 at 14:01
source share

Thing is a more readable name than IThing . I am from the school of thought that we should program interfaces, not specific implementations. In general, interfaces should take precedence over implementations. I prefer to give a more readable name for the interface rather than for implementation (ie My interfaces are named without the "I" prefix).

+17
Aug 26 '09 at 12:24
source share

I think this is better than adding the “Impl” suffix to your specific class. This is one letter, and this agreement is well known. Of course, you can use whatever naming you want.

+9
Mar 25 '09 at 14:02
source share

In my opinion, this “I” is just visual noise. The IDE should show class and interface names differently. Fortunately, the standard Java library does not use this convention.

+9
Jul 07 '10 at 17:41
source share

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

+7
Mar 25 '09 at 14:07
source share

I am sure that your question has been the subject of many lengthy discussions in the Microsoft team that have worked on the .NET Framework and its standards.

I think that the most striking example comes from the source itself. Below I will transcribe excerpts from the Framework Development Guide , a book I highly recommend.

From Krzysztof Cwalina, CLR Program Manager:

The only prefix used is "I" for interfaces (as in ICollection ), but this is for historical reasons. In retrospect, I think it would be better to use regular type names. In most cases, developers do not care that something is an interface and not an abstract class, for example.

From Brad Abrams, CLR, and the .NET Framework Program Manager:

On the other hand, the “I” prefix on interfaces is a clear recognition of the impact of COM (and Java) on the .NET Framework. COM popularized, even institutionalized, the notation with which interfaces begin with "I." Despite the fact that we discussed the discrepancy with this historical model, we decided to continue this model, as many of our users are already familiar with COM.

From Jeffrey Richter, Consultant and Author:

Personally, I like the "I" prefix, and I would like for us to have more of these things. Small one-character prefixes are very important in order to keep the code concise and yet descriptive. [...] I use prefixes for fields of my personal type, because I find this very useful.

My point is that it was under discussion. The advantage that I see is that it helps to avoid name matches between classes and interfaces, so your names can be either descriptive or compact

Personally - and perhaps out of habit - I like the prefix I , because it is reddish with the flags of the interfaces, allowing me to have an individual correspondence of names with implementation types. This shines when you want to implement a base implementation: IThing is an interface, Thing is a base (possibly abstract ) type. Derived types may be SomeThing . I like to use such crystal clear cuts.

+7
May 14 '13 at 18:05
source share

Because you usually have IThing and Thing. Therefore, instead of allowing people to come with their “conventions” for this recurring situation, a single uniform option was chosen for all conventions. Repeating what others say, actual standardization is reason enough to use it.

+3
Mar 25 '09 at 14:07
source share

Enter the prefix interface names with the letter I to indicate what type the interface is.

The manual does not explain why you should use the prefix I , but the fact that this is an already established protocol should be sufficiently substantiated.

What do you need to get by dropping the prefix I ?

+2
Mar 25 '09 at 14:08
source share

This is just an agreement whose goal is to prevent name conflicts. C # does not allow me to have a class and interface named Client, even if the file names are Client and IClient, respectively. I find it convenient to use the convention; if I had to offer another agreement, I would suggest using the “Contract” as a suffix, for example. ClientContract.

+2
Mar 25 '09 at 14:13
source share

I don’t know exactly why they chose this convention, perhaps partially thinking about defeating the class with the help of “I”, as in “I will list”.

The naming convention, which will be larger on the line of the rest of the framework, will include the type in the name, such as the xxxAttribute and xxxException classes, which makes it xxxInterface. It is a little longer, though, and all interfaces are something separate, and not just another group of classes.

+2
Mar 25 '09 at 14:27
source share

Separate interfaces from classes.

Also (this is more of a personal observation than dictated by high), the interfaces describe what the class does. "I" succumbed to this (I'm sure this is a construction in grammar that would be great to pop out right now); the interface that describes the classes that test will be Ivali. One of them describing compliance of behavior, is "IMatch".

+1
Mar 25 '09 at 14:03
source share

He looks Hungarian to me. Hungarian is generally considered a threat in strongly typed languages.

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

+1
Mar 25 '09 at 14:25
source share

I know that Microsoft recommends using an “I” to describe it as an interface. But this comes from IBM's naming conventions, if I'm not mistaken, initializing an “I” for interfaces and the subsequent * Impl for implementation.

However, in my opinion, Java naming conventions are a better choice than the IBM naming convention (and not just in Java, for C # and any OO programming language). Interfaces describe what an object can do if it implements an interface, and the description should be in the form of a verb. Ie Runnable, Serializable, Invoiceable, etc. IMHO is the perfect description of what the interface represents.

+1
Mar 25 '09 at 15:16
source share

For the graphical interface of the OS, it is very popular to use different icons for files and folders. If they all have the same icon, but folders with the prefix "F", it would be acceptable to use the same icon. But, since the speed of recognition of images of people is faster than the speed of recognition of words, we have set icons.

Computer programs such as the IDE are fully capable of making file interface visibility obvious. This frees up the namespace of different levels of abstraction occurring with the same name. For example. in "iCare", "I" describes the implementation, and "Care" describes the capabilities of the interface.

I suppose the self-agreement is so popular because we could not come up with anything better, but its existence is important because it indicates the need that we have in order to know this information.

+1
Dec 16 '10 at 23:59
source share

An interface naming should have a much deeper meaning than just specifying or not putting an “I” at the beginning of a name.

Neither "Fruit" nor "IFruit" would matter to me as an interface. This is because it is much more like a class. Classes define things, while interfaces must define functionality.

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

I really like to call my interfaces based on what functions they provide and what type of interface they have (for example, animated or inanimate interfaces).

If you focus on the functionality provided by the interface, you can quickly determine the name for the interface. It will also help you quickly find out if your interface defines unrelated functions.

Interfaces that define inanimate objects (i.e. things that cannot act on their own) ... I like to call them ... capable at the end of IPrintable (like a document, invoice) IPlayable (like Instrument, MediaPlayer) ISavable (e.g. document, image) IEdible (e.g. fruit, beef) IDrivable (e.g. car) IFlyable (e.g. Plane)

Interfaces that define animated objects (i.e. things that act on their own) ... I like to call them ... er at the end of ISwimer (e.g. fish, dog, duck) IDiver (e.g. Fish, Duck) IFlyer (e.g. Pilot) IDriver (e.g. NascarDriver)

In the end, the naming convention "I" helps distinguish between interfaces and classes. But it makes sense to add additional naming logic, except as soon as an “I” at the beginning.

+1
Aug 21 '14 at 2:09
source share

The fact is that everyone understands this, and part of writing the best code makes reading and understanding easier.

0
Mar 25 '09 at 14:04
source share

I do not like this agreement. I understand that this helps if you have an interface and an implementation that have the same name, but I just find it ugly. Anyway, I will follow him if it is a convention, where I work, of course. Coherence is the point of agreement, and coherence is a very good thing.

I like to have an interface that describes that the interface makes it as general as possible, for example, Validator . The concrete implementation that checks for a particular thing will be ThingValidator , and the implementation with some abstract functionality shared by Validator will be AbstractValidator . I would do this even if Thing is the only ... well ... thing I check and Validator will be shared.

In cases where only one specific class makes sense for an interface, I still try to describe something specific in this particular implementation, rather than name the interface in different ways to prevent name collisions. In the end, I'm going to enter the interface name more often than the implementation name.

0
Mar 25 '09 at 14:55
source share

You can add the word “interface” as a suffix to your custom interface, for example, “SerializerInterface”. For the Fruit abstract class, for example FruitAbstract, or you can make it AbstractFruit, just be consistent throughout. This is fairly readable or consistent with normal naming conventions.

0
Dec 27 '10 at 22:31
source share

Only my 2 cents:

The reason I personally add the suffix “Interface” is because it is easier to read than the “I” prefix, and because the files on the system are listed “grouped”. For example:

Not very 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 this is just a personal taste. I think that as long as you use a consistent naming convention in the whole project, it says nothing against using your own naming convention.

0
Sep 14 '15 at 18:05
source share



All Articles