MyClassBase or BaseMyClass? [duplicate] - naming-conventions

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Using “Base” in a Class Name
C# Class naming convention: Is it BaseClass or ClassBase or AbstractClass
Naming Conventions for Abstract Classes
What is the better way to name base abstract classes? I still can't decide where to put 'Base' word. Should it be 'BaseMyClass' or 'MyClassBase'?
How do you name those and why?

Imagine if you have a chain of abstract classes, would you call it MyClassBaseBaseBase then?
Avoid the use of "Base" in your name if you can. You have an abstract base class because it's somehow more generic than your concrete implementation. Name it in a more generic way then, describing what common ground it supplies for deriving classes.
If the above cant be done for some reason, I agree with the previous poster; use MyClassBase.

Subjective... But ok. This is my perception of things: BaseMyClass sounds like an imperative: "Base my class! Now!" :)
Whereas MyClassBase is clearer... a class that is a Base

MyClassBase is the way to go.
This is subjective but calling it MyClassBase brings readabilty and symmetry in design.

I wouldn't use the words "Base", "My" or "Class" at all but chose a name for the interface that actually describes what it is.

Not sure if this is correct but when I name my base class I want that just by looking at the class name one should understand that this is meant to be inherited. like I have two versions of webpart(GoogleSearch) one for mobile and other for normal brower then I would create an abstract base named GoogleSearchWebpartBase and name the webparts as MobileGoogleSearch and GoogleSearch.

A choice between between MyClassBase and BaseMyClass is like asking whether I'd prefer a kick in the nuts or a punch in the face.
I don't wish to offend, but both those names are awful. Specifically, there is no reason to include Class in the class name (unless you're modelling an eduction system). If you really need to use the class name to indicate it can't be instantiated, I would prefer to use the term Abstract, rather than Base.
As a general rule, look at how the standard libraries are named and follow that convention. If your standard libraries have names like MyClassBase and BaseMyClass, then naming is the least of your problems.

Related

Naming a class that controls another class

I have classes using the strategy pattern (each class has the same single method, but implements it very differently). I have another class which chooses the implementation to use based on runtime-accessible values. I have one final class which basically pulls the others together, calls the necessary methods of the implementing class and formats the output.
What could I name this controller class to make it at least semi-clear what it is for? Before someone asks, it is already a very small class (< 100 lines), not worth splitting - I'm confident that it's not because of multiple responsibilities that I'm having trouble naming it.
I want to say "controller" - but that's already a specific concept in MVC architecture (which our app is using). Any ideas? Is there an accepted name for the pattern I'm describing?
In OOP, classes should generally be named after what they are; class names should be nouns. You already know that.
The problem with class names like XyzManager or XyzController is that although technically they are nouns, they're really verbs disguised as nouns. Thus, such classes are named after what they do, instead of what they are. That makes them not objects, but services, or functions.
Now, naming is hard, and sometimes it can't be avoided with an XyzManager. Often, when it happens, it's because you've not yet realised what concept the class really should encapsulate. Still, you should strive to identify what the class is, instead of what it does, and name it after that.
FWIW, I often use a thesaurus (there are several excellent online services for that) to find a good name.
I would like to name it Service
Example: if you have a TaxStrategyGerman, TaxStrategyFrench that are chosen by TaxStrategyFactory and used by TaxCalculationService.

OOP - 'Default' bad class name?

