Abstract classes naming convention vote

We’re going to make a decision on another aspect of the code style: abstract classes. Please vote for the variants you like and state arguments for it.

  • abstract class ActionAbstract
  • abstract class AbstractAction
  • abstract class Action

0 voters

Main argument for me for using Action would be that adding Abstract is redundant, it is more a characteristic of the class and not part of what it does/its name.

If we decide to add Abstract to the name I prefer the postfix, but that has mainly historical reasons, it is what I am used to from old frameworks like e.g. ZF1 :slight_smile:

Prefixing Abstract sounds weird to me, in that case I’d prefer something like BaseAction as it describes better why the class is abstract: it is the base class for concrete implementations.

6 Likes

If I would append/prepend I would prepend it, because otherwise Imagine Name :DoStuffControllerAbstract vs AbstractDoStuffController

ActionAbstract

Though I find it bit verbose, but I prefer it because it create mapping in my mind which file is class, interface, trait or abstract class.

I would vote for BaseAction.
Any non-abstract base class can become abstract at any time. So what, now you have to rename it everywhere?

3 Likes

Do you mean prefixing all abstract classes with Base or just using variant 1 and a good name?

I mean, Abstract is not needed as a prefix and postfix. If you need to mark a class as base for descendants, then it is better to make it with the Base prefix.

However, you do not need to make this prefix necessary.

In cases where an abstract class used as an interface (defines contracts and is used in method signatures), any prefix and postfix may look superfluous (Case 1).

Case 1 (class as contract)

class SomeWidgetHelper {
    # all *Widget* classes are abstract

    public static function wrap(AbstractWidget $widget);

    public static function wrap(WidgetAbstract $widget);

    public static function wrap(BaseWidget $widget);

    public static function wrap(Widget $widget); # <== i like it
}

Case 2 (class as functionality)

namespace Yiisoft\Widget;

interface WidgetInterface {}

abstract class AbstractWidget implements WidgetInterface {}
abstract class WidgetAbstract implements WidgetInterface {}
abstract class BaseWidget implements WidgetInterface {} # <== i like it
abstract class Widget implements WidgetInterface {}

final class Form extends BaseWidget {}
final class Button extends BaseWidget {}
2 Likes