Well, you summarized it very well: if you wanted to connect more freely by entering hasAddress(), why return Optional.
Reading what LoD says means there is "limited" knowledge of "closely related" units. Sounds like a gray color to me, but later on he also mentions the rule "Only one point". However, I agree with the comments on your question that the zero check (or isPresent()) is completely fine (heck, the real check for zero information does not technically need a dot; P).
If you want to really encapsulate more, you can completely remove getAddress()and suggest instead:
class User {
private Optional<Address> address;
boolean hasAddress() {
return address.isPresent();
}
void ifAddressPresent(Consumer<Address> then) {
address.ifPresent(then::accept);
}
void ifAddressPresent(Runnable then) {
address.ifPresent(address -> then.run());
}
void ifAddressPresentDoSomeSpecificAction() {
address.ifPresent(address -> {
});
}
}
, : /? / , . LoD KISS.
, , User. , // :
- , User
- (, UserDao, POJO? DAO ? , " " " "?)
- ...