And then after some time it will become City city = defaultCity; Optional<City> optionalCity = getCity(); If (optionalCity != null & optionalCity.isPresent()) { city = optionalCity.get(); }
If you need to check if Optional<City> field is not null, you're kind of defeating the point of not having a default value for it. Just set it to Optional.empty() then only do a optionalCity.isPresent() check every time.
View all comments
Show parent comment
1
u/yk313 Feb 17 '26
Also, if you model getCity to carry the optionality information by returning Optional<City> then that makes it easy for the callers to: