Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java - DAO methods convention - overloading or change the method name?

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.

like image 274
Ismail Yavuz Avatar asked Sep 28 '22 01:09

Ismail Yavuz


1 Answers

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.

like image 134
Kayaman Avatar answered Nov 02 '22 05:11

Kayaman