5

Related to Standard Naming Convention for DAO Methods and DAO class methods naming questions.

Why the methods in DAO classes are like:

getUserById(int id)
getUserByUsernameAndPassword(String username, String password)

instead of:

getUser(int id)
getUser(String username, String password)

in IDE like Eclipse auto-suggest will start to show you both when you start to type getUser. And according to the parameters you can choose which method is to go with.

Of course this is overloading. Why people avoid overloading and use different method names for different parameters? Or are they avoiding?

Regards.

Community
  • 1
  • 1
Ismail Yavuz
  • 6,727
  • 6
  • 29
  • 50
  • 1
    What if you have two methods that take 2 String parameters? Oops, your naming scheme just failed. Not to mention that it's a lot easier to see what's going on when the methods are clearly named. – Kayaman Aug 12 '15 at 09:34
  • I think it's more readable when you have different method names instead let's say 5 methods `getUser`. – wawek Aug 12 '15 at 09:35
  • Check which one is more understandable. – Sai Ye Yan Naing Aye Aug 12 '15 at 09:36
  • 1
    @Kayaman 2 methods `getUser()` which both accept 2 strings as parameters? Why? Until the methods are designed for same operations like `getUser` it's very good practice to overload them. Different method name should be used if the next method is going to make another or addidional operations than predecessor – itwasntme Aug 12 '15 at 09:38
  • @ismailyavuz overriding and overloading are different mechanisms both used as part of polymorphism but for different reasons. – itwasntme Aug 12 '15 at 09:39
  • 1
    @mastah While normally it's a often useful to use overloading if the method doesn't essentially differ except for the parameters, in a DAO (especially non-trivial ones) this can result in bad things as described in my answer. – Kayaman Aug 12 '15 at 09:50

1 Answers1

4

Your proposed naming scheme fails in 2 (obvious) ways.

First way, conflicting method signatures:

getUser(int id);
getUser(int age);
getUser(String username, String password);
getUser(String firstname, String lastname);

Second way, unclear code requiring you to verify parameter types and names:

// What's being used to search for users in this code?
User user = getUser(poorlyNamedVariable);  

Not to mention potential errors, when your variable isn't the type that you thought it was.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Third way, using Spring Data. – Branislav Lazic Aug 12 '15 at 09:59
  • Can you explain more about Spring Data? @BranislavLazic – Ismail Yavuz Aug 12 '15 at 10:37
  • @ismailyavuz Would it be ok in answer? – Branislav Lazic Aug 12 '15 at 10:38
  • I think that is not directly related to the question but may be I am wrong? Only you know what you want to say :) I use Spring with hibernate and I still have this SO question in my mind. What can Spring Data offer to me about this? – Ismail Yavuz Aug 12 '15 at 10:42
  • I have no idea how Spring Data is related to this question, or my answer. – Kayaman Aug 12 '15 at 10:46
  • OK, I've accepted, you are right. So where to use overloading? I think with this perspective no where to use. Right? @Kayaman – Ismail Yavuz Aug 18 '15 at 06:58
  • @ismailyavuz Just because it doesn't work very well for DAOs doesn't mean it's not useful elsewhere. DAOs just happen to be badly suited for overloading, since there are usually several read-methods that have similar parameter types. It doesn't happen as easily with other types of classes. – Kayaman Aug 18 '15 at 07:03