I am implementing a template method type of pattern and have several classes to implement the behaviour.
As an example, my structure is as follows:
TemplateAbstract
Type
CustomType1
CustomType2
CustomType3
Default
The 'Default' class holds the behaviour should none of the custom types be needed. My question is, is 'Default' a bad name for the class?
I guess, since I am asking this question, I already have some serious doubts over the name, but what else would you call a class that provides the default behaviour amongst types?
For sake of example, do you mean something like this?
Number
Integer
PositiveInteger
NegativeInteger
OddInteger
EvenInteger
As Tom B pointed out in his comment, how you approach this depends on composition or inheritance; but I assume given this structure you laid out you mean to use inheritance. In my example having a "DefaultInteger" type seems rather awkward to use, but in your particular problem it may not be. And if Integer is just an interface a StandardInteger could make sense. Think about how a developer will use this type, and whether a "Default" makes sense as its own, separate thing:
Bread
YeastLeavened
Wheat
TwelveGrain
Default
Now I'm not sure what a Default yeast-leavened bread would be, but if it's a useful construct to your Baking program, then there's nothing inherently 'wrong' about it. Although I personally prefer to not have Default as its own type and instead just have a method on YeastLeavened.getDefault() to return the correct one.
Edit: So to answer your question directly, in general yes I would say "Default" is bad name unless you're always using it via a scope (Integer::Default), and in general is a hard class to have on its own conceptually (it's either abstract base functionality, or some concrete, "real" class functioning as what is used by default in the system)
Just expand upon the name, call it:
DefaultTypeBehaviour
That is very unlikely to cause a conflict, and tells you a little bit more about what the class does.
Ideally, the class name should describe the purpose.
"Default" is a vague term and doesn't have any significance with the functionality the class provides.
You can name your Default class something which tells that "this class has all the basic features implemented"
e.g. You can put the name something like "BaseType" or "BasicType" or "SimpleType" or anything else suits your need.

Use of the suffix "base" when naming types

I sometimes see types named with a suffix of "base", presumably meaning it forms the parent of one or more sub-types. Is implying the use of inheritance in the type name like this a code smell?
Very often the "Base" suffix is used for abstract classes.
It is also a common practice to give an implementation for each interface you provide in your API. When these implementations are abstract classes, used to implement logic common to all implementations, the suffix "Base" is appended to the name of the class which by convention lets consumer know they should inherit from that class for their concrete implementation.
Sometimes the common features of two child classes does not have a natural language equivalent, or the most obvious name would result in a name collision. In such cases I sometimes use a suffix in this manner. I would not consider it a code smell unless it was part of the exported API intended for use by other developers using a library.

Are there established alternatives to ISomething / ISomethingable for interfaces?

