5

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?

  • 1
    If it is an abstraction of the data base why not just call it DataBase? – Roman Reiner Jul 28 at 6:48
13

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.

Your idea seems to be about Persistence. That's a fine noun.

4

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

1

It looks like it is a utility class. It's common to just name is WhateverItIsUtil but you need to ask yourself if they would be better placed in the class they are used on.

Also, I don't think you should name it a verb. Many might also name it a SomethingManager.

Obviously if you need to share the methods with many classes then you might feel stuck with this implementation. The reason you might want to include them in only the class that uses them is because of the Single Responsibility Principle. It states that an implementation should only require a single change from one 'actor.' An actor is a group that would require the changes. If you have multiple actors that would require a change to the method then changing it would break the code for the other actors and then you have a maintainability issue. So the context is always going to matter for this type of thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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