Java naming conventions and overloading

I have a service that should return a person according to some properties. And I see two strategies for designating these methods:

First:

getPersonById(int id) getPersonByBirthDate(Date date) getPersonByBirthDateAndSex(Date date,Sex sex) getPersonByNameAndSex(String name,Sex sex) getPersonByBirthDateAndNameAndSex(Date date,String name,Sex sex) etc.. 

or applying overload rules, follow these steps:

 getPerson(int id) getPerson(Date date) getPerson(Date date,Sex sex) getPerson(String name,Sex sex) getPerson(Date date,String name,Sex sex) 

What is correct according to java naming convention?

+5
source share
2 answers

Regardless of the overload issue your design suffers from the combinatorial explosion of the API: you begin to introduce methods for all possible combinations of search criteria. An alternative would be the introduction of a builder who collects the criteria and finally returns the person.

Example:

  queryPerson().byId(5).run(); queryPerson().byName("John").bySex(Sex.MALE).run(); queryPerson().bySex(Sex.FEMALE).run(); 

And now the service API only has a good queryPerson() method.

+9
source

Overload is always better. Many of the KNOWN java-based systems follow this approach. In addition, the basic concept of congestion is related to the condition you specify.

Overloaded methods simplify the readability of developers, since individual names should not be remembered. In addition, the eclipse function Ctrl + Space always tells you which method you want to use.

+2
source

All Articles