The .NET standard of prefixing an interface name with an I seems to be becoming widespread and isn't just limited to .NET any more. I have come across a lot of Java code that uses this convention (so it wouldn't surprise me if Java used it before C# did). Also Flex uses it, and so on. The placing of an I at the start of the name smacks of Hungarian notation though and so I'm uncomfortable with using it.
So the question is, is there an alternative way of denoting that Something is an interface, rather than a class and is there any need to denote it like this anyway. Or is it a case its become a standard and so I should just accept it and stop trying to stir up "religious wars" by suggesting it be done differently?
From the Framework Design Guidelines book:
Interfaces representing roots of a hierarchy (e.g. IList) should also use nouns or noun phrases. Interfaces representing capabilities should use adjectives and adjective phrases (e.g. IComparable, IFormattable).
Also, from the annotations on interface naming:
KRZYSZTOF CWALINA: One of the few
prefixes used is “I” for interfaces
(as in ICollection), but that is for
historical reasons. In retrospect, I
think it would have been better to use
regular type names. In a majority of
the cases developers don’t care that
something is an interface and not an
abstract class, for example.
BRAD ABRAMS: On the other hand, the “I” prefix on interfaces is a clear
recognition of the influence of COM
(and Java) on the .NET Framework. COM
popularized, even institutionalized,
the notation that interfaces begin
with “I.” Although we discussed
diverging from this historic pattern
we decided to carry forward the
pattern as so many of our users were
already familiar with COM.
JEFFREY RICHTER: Personally, I like the
“I” prefix and I wish we had more
stuff like this. Little one-character
prefixes go a long way toward keeping
code terse and yet descriptive. As I
said earlier, I use prefixes for my
private type fields because I find
this very useful.
BRENT RECTOR Note:
this is really another application of
Hungarian notation (though one without
the disadvantages of the notation's
use in variable names).
It has very much become a widely adopted standard, and while it is a form of Hungarian, as Brent states, it doesn't suffer from the disadvantages of using Hungarian notation in variable names.
I would just accept it, to be honest. I know what you mean about being a bit like Hungarian notation (or at least abuse of the same) but I think it gives sufficient value to be worth doing in this case.
With dependency injection being in vogue, often I find I end up with an interface and a single production implementation. It's handy to make them easily distinguishable just with the I prefix.
One little data point: I work with both Java and C# a fair amount, and I regularly find myself having to check which types in Java are actually interfaces, particularly around the collection framework. .NET just makes this simple. Maybe it doesn't bother other people, but it bothers me.
+1 for IFoo from me.
As a .NET programmer (for the most part), I actually prefer the Java convention of dropping the I here, for a simple reason: Often, small redesigns require the change from an interface into an abstract base class or vice versa. If you have to change the name, this might require a lot of unnecessary refactoring.
On the other hand, usage for the client should be transparent so they shouldn't care for this type hint. Furthermore, the “able” suffix in `Thingable” should be enough of a hint. It works well enough in Java.
/EDIT: I'd like to point out that the above reasoning had prompted me to drop the I prefix for private projects. However, upon checking one of them against the FxCop rule set, I promptly reverted to the usage of I. Consistency wins here, even though a foolish consistency is the hobgoblin of little minds.
Its all about style and readability. Prefixing Interfaces with "I" is merely a naming convention and style guideline that has caught on. The compilers themselves couldn't care less.
My main assumption is that the most important thing is to maintain readability in domain part of the implementation. Therefore:
If you have one behaviour and one possible implementation, then just don't create an interface:
public class StackOverflowAnswerGenerator { }
If you have one behaviour and many possible implementations, then there is no problem and you can just drop the "I", and have:
public interface StackOverflowAnswerGenerator {}
public class StupidStackOverflowAnswerGenerator : StackOverflowAnswerGenerator {}
public class RandomStackOverflowAnswerGenerator : StackOverflowAnswerGenerator {}
public class GoogleSearchStackoverflowAnswerGenerator : StackOverflowAnswerGenerator {}
//...
The real problem comes when you have one behaviour and one possible implementation but you need an interface to describe its behaviour (for example for convenient testing, because of convention in your project, using some library/framework which enforces this, ...). Possible solutions, other from prefixing the interface are:
a) Prefix or suffix the implementation (as stated in some other answers in this topic)
b) Use a different namespace for interface:
namespace StackOverflowAnswerMachine.Interfaces
{
public interface StackOverflowAnswerGenerator {}
}
namespace StackOverflowAnswerMachine
{
public class StackOverflowAnswerGenerator : Interfaces.StackOverflowAnswerGenerator
{}
}
c) Use a different namespace for implementation:
namespace StackOverflowAnswerMachine
{
public interface StackOverflowAnswerGenerator {}
}
namespace StackOverflowAnswerMachine.Implementations
{
public class StackOverflowAnswerGenerator : StackOverflowAnswerMachine.StackOverflowAnswerGenerator
{}
}
Even though I think the last possibility is the cleanest, its one drawback is that even though using StackOverflowAnswerMachine; gives you access to all domain objects you must prefix all domain interfaces not to be confused with their implementations. That may feel like something not very convenient but in clean design usually a class doesn't use many other domain objects, and mostly you need to use the prefix only in field declaration and constructor parameter list. So, that is my current recommendation.
The client of domain functionality shouldn't need to know whether they're using an interface, an abstract class or a concrete class. If they need to know this, then there is some serious problem in such a project, because it has domain logic and infrastructural concerns mixed on the same abstraction layer. Therefore I recommend "a" or "c" solutions.
The coding standard for Symbian has interfaces (pure abstract C++ classes) denoted with an M rather than an I.
Otherwise, the only other way I have seen of denoting interfaces is through context.
For .NET, Microsoft's Framework Design Guidelines book absolutely recommends it, and yes, it is very much standard. I have never seen it done otherwise, and to create a new convention would only serve to confuse people.
I should add that I dislike Hungarian notation too, but this and the case of prefixing class variables with an underscore are good exceptions to me, because they make code so much more readable.
I've always thought this naming convention is a bit of a dinosaur. Nowadays IDEs are powerful enough to tell us that something is an interface. Adding that I makes the code harder to read so if you really want to have a naming convention that separates interfaces from classes I would append Impl to the name of the implementing class.
public class CustomerImpl implements Customer
You asked for an alternative, so here is one I have encountered:
Use no prefix on the interface class, but use a c or C prefix on the corresponding concrete classes. Most of your code will generally reference the interface, so why pollute it with the prefix and not the generally much less used concrete type.
This approach does introduce one inconsistency in that some concrete types will be prefixed (the ones with matching interfaces) and others will not. This may be useful since it reminds developers that an interface exists and its use should be preferred over the concrete type.
To be honest, I use the prefix on the interface, but I think it is more because I have become so accustomed and comfortable with to it.

