David Ly
David Ly

Reputation: 31596

Prefixing abstract classes with "A" like interfaces are prefixed with "I"?

It seems to me that it'd be useful to be able to tell at a glance that a class is abstract and not meant to be directly instantiated just like it's useful to be able to easily identify an interface.

My question is, why didn't "AFourLeggedAnimal : IAnimal" catch on? Is it simply because of the possible confusion (which I just noticed while writing that) for example confusing it as "A four legged animal" instead of "abstract class FourLeggedAnimal"? Or is it something more?

Coming from Java in school to C# at work, I found the "I" prefix naming convention extremely useful when glancing through a list of classes and it seems to me that it'd be convenient to be able to distinguish between concrete and non-concrete classes at a glance without needing to look at the code.

Upvotes: 4

Views: 3683

Answers (5)

James
James

Reputation: 151

The I prefix is ugly, but unfortunately that's the Microsoft convention. In an ideal world, we should all be programming to interfaces. Concrete classes would have a suffix Impl or something to distinguish them from the interface or abstract class (and we don't care because our IoC containers will handle that for us!)

e.g. Book would be an interface or abstract class and it really doesn't matter which. We would be programming to nice clean names most of the time. The actual implementation to use is easily pluggable from a configuration file.

But alas, we don't live in such an ideal world.

Upvotes: 1

mP.
mP.

Reputation: 18266

In Java many "abstract" classes are prefixed with "Abstract" eg - AbstractList etc.

In the end why does it matter that one needs to know whether a class is abstract just from reading it's name. There's only so much detail one can cram into a class name before they become quite long.

I personally find the "I" prefix thing for interfaces quite ugly as well. I believe one should not try and encode such details in a class name. I believe by combining implementation details with the interface name one comes up with truely meaningful yet short names. A perfect example is Java's Map, HashMap etc all very discriptive and concise.

Upvotes: 2

eduncan911
eduncan911

Reputation: 17604

Use the suffix "Base" as Joel mentions. Once you have a lot in your project, it's pretty easy to tell apart:

public abstract AnimalBase
{
  public AnimalType AnimalType { get; set; }
}

public abstract HorseBase : AnimalBase
{
  public HoovesManufacturer HoovesManufacturer { get; set; }
}

public class Pony : HorseBase
{
  public Pony()
  {
  }
}

Upvotes: 15

x0n
x0n

Reputation: 52430

Because, quite frankly, the accepted pattern for naming abstract classes is already set ;-) It's with a "Base" suffix, like MyControlBase, or FooBase.

-Oisin

Upvotes: 4

Joel Coehoorn
Joel Coehoorn

Reputation: 415880

I prefer to suffix with "Base".

Upvotes: 13

Related Questions