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 respo
Actually I find it useful to avoid naming clashes, I might for example create a concrete class called Fred that implements IFred
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()
.
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".
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.
It makes it easily identifiable as an interface.
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.