4

(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?

9
  • What is a "Function" for you? I don't really see what you mean with that. Commented Jun 13, 2018 at 8:27
  • 2
    I think there is no rule for that. Only the common sense saying "name the thing so you and others can understand what it is" Commented Jun 13, 2018 at 8:27
  • You can try this : link Commented Jun 13, 2018 at 8:28
  • I'd probably follow the example of the classes in java.util.function. Commented Jun 13, 2018 at 8:28
  • Yes but I want it to be universally understood, so I can't go around naming it HouseButtonsWidget or something like that, because it will be misconstrued as a java widget. Commented Jun 13, 2018 at 8:29

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Chapter 2 in Clean Code is the updated version of that paper that Bob asked me to write for the anthology. If you can get a copy of each, you'll see some of the evolution (and I won't make a penny from you doing so).
1

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.

Comments

0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.