views:

300

answers:

5

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.

+9  A: 

I prefer to suffix with "Base".

Joel Coehoorn
My question - why base?
Thomas Owens
Because pretty much everyone was taught OOP using 'base' classes in the terminology. It will be understood by other other programmers.
Joel Coehoorn
But what if it's not a Base class? Like there's an interface, a class that implements the interface, and a subclass of the class. Do you still prefix with Base?
Thomas Owens
An abstract class can't be anything other than a base class of some other class can it? Base doesn't mean root.
Davy8
+3  A: 

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

-Oisin

x0n
That was implied in his question. He is asking about C#: "coming from java to c# ..."
x0n
+2  A: 

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()
  {
  }
}
eduncan911
lol, I guess this is one of those "duh" moments. I completely forgot about the Base suffix, even though I knew it at some point. Marking as accepted since you give a nice example.
Davy8
HoovesManufacturer? Would that be the dam and sire?
tvanfosson
@tvanfosson: lol I wrote it on Opera Mobile, no spell checker.
eduncan911
+2  A: 

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.

mP
I find it makes code organization easier to follow. In your example with Map, without either looking at the code or consulting the JavaDocs, I can't tell that Map somemap = new Map() isn't valid and that I should be looking for a class that implements Map
Davy8
I find it especially helpful when trying to understand an unfamiliar codebase, it's easier to get a feel for how things fit together.
Davy8
It should be obvious from the name eg "Map" that it's describing an interface and is not concrete. The "hash" in HashMap clearly shows strategy in implementing Map. By using a combination strategy when naming your interfaces have shorter names than concrete classes. This is perfectly ...
mP
Compatible with the best practice of using interface types in all your references, method signatures and so on. Anything with a combo name is a concrete class with the added benefit of knowing some/limited detail of how the interface was implemented.
mP
When naming we should be attempting to give things meaningful names rather encoding details by fiddling with chars etc. This seems very computerish almot the equivalent of bit manipulations.
mP
How does the name Map make it "obvious" that it's describing an interface? Sure if you know that in addition to the Map class there also exists a HashMap class, but that requires outside knowledge. Without knowing the context it is conceivable that it could represent a map as in mapquest.
Davy8
I'm not saying it's bad and unintuitive, but just expressing that in my experience with the switch from Java to C# the I-interface naming convention made it easier to familiarize myself with a new codebase without digging through every file.
Davy8
In the end before you can use a particular class you need past experience which Involved Reading it's dock. Spotting or finding a class one needs to use is only the start. Using a convention like prefixing interfaces with "I" seems to primarily motivated by laziness.
mP
I don't see how knowing whether a class is an interface or concrete helps all that much in understanding it's operations and features. Development work is a bit more involved than that.
mP
A: 

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.

James