Today, talking with some colleagues of mine, we were discussing the usage of tuples. The specific problem was: we have an API returning a list of some kind of objects (let's say POJO instances)
public List<Pojo> getPojos()
we decided to use pagination in order to prevent enormous responses
public List<Pojo> getPojos(int page, int pageSize)
the next step was: can we return both the list and a boolean saying if more results are available preventing useless calls?
My approach was to consider the results as a pair of values
public Pair<List<Pojo>, Boolean> getPojos(int page, int pageSize)
Some colleagues say that this approach can hide the meaning of the returned variables because it is not explicit what the boolean means, their proposal was to introduce an "ad hoc" object to handle the response
class PojoResponse{
List<Pojo> result;
Boolean hasMore;
}
public PojoResponse getPojos(int page, int pageSize)
In my opinion, this solution breaks the uniformity of the APIs, introduces new classes that should be maintained and, in my opinion, hides the real objective of the method, in this case, return a list of POJOs.
What is your opinion on that problem? Find articles on these "style problems" on the internet is not always so easy.
PagedResponse<T> { List<T>, bool }
is another alternative. It might be more useful to return the total number of available results rather than just a bool. If you're designing a HTTP Api, you could put something like that in theX-Total-Count
header. In fact I like what this guy does, something likePagedResponse<T> { T data; Paganation paging: { int total; int page etc } }
.