Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming Convention for methods that return different types with similiar parameters

I have a SearchService which uses an algorithm for querying a database and returing the results. There are a couple of different formats the data can be returned as, depending on what the invoker wants from the service. These formats are:

  • A list of entities that directly match against a table in the database
  • A list of primary keys (Longs) of the records that match
  • A list of 'search results' which is composed of a bunch of fields that are generally relevant to what a user would want to see from a search result (say a persons name, address phone number etc)

Currently my SearchService looks like:

public interface SearchService {
    public List<People> searchPeopleReturnEntity(SearchRequest request);
    public List<Long> searchPeopleReturnId(SearchRequest request);
    public List<SearchResult> searchPeopleReturnSearchResult(SearchRequest request);
}

I'm looking for advice on best practices regarding this. Currently the naming convention seems pretty clunky and I believe there is a better solution than what I have now.

like image 259
BuffaloBuffalo Avatar asked Oct 15 '25 15:10

BuffaloBuffalo


1 Answers

I'd call them something simple like getPeople, getIds, getSearchResults.

If you need these same 3 methods for entities other than people, I'd consider making some generic intermediate type defining them, allowing you to write something like this:

List<People> people = service.getPeople(request).asEntities();
List<Long> fooIds = service.getFoos(request).asIds();

// or something like this
List<People> people = service.searchPeople().getEntities(request);
like image 169
ColinD Avatar answered Oct 18 '25 05:10

ColinD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!