Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word order in class names in java

I had a quick question about naming conventions in Java. I read a few guidelines, but none answered my specific question.

What word order should I use?

For example, I have an abstract class:

abstract class Requirement

If I wanted to make children, how do I name them?

Like this:

class RequirementOnlyPlayer extends Requirement
class RequirementOnlyConsole extends Requirement
class RequirementArgumentCount extends Requirement

Or like this:

class OnlyPlayerRequirement extends Requirement
class OnlyConsoleRequirement extends Requirement
class ArgumentCountRequirement extends Requirement

Or in some other way? I would reckon it's my first example, but I'm not sure.

This is how these requirements would then be used:

addRequirement(new RequirementOnlyPlayer());
addRequirement(new RequirementArgumentCount(3));

Any help or feedback would be greatly appreciated! Sorry if this a bit of a "nooby" question, I'm quite experienced with Java and programming in general, but for some reason I'm still not too sure on this.

like image 678
Vapid Linus Avatar asked Aug 06 '14 18:08

Vapid Linus


People also ask

What are the rules for naming classes in Java?

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).

Can we use _ in class name in Java?

Can Java class name contain underscore? Technically it's possible to use the underscore in a class name, but there's no practical reason to do so. If the underscore were used in a class name, it would act as a separator between words. In Java, we achieve optical separation between words with the camel case.

Should class names start with capital letters?

When you are talking about the name of a specific class or course, such as Math 241 or Chemistry 100, always capitalize it. Capitalize course titles such as History of the French Revolution and Childhood Psychology.

Should class names be singular or plural?

Class names are singular, not plural. It is incorrect to name a class “Books”. Although there may be many book objects, there is only one book class.


2 Answers

If you look in the standard Java library, you will usually either find the parent class as a suffix, or the child class will have a completely different name. Some examples :

// in java.io - often suffixes:
abstract class InputStream
class FilterInputStream extends InputStream
class BufferedInputStream extends FilterInputStream
class ByteArrayInputStream extends InputStream

// in javax.swing - often completely different names:
abstract class JComponent
class JLabel extends JComponent
abstract class JTextComponent extends JComponent
class JEditorPane extends JTextComponent

However, what all these examples have in common is that they make sense in plain English. I think the reason why we often find the name of the parent class as a suffix for the child class is simply because adjectives are put before the nouns they qualify in English (and the child classes often take on the name of the parent class, and add an adjective to explain their specificity). If you named your classes in a language like French, you would often want to do the opposite!

At the end of the day, I'd say that you should go with what sound like correct English, which I think would be your second proposition. But I would also consider dropping the "Requirement" part entirely - after all, OnlyConsole (or ConsoleOnly?) may already sound like it is a Requirement, just like a Timestamp is quite obviously a kind of Date.

like image 101
Cyäegha Avatar answered Sep 21 '22 00:09

Cyäegha


I'd suggest you to name your Classes as

OnlyPlayerRequirement
OnlyConsoleRequirement
CountArgumentRequirement

This is suggested because the classes in Java are thought to have some real world linking and some real world relation. These names are not in a pre-fixed order or as per some rule but simply on the real-world existence. See, the OnlyplayerRequirement suggests that only player is required and so on for the other two...

like image 22
Am_I_Helpful Avatar answered Sep 22 '22 00:09

Am_I_Helpful