Sometimes I can see method definition like:

public User findUserById(int id){

But my justification is, isn't "(int id)" already include the information of "ById"?

is it better to have naming just like:

public User findUser(int id){

instead of having "ById" to repeat the already present information?

  • 2
    What happens when you want to sometimes find by username and other times by email, when both are strings? Commented Jun 27, 2018 at 6:00
  • 1
    ... and the API documentation is so poor that lint's suggestions show things like findUser( String arg1 )
    – Laiv
    Commented Jun 27, 2018 at 6:06
  • 7
    It happens too often that I need a findUserBy(long id) signature as well as a findUserBy(long idForeignKey) signature that would make it silly to not at least attempt to differentiate the two as a general rule.
    – Neil
    Commented Jun 27, 2018 at 6:33
  • 2
    One way I've always wanted to do but never been disciplined/bothered to do is subclass your IDs to their underlying type, e.g. a UserID inherits from Int and a UserName (or email) inherits from String. This means that you can in fact remove the ID thing as you suggest and you also get the compiler to detect bugs caused by mistakes such as passing the email address to the user name version. I think it's a good concept, but one I've yet to put into practice. Commented Jun 27, 2018 at 8:37

1 Answer 1


It really depends on how many ways you have of finding something, the coding standards for your team, etc. You do want to distinguish between finding by an identifier and finding by the number of posts or some other integer related attribute of a User.

My personal preference is to distinguish between a simple lookup and search with the verbs get and find. For example:

public User getUser(int id) { }
public User getUserByUsername(String username) { }

public Enumerable<User> findUsersForRole(Role role) { }

But that is a personal preference. The ByXXX suffix provides context when you read the code. It can be very helpful when your variable names are not necessarily named well, or the value passed in is hard coded.

What's more important than personal preferences is that the naming conventions make sense to the team that has to maintain the code. In my example, I have a simple getUser(int id) method for the primary means of getting a User. That said, if I were maintaining software that already had the convention of using the ByXXX suffix I would adapt. I would even adapt to using findByXXX for everything if that was the convention.

Your Answer

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

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