I have to name all my abstract classes AbstractFoo

Is it good to make sure that all abstract classes have names with the prefix "Abstract"?

+6
class abstract
source share
9 answers

You can, but I try not to do this, as this is an implementation detail.

I do not like to add implementation details to type names and identifiers, as such information may change in the future. In my opinion, it is best to name things as they are, and not how they are implemented.

+8
source share

I think this naming convention is used only because it is hard to find another good name. If you already have an interface called List, how can you call the AbstractList class? This is more about avoiding name collisions, and then about implementation details.

+4
source share

It depends on your conventions.

You can also name them FooBase or just Foo if you don't already have a Foo interface.

+3
source share

If you are considering how to do this in the .NET Framework, no. Take, for example, the abstract Stream class. Nothing in the class name indicates that it is actually abstract.

+1
source share

It is difficult to explain, but I use it only to avoid copying / pasting the same code into functional classes, and not into something like domain objects.

  • AbstractServiceTestCase -> Abstract prefix seems useful
  • AbstractAnimal → Seems strange and worthless

You must, of course, decide for yourself, as long as the same agreement is respected throughout the project.

+1
source share

I would not call Abstract classes Abstract for the following reason:

Each Rectangle is a Form . Everywhere you can use a shape, you can use a rectangle. Code that deals with a rectangle (but which can also deal with circles) might look like this:

Shape s = .....; s.drawTo(myGraphicsContext); 

Using an object (such as a rectangle) anywhere you can use its generalization (such as Shape) is an important part of an object-oriented concept and is known as the Liskov Principle of Replacement . (Its also obvious: which sentence or logic will make statements about shapes, but then not applicable to rectangles?)

If you called a generalization AbstractShape , this principle is violated. The rectangle is not AbstractShape. If anything his "Concrete form"! The rectangle is not abstract (in the sense of "I don’t know what type of shape it is. Maybe a rectangle, maybe something else"). Code using AbstractShape then reads incorrectly:

 AbstractShape s = new Rectangle(...); 

I wrote more comments on this topic here .

+1
source share

I find it useful to name classes this way. It sends a message that they are intended for a subclass; not instantiated; contain code common to subclasses, etc.

This is not always necessary, though. Most programmers are likely to identify “form” as an abstract class and “square”, “circle”, etc. How specific. But if this is not immediately clear, this is a useful hint.

You should also be guided by local programming conventions and style guides.

0
source share

I think this partly depends on how you use the class. If it is intended only for internal use when coercing derived classes to an interface, then adding Abstract before this may be a bad idea. However, if you provide a Foo factory that will provide instances of Foo that are actually SpecializedFoo1 or SpecializedFoo2, then it seems inconvenient to return instances of AbstractFoo.

0
source share

The answers are still very helpful and demonstrate the responsible dissemination of practice. I tend to agree that names should not point to an implementation ( Foo may be an abstract class that was later ported to the interface). However, this is useful to me when coding has visual cues that I need to provide methods for derived classes.

As an example, I currently have a hierarchy (don't ask about the rationale for names, but they make sense in context and display the names of XML elements). I use Java, but I think most languages ​​will be similar:

 public abstract class Marker {...} public class Template extends Marker {...} public class Regex extends Marker {...} 

Now I aim for:

 public abstract class Marker {...} public class TemplateMarker extends Marker {...} public class RegexMarker extends Marker {...} 

but not

 public abstract class AbstractMarker {...} public class Template extends AbstractMarker {...} public class Regex extends AbstractMarker {...} 

or

 public abstract class AbstractMarker {...} public class TemplateMarker extends AbstractMarker {...} public class RegexMarker extends AbstractMarker {...} 

I personally remember that Marker is an abstract functional concept and that subclasses are concrete implementations.

0
source share

All Articles