kay
kay

Reputation: 481

Java naming guide (specific class type naming - not conventions)

(This has nothing to do with Java conventions)

I'm looking for a guide that can help me understand how to name classes that function in certain ways.

I have looked online and read on various websites about this; but, all I learned is what not to do. i.e lower casing/dashes/dollar-signs.

But what I really want to know is how to name classes like these:

(Looking through similar projects I see that people named it "Module") In this example, I will be naming them "Function".

public class HouseButtonsFunction extends ButtonSwitchFunction{

@Override 
public void interact(Person p, Object...args){
    //if person turns a light switch or open-garage switch
}

}


public class CarButtonsFunction extends ButtonSwitchFunction{

@Override 
public void interact(Person p, Object...args){
    //if person turns on ignition or roof lights
}

}

There are also superclasses like

PersonInteractionFunction

that would deal with things like:

PersonWalkingFunction extends PersonInteractionFunction
PersonSittingDownFunction extends PersonInteractionFunction
PersonDrivingFunction extends PersonInteractionFunction

So is "Function" the correct word here?

Where can I find out the correct nomenclature?

Upvotes: 4

Views: 207

Answers (5)

Ramesh
Ramesh

Reputation: 76

There is no hard rule as many stated in comments. Naming convention is a best practice so when all developers follow a certain naming standard then they can easily correlate and understand code effectively, otherwise it's cumbersome task to build relation.

In general practice a Class should be referred to Noun where as methods refer to action so those are associated with verbs.

In above case, I would suggest something like this..

HouseButtonsFunction => House or HouseBotton

ButtonSwitchFunction => Switch or SwitchBotton

CarButtonsFunction => CarButton

PersonWalkingFunction => Walker

PersonSittingDownFunction => Sitter

PersonDrivingFunction => Driver etc..

Note that it all depends on naming convention you follow..

Upvotes: 0

Dishonered
Dishonered

Reputation: 8841

Ideally the name of the class should be a noun which is specific and is easy to understand to someone reading the code. Ending the class name with 'Function' is fine considering the super class seems to be named Function as well. But if you can rename the super class i would name them ending with Handler. Like

  • PersonWalkingHandler
  • PersonSittingDownHandler
  • PersonDrivingHandler

etc

Upvotes: 0

vincrichaud
vincrichaud

Reputation: 2208

There is no rule/conventions about how to name your class and methods. You have to follow the common sense : Name things explicitly so you and others can understand what it is and what it does

As example, for the case of a button in a house, with a function that interact this button. You can think of different valid naming. The class could be named HouseButtonFunction or HouseButtonAction, etc. And the method could be named interact() or action(), etc. The difference depend on what does exactly your method. If it toggles something, use toggle(). Use the one you find the most understandable and the most explicit. In your case, personnaly, since your class define the interaction of this button, I would suggest to name it HouseButtonInteraction

In all the case, I strongly recommend to comment your stuff so there is no more possible confusion.

/**
 * This method is called when a person interact with the House button.
 * This method will action the given object
 * @param p : the person that interact with the house button
 * @param o : the object to action
 */
public void interact(Person p, Object o) { ... }

Also try to always be consistent in your naming. i.e. If for the house button you name the class HouseButtonFunction and the method interact. Do not change the naming for the car button :

//not to do !!
class : CarButtonAction
method : actionObject()
//To do
class : CarButtonFunction
method : interact()

Upvotes: 0

Oz Molaim
Oz Molaim

Reputation: 2136

I suggest you to read the classic an most famous document that was written about naming which is called "Ottinger's Rules for Variable and Class Naming". It was written by Tim Ottinger back in 1997 and it is still relevant as it was written today.

I would also suggest you to read the books Clean Code written by Robert C. Martin (Uncle Bob) and Implementation Patterns written by Kent Beck

Upvotes: 2

Andrew
Andrew

Reputation: 49606

I would use neither "Module" nor "Function" here.

"Module" is associated with a storage. For instance, it can be a class that holds a collection or gathers information about classes that can be logically grouped together.

"Function" is a means to transform something into something else. For example, a Function<Person, Rabbit> turns a human into a rabbit.

I would go with the word "Action" since you defined a single method interact.

Upvotes: 1

Related Questions