Using “Base” in a Class Name

2019-02-05 15:14发布

Is it acceptable to use the word 'Base' in a class name which is a the bottom of the inheritance tree?

I have always found this a bit of a cop-out, just wondering if anyone agrees with me.

For example, if I am refactoring certain elements from MyClassA and MyClassB into a common base class, I'd be tempted to create a MyBaseClass from which the two inherit.

But what happens if I ever need to refactor MyBaseClass? MyBaseBaseClass? Now that's just silly.

I know that Rocky Lhotka doesn't mind with his CSLA framework, but I'm always uneasy about 'definites' in programming.

Thoughts?

Let me clarify why I'm even worrying about this.

I have two namespaces - MySpecificNamespace and MyCommonNamespace. MyNamespace uses MyCommonNamespace, as you might expect.

Now, I like to make maximum use of Namespaces wherever possible to describe the context of the problem, and avoid adding the context to the class name. So, for example, consider that I have a class in MyNamespace which descends from one in MyCommonNamespace.

Option A

I could call this

MySpecificClass: MyClass
{
}

But then I'm adding 'Specific' (the context) to the name - which is redundant as it's already in MySpecificNamespace.

Option B

MyClass: MyCommonNamespace.MyClass
{
}

You can see how we could get confused here, right?

Option C

The one I think is fishy:

MyClass: MyBaseClass
{
}

12条回答
虎瘦雄心在
2楼-- · 2019-02-05 15:53

I tend to add a Base suffix to the name of the base class only if it exists from technical perspective (to share some code), and doesn't really constitute any usable class on its own (so all of these classes are abstract). These are quite rare cases though, and should be avoided just as Helper classes.

查看更多
贼婆χ
3楼-- · 2019-02-05 15:58

I usually go with IFoo for the interface and AbstractFoo for the skeletal implementation, which is a mix of .NET and Java conventions.

查看更多
Lonely孤独者°
4楼-- · 2019-02-05 15:59

In Java I tend to provide a base implementation of an interface Foo in an abstract class FooBase. I think that is perfectly ok, and makes the connection to the interface very clear and regular.

Without the interface I would call the abstract base class Foo.

查看更多
祖国的老花朵
5楼-- · 2019-02-05 16:00

I too would suggest No, but not cast in stone...

Following OO mantra, your naming system should best represent the underlying objects that the code is supposed to be encapsulating. There should really be no 'meta language', related to the actual syntactical makeup of the programming language of choice in there.

That said, if your object is truly abstract and you really don't see it changing anytime soon, there is an argument that adding 'Base' helps with general readability.

As with most things, there's no blanket right and wrong answer - it depends on the overall layout of your codebase, what this specific code is supposed to be representing and the in-house style that you have. Just try to be consistent.

Is base used anywhere else?

查看更多
欢心
6楼-- · 2019-02-05 16:03

I side with "no" for exactly the refactoring reason you've cited.

A class should be named after what it logically represents, and nothing but the Object class is really really Base. Metaphysics ftw :)


re: Option B, there is nothing confusing about

namespace MySpecificNamespace
{
  MyClass: MyCommonNamespace.MyClass
  {
  }
}
查看更多
聊天终结者
7楼-- · 2019-02-05 16:03

"Abstract" prefix maybe?

查看更多
登录 后发表回答