Daily tip #4
When you are building a method that returns a collection, never return null. Instead, return an empty collection.
var cars = carService.withNullOption();
//need null test before do anything
System.out.println("printing null option");
if(cars != null){
// if you don't do this you will get
// Cannot invoke "java.util.List.forEach(java.util.function.Consumer)" because "cars" is null
cars.forEach(System.out::println);
}
cars = carService.withEmptyOption();
System.out.println("printing empty option");
cars.forEach(System.out::println);
cars = carService.withValues();
System.out.println("printing values option");
cars.forEach(System.out::println);
Your coworkers will write less if/else, thanks to you.
Take a look in repo-tip-4 to see a real example.