It assumes that user has type String, although for fair comparison it should have type Optional<String>
You just stepping through object properties (BTW, your code might be a code smell, because it knows too much about User internals). If you try to actually compose operations, your code will not look so short.
You may try to rewrite code below with optional chaining:
public UserProfileResponse userProfileRequestHandler(User.Id userId) {
User user = userService.findById(userId);
if (user == null) {
return UserProfileResponse.error(USER_NOT_FOUND);
}
UserProfileDetails details = userProfileService.findById(userId);
if (details == null) {
return UserProfileResponse.of(user, UserProfileDetails.defaultDetails());
}
return UserProfileResponse.of(user, details);
}
For comparison, monadic version of this code looks so:
Your code is shorted for two reasons:
user
has typeString
, although for fair comparison it should have typeOptional<String>
User
internals). If you try to actually compose operations, your code will not look so short.You may try to rewrite code below with optional chaining:
For comparison, monadic version of this code looks so:
Just assumed User/Address were kt data classes / java POJO but Indeed callee could've been returned a Optional