136

What is the recommended approach to naming base classes? Is it prefixing the type name with "Base" or "Abstract" or would we just suffix it with "Base"?

Consider the following:

type: ViewModel e.g. MainViewModel, ReportViewModel

base class: BaseViewModel or ViewModelBase or AbstractViewModel

Also consider:

type: Product e.g. VirtualProduct, ExpiringProduct

base class: BaseProduct or ProductBase or AbstractProduct

Which do you think is more standard?

class Entity : EntityBase
{
}

or

class Entity : BaseEntity
{
}
John Washam
  • 4,073
  • 4
  • 32
  • 43
Soni Ali
  • 18,464
  • 16
  • 44
  • 53
  • 2
    See also [Naming conventions for abstract classes](http://stackoverflow.com/a/429494/33499) – wimh Nov 28 '11 at 08:25
  • possible duplicate of [Using "Base" in a Class Name](http://stackoverflow.com/questions/510839/using-base-in-a-class-name) – nawfal Jun 02 '13 at 09:12

8 Answers8

105

There are examples in the Framework with the Base suffix, e.g. System.Configuration.Provider.ProviderBase, System.Web.SessionState.SessionStateStoreProviderBase.

But by no means all abstract base classes in the Framework follow this convention (e.g. System.Data.Common.DbParameter, System.Data.Common.DbCommand).

Personally I would avoid using the suffix unless I wanted to emphasize the fact that it's an abstract class and felt that otherwise users of the class might expect the name to indicate a concrete implementation.

sshow
  • 8,820
  • 4
  • 51
  • 82
Joe
  • 122,218
  • 32
  • 205
  • 338
  • 11
    "`Avoid` naming base classes with a `Base` suffix if the class is intended for use in public APIs." Framework Design Guidelines, page 174 – Yousha Aleayoub May 15 '20 at 09:46
49

None of the above. Consider what purpose your base class provides; name it that. For example, the base class of Automobile and Bicycle could be Vehicle.

If you're creating base classes just to have a base class of one class, and with no purpose or reason other than that, you're probably doing something wrong.

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117
  • 23
    That is not always the case and you will find many Base* classes in the framework. CollectionBase, DictionaryBase .... etc – Chad Grant May 05 '09 at 21:53
  • 1
    Good point the real world objects should fit exactly in artifacts. The abstraction of commons should be based like Vehicle, specifics have to go behind but also exact. – ruslander May 08 '09 at 21:53
  • 1
    Or what about a PageBase in a web application? –  Jan 10 '10 at 12:51
  • 51
    Worth noting that some of the classes in the .NET Framework with a "Base" suffix were named before version 1.0 and Microsoft no longer follows those conventions.... but they can't change the names now. Microsoft's Framework Design Guidelines book, section 6.2 (which covers base classes), specifically recommends against using the "Base" suffix. – Warren Rumak Jan 09 '12 at 19:13
  • 6
    The practice I've seen used most often in a number of OO languages at a number of companies is that “Base” designates base classes that are meant only to be inherited from— not to be used directly.  With your example, a class named “Vehicle” sounds like something that can be used and “driven”— it claims itself to be a vehicle.  However, if the base class were named “Vehicle Parts” or “Vehicle Essentials” or “Vehicle Kit”— we know it to not be something directly usable, but a base for a variety of vehicles. – Slipp D. Thompson May 17 '14 at 21:33
  • 'a class named Vehicle sounds like something that can be used and driven' Exactly so. You can see the problem by writing "VehicleKit car = new Car()" - a car !is-a VehicleKit. – Iain May 19 '14 at 00:26
  • 5
    As mentioned by Warren, Microsoft Framework Design Guidelines book, section 6.2 on Base Classes: https://msdn.microsoft.com/en-us/library/ms229003(v=vs.110).aspx They say to avoid the Base suffix: "AVOID naming base classes with a "Base" suffix if the class is intended for use in public APIs." – cwills Dec 12 '16 at 20:55
  • 1
    As mentioned, the Design Guidelines book recommends avoiding the Base suffix. However, the book offers no explanation, justification, or alternative; absent logical reasoning, we may infer solely aesthetic motivation. DG points out that "Base-ness" can be stated in other ways (e.g. `abstract`); so what do we name the class? The name should distinguish within folder/project/etc and uniquely denote intended utility. The name `XBase` fulfills all of these requirements in a manner that couldn't be simpler or clearer. Maybe instead of avoiding the suffix we can just avoid the book. – Jake Sep 19 '19 at 09:19
  • 1
    This only makes sense in a limited number of scenarios. As already mentioned by others, how would we go around naming a page base, or a control base? @Jake said it well and clear. Use generic terms whenever applicable (i.e. Vehicle > Automobile/Bycicle scenario). Use base suffix whenever generic terms do not make sense. Either way, whatever you choose, try and stick with ONE single convention. – String.Empty Mar 15 '21 at 23:10
  • It's quite interesting, because Microsofts Own Public API for creating controllers are [ControllerBase](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase?view=aspnetcore-6.0). Regardless if you choose to use it or not use it, Microsoft definitely prefers Base as the suffix. – Erik Philips Aug 10 '22 at 23:45
5

If you're talking about virtual base classes, Microsoft's standard is ClassnameBase (like CollectionBase.)

Yes - that Jake.
  • 16,725
  • 14
  • 70
  • 96
  • From the docs: "AVOID naming base classes with a "Base" suffix if the class is intended for use in public APIs." – Eyad Arafat Feb 02 '23 at 16:44
3

We use BaseEntity, but I think it your own preference. I frequently see the other.

Just be consistent within your context, be that your project, namespace or if possible, your team. Different conventions are worse than a bad convention IMHO.

Gary.Ray
  • 6,441
  • 1
  • 27
  • 42
3

I think its a matter of choice. I'd say if you are creating a lot of base classes then perhaps it is better to go with BaseClassname always because that way you can ALWAYS find out what base classes you can start using by just typing Base and getting the rest of the help from Intellisense. What if you had 20 Base classes and you added Base as suffix and you forgot what was the name of the base class? Do you want to create a class diagram first from VS and find out what base classes are available? It's alright to name them ClassBase when it is just one or two classes.

Same goes for decision between GetItems and ItemsGet function. I'd say for readability's sake atleast - go for GetItems. Follow the conventions :)

  • 1
    'Base' classes are an OO hack, see [Paul's comment](http://stackoverflow.com/a/826838/162215). They're often helpful and sometimes necessary, but still they're a hack. If you feel a need to look for all available base classes, why is that? What do they share that creates your need to see them all together? The answer could help you find a better naming. – Iain Aug 01 '12 at 01:11
  • 1
    @Abhishek: The analogy to `GetItems` doesn't work— “Get” is only a verb and thus only makes English grammatical sense as a prefix, whereas “Base” works as both an adjective (prefix) or noun (suffix). – Slipp D. Thompson May 17 '14 at 21:12
  • 2
    @Iain: The reason people use “Base” in class names isn't to be able to find all the base classes— the rationale is closer to the opposite.  It's so that the base class of a number of related classes stands out from them, much in the way the “I” prefix is meant to distinguish an interface from the classes that adopt it.  I've seen the “Base” prefix/suffix used most often when the base class is non-functional on its own—  abstract by intended usage, regardless of an enforced `abstract`. – Slipp D. Thompson May 17 '14 at 21:26
  • @SlippD.Thompson yeah, 'find all the classes' was a response to Abhishek. I replied on Paul's answer. – Iain May 19 '14 at 00:28
2

Personally, I would recommend against adding the word base at all. You never know when you'll have to change the code around and it won't be the base object anymore. That being said, we have done this in the past, we prefixed the word Base on the front. It seems to flow better.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
-1

BaseEntity looks a lot like camel case - strName, bseEntity. I'd go for EntityBase since it defines the subject first, which will help you identify it's function quicker.

Mike Robinson
  • 24,971
  • 8
  • 61
  • 83
  • 11
    You're talking about Hungarian notation, not camel casing. Hungarian notation just happened to be camel-cased, but they're completely different things. – sliderhouserules Jul 01 '10 at 06:47
-8

Always think about alphabetizing when you name stuff. I really don't like looking at a SQL server and every stored procedure is named usp[something]. Along the same lines, don't overuse Get and Set as leading names for a function. Instead of GetItems or PlaceOrder, think about naming them as ItemsGet or OrderPlace.

So, in general, ClassnameBase / EntityBase would be a better choice.

marcc
  • 12,295
  • 7
  • 49
  • 59
  • 11
    Disagree; Readability. GetItems makes a lot more sense than ItemsGet. – Nathan Ridley May 05 '09 at 21:37
  • 3
    Disagree... same reason as above.. but unable to vote down... :( – Mugunth May 06 '09 at 06:36
  • Contrary to the other comments marcc have NOT suggested something like itemgsGet(). He wrote items(). – Hontvári Levente May 05 '15 at 12:22
  • sounds too much like you are relying on your IDE to show you what you are looking for rather than knowing your code. Also sounds like an IDE issue if your IDE doesn't do fuzzy name matching to help you find what you're looking for. This seems like more of an IDE hack than a helpful coding practice. Your code may outlive your IDE, and a new IDE or version feature might render this useless. – grego Nov 13 '20 at 00:13