Using "Base" in a Class Name

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
{
}
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.
"All your BaseClass are belong to us."
I side with a definitive no, with a single exception. If you are writing an app to manage military installations or baseball stadiums, go for it.
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
{
}
}
Classes that have the same name as their parent classes bug me to no end. In Java java.sql.Date extends java.util.Date. This is very annoying because you have to specify the exact class you want to import or else specify the classname fully (including package/namespace).
Personally I prefer to name things as they are; if a Base or Abstract class exists only to provide a partial implementation of something, and doesn't represent the interface for that thing, it is often acceptable to put the word Abstract or Base in its name. However, if that class represents the interface as well, then you should just name it after what it does.
For example, in Java, we have the Connection interface (for DB connections). It's just called Connection, not IConnection. You use it like this:
Connection con = getConnectionFromSomewhere();
If you are making a JDBC driver and need to implement connection, you could have a ConnectionBase or AbstractConnection which is the lower layer of the implementation detail of your particular Connection. You might have
abstract class AbstractConnection implements Connection
class OracleConnection extends AbstractConnection
or something like that. The clients of your code, however, never see AbstractConnection nor do they see OracleConnection, they only see Connection.
So, in general, classes that are meant to be generally useful should be named after what they represent/do, whereas classes that are helpers for code maintenance/organization can be named after what they are.
*ps I hate naming Interfaces with I. Do people name all their classes with C? It's 2009! your IDE can tell you what type of object that is, in the odd case when it even matters if it's an interface or a class.
I think it's worth wiki-fying this question.
FWIW, I agree. I usually try to find a more "generic" term for my base classes. So if I have a "Customer" class and need to introduce a new base class for it, I'd go with "Contact" or something rather than "CustomerBase".
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?
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.
I also side with the no camp...place a Base in there today and in 6 months someone will whack a MyDerivedClass class in you code base while you're not looking.
"Abstract" prefix maybe?
I usually go with IFoo for the interface and AbstractFoo for the skeletal implementation, which is a mix of .NET and Java conventions.
I think it should probably be avoided where possible in favour of an identifier that actually describes what it is!
This question is difficult to answer because it's abstract. I might, for example, consider calling the base of MyClassA and MyClassB, "MyClass".
I agree, AbstractFoo is a decent solution. I try to pick names that don't need additional adjectives. I would shy away from using Base.
It seems like any principled answer will end up being no... However, comma, when I'm looking at code I'm not particularly familiar with, which happens a lot in python (where the source code is sometimes the only dependable documentation), I find it really helpful when a class has Base in it. Python is different from other OO languages where the class is defined with an "abstract" or "interface" specifier though. For naming, I like to ask myself "if I have never seen this code before, which way would make it easier for me to understand this code?" (Then, depending on how lazy I'm feeling, I name it accordingly).