top of page
Writer's pictureKlemens Morbe

different styles when coding #3

Updated: Mar 12, 2021

it wasn't that long ago that i tried to convince others in using optionals over nulls or empty strings. today i can say that i am more matured and accept almost any style of error handling or flow. optional flow forks like a charm if you can chain the return values one by one and dont have deal with multiple variables. with nulls or empty strings you are more or less forced to use classic if checks. additionally one cannot know if a returned null value is valid or should lead to a termination of the program. as in the last few posts, lets see which of of the two styles you prefer.


style 1:

plotter.getComplexDto()
        .map(ComplexDto::getSimpleDto)
        .map(SimpleDto::getMinimalDtos)
        .stream().flatMap(List::stream)
        .filter(Objects::nonNull)
        .forEach(minimalDto -> System.out.println(minimalDto.getNumber()));

style 2:

final var complexDto = plotter.getComplexDto();

if (complexDto == null) {
    return;
}

final var minimalDtos = complexDto
        .getSimpleDto()
        .getMinimalDtos();

for (final var minimalDto : minimalDtos) {
    if (minimalDto != null) {
        System.out.println(minimalDto.getNumber());
    }
}

so which style are you, 1, 2 or even something else, tell me about it in the comments. 😄



Recent Posts

See All

Comments


bottom of page