4

GOAL: naming a class which has the responsibility to save data in Android database. The class structure is like this

ClassName {

 public void saveSync(...){
     /* implementation*/
 }

 public void auxiliaryMethod(...){
    /* implementation */
 }
}

QUESTION: which is the proper manner to name this kind of class? Should I always find a noun or can I use a verb?

8

If a class is properly encapsulated it's hard to tell if it even has fields from outside.

Some classes are immutable. You can't change their fields once their objects are constructed. Your class is also immutable. But not only can't its object change, you only have one way to build it.

That doesn't mean you have to think of the whole thing as a verb. It can still be a noun. It's just a noun that comes in only one flavor.

What you have is a bag of functions that move around together. If your class is properly designed those functions have something to do with each other. That single unifying idea that brought them together should give the class it's name. The name should make clear what functions belong in the class and what functions don't. If people read the name and then are surprised by what they find inside then you have a bad name.

3

When I find myself in this situation, I take a step back and think about how I am going to call the methods. Forget nouns vs. verbs for a minute. Then I play fill-in-the-blank and go with what sounds most natural.

  • ___________.saveSync()
  • dataManager.saveSync()?
  • saveHelper.saveSync()?
  • entityRepository.saveSync()?

In some cases I don't even name the class until after I have called it from a few places. Call it NewClass1234 until you can see it being called in context, and then read the code aloud while trying different names.

You could also show the code to a coworker and ask for suggestions. I try to reduce the amount of time coworkers will puzzle over my naming, so getting that feedback upfront can help.

1

First consideration for me is whether the data represents a concept. If so, I would take the name of the concept and optionally add a "DAO", such as "UserDAO".

If the data is a mixture of whatever, you might also consider more general topic such as "UtilDAO".

Please refer to https://en.wikipedia.org/wiki/Data_access_object

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.