Return Type With The Information of The Method

Posted on Posted in programming

When we need a list of values of a type Foo, it quickly comes to a type List<Foo>, which is a list of Foo and nothing more. What if different methods return different list of values that have the same type? Why won’t we create different types for lists of values? One possible step is to create special types for return values of methods.

Example: simple return type the list of values

For example, we have a class Person:

public class Person {
    String name;
    Integer age;
    Integer weight;
}

and a repository PersonRepository:

public interface PersonRepository {
    List<Person> findByAll();

    List<Person> findByAge(Integer age);

    List<Person> findByWeight(Integer weight);
}

All three methods have the return type List<Person>. What does it mean a list of values with type Person? We only know that methods return a list of persons.

For example, in a caller method, where the repository method is called, we know more about of the list. It can be a list of all persons or a list of persons with specific age. If the list leave the caller method, we do not know that information any more.

Example of a caller method:

public static void main(String[] args) {
    List<Person> personList = personRepository.findByAll();

    doSomethingWith(personList);
}

Method doSomethingWith has for an argument List<Person>. It does not know, if a list contains all persons or only persons with specific age or weight. If doSomethingWith does a generic operation, then it is ok. If doSomethingWith is a method which for an argument gets only a list of persons of specific age then we can enforce that fact with a type.

Example: return type with the information of the method

Let’s look another repository PersonRepository2:

public interface PersonRepository2 {
    ListPersonAll findByAll();

    ListPersonByAge findByAge(Integer age);

    ListPersonByWeight findByWeight(Integer weight);
}

Return type is not List<Person> any more. Each method returns a type with the information of the method. For example, the method findByAge returns the type which contains data age and list of persons.

Return type ListPersonByAge:

public class ListPersonByAge {
    Integer age;
    List<Person> personList;
}

We can create a method like the doSomethingWith from the previous example and we can create method which gets for argument only list of persons of specific age.

Example: a more complex method

public interface PersonRepository3 {
    ListPersonAll findByAll();

    PersonsByAge findByAge(Integer ageFrom, Integer ageTo);

    PersonsByWeight findByWeight(Integer weightFrom, Integer weightTo);
}

The repository has methods findByAge and findByWeight that returns lists of persons for the range of values. From that type we can get a list of persons for specific age or weight.

Return type PersonsByAge:

public class PersonsByAge {
    Map<Integer, List<Person>> personMap;
}

Conclusion

Return type with the information of the method is probably not the best design. When do you have a bad design, it could be that lists or maps of values are input values for methods. With other words, there could be a lot of different methods that have the same argument type with a different meaning.

Leave a Reply