Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for naming subclasses

I am often in a situation where I have a concept represented by an interface or class, and then I have a series of subclasses/subinterfaces which extend it.

For example: A generic "DoiGraphNode" A "DoiGraphNode" representing a resource A "DoiGraphNode" representing a Java resource A "DoiGraphNode" with an associated path, etc., etc.

I can think of three naming conventions, and would appreciate comments on how to choose.


Option 1: Always start with the name of the concept.

Thus: DoiGraphNode, DoiGraphNodeResource, DoiGraphNodeJavaResource, DoiGraphNodeWithPath, etc.

Pro: It is very clear what I am dealing with, it is easy to see all the options I have

Con: Not very natural? Everything looks the same?


Option 2: Put the special stuff in the beginning.

Thus: DoiGraphNode, ResourceDoiGraphNode, JavaResourceDoiGraphNode, PathBaseDoiGraphNode, etc., etc.

Pro: It is very clear when I see it in the code

Con: Finding it could be difficult, especially if I don't remember the name, lack of visual consistency


Option 3: Put the special stuff and remove some of the redundant text

Thus: DoiGraphNode, ResourceNode, JavaResourceNode, GraphNodeWithPath

Pro: Not that much to write and read Con: Looks like cr*p, very inconsistent, may conflict with other names

like image 975
Uri Avatar asked Feb 22 '09 03:02

Uri


People also ask

What is the best choice for naming a class?

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

How do you name an extended class?

Naming extension classes Preferably, the extension class should include the name of the type that is being extended. However, the name must also include a term, abbreviation, or prefix that distinguishes the class from other types.

How do you name a class manager?

Using words like "Manager", "Service" or "Handler" in your class names can be considered too generic, but since a lot of programmers use them it also helps understanding what the class is for. I myself have been using the facade-pattern a lot (at least, I think that's what it is called).

Are naming conventions important?

A file naming convention is a framework for naming your files in a way that describes what they contain and how they relate to other files. File naming conventions help you stay organized and makes it easier to identify your files. By consistently organizing your files, you will be able to quickly find what you need.


1 Answers

Name them for what they are.

If naming them is hard or ambiguous, it's often a sign that the Class is doing too much (Single Responsibility Principle).

To avoid naming conflicts, choose your namespaces appropriately.

Personnally, I'd use 3

like image 90
Mitch Wheat Avatar answered Oct 11 '22 03:10

Mitch Wheat