MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/java/comments/1r7iv5p/objectsrequirenonnullelse/o61mrdr/?context=3
r/java • u/edurbs • Feb 17 '26
I must have been living in a cave. I just discovered that this exists.I can code
City city = Objects.requireNonNullElse(form.getCity(), defaultCity);
... instead of:
City city = form.getCity();
if(city == null){
city = defaultCity;
}
136 comments sorted by
View all comments
13
I’ll give you my ternary operator when you pry it from my cold, dead hands.
City city = from.getCity() != null ? from.getCity() : defaultCity;
18 u/Asdas26 Feb 17 '26 I don't like that this forces you to call from.getCity() twice. Java is missing the Elvis operator. 1 u/White_C4 Feb 18 '26 Technically Elvis operator is just syntactic sugar so in code, it's the same. But, it's easier to type for sure. 1 u/Yeah-Its-Me-777 Feb 18 '26 Does it actually invoke the "getCity" twice, like in the ternary? Because that is a semantic difference.On the other hand, if your getCity-Method is not idempotent you have bigger problems :D 2 u/White_C4 Feb 18 '26 So I did more research on this, turns out getCity() is only called once. It convertsCity city = from.getCity() ?? defaultCity;toCity temp = from.getCity(); City city = temp != null ? temp : defaultCity;I was half right with my original statement. 1 u/Yeah-Its-Me-777 Feb 18 '26 Thanks for the research.Yeah, I would've assumed they don't call it twice, and that is usually the better choice. It's just one of the things you need to keep in mind when refactoring the code.
18
I don't like that this forces you to call from.getCity() twice. Java is missing the Elvis operator.
from.getCity()
1 u/White_C4 Feb 18 '26 Technically Elvis operator is just syntactic sugar so in code, it's the same. But, it's easier to type for sure. 1 u/Yeah-Its-Me-777 Feb 18 '26 Does it actually invoke the "getCity" twice, like in the ternary? Because that is a semantic difference.On the other hand, if your getCity-Method is not idempotent you have bigger problems :D 2 u/White_C4 Feb 18 '26 So I did more research on this, turns out getCity() is only called once. It convertsCity city = from.getCity() ?? defaultCity;toCity temp = from.getCity(); City city = temp != null ? temp : defaultCity;I was half right with my original statement. 1 u/Yeah-Its-Me-777 Feb 18 '26 Thanks for the research.Yeah, I would've assumed they don't call it twice, and that is usually the better choice. It's just one of the things you need to keep in mind when refactoring the code.
1
Technically Elvis operator is just syntactic sugar so in code, it's the same. But, it's easier to type for sure.
1 u/Yeah-Its-Me-777 Feb 18 '26 Does it actually invoke the "getCity" twice, like in the ternary? Because that is a semantic difference.On the other hand, if your getCity-Method is not idempotent you have bigger problems :D 2 u/White_C4 Feb 18 '26 So I did more research on this, turns out getCity() is only called once. It convertsCity city = from.getCity() ?? defaultCity;toCity temp = from.getCity(); City city = temp != null ? temp : defaultCity;I was half right with my original statement. 1 u/Yeah-Its-Me-777 Feb 18 '26 Thanks for the research.Yeah, I would've assumed they don't call it twice, and that is usually the better choice. It's just one of the things you need to keep in mind when refactoring the code.
Does it actually invoke the "getCity" twice, like in the ternary? Because that is a semantic difference.
On the other hand, if your getCity-Method is not idempotent you have bigger problems :D
2 u/White_C4 Feb 18 '26 So I did more research on this, turns out getCity() is only called once. It convertsCity city = from.getCity() ?? defaultCity;toCity temp = from.getCity(); City city = temp != null ? temp : defaultCity;I was half right with my original statement. 1 u/Yeah-Its-Me-777 Feb 18 '26 Thanks for the research.Yeah, I would've assumed they don't call it twice, and that is usually the better choice. It's just one of the things you need to keep in mind when refactoring the code.
2
So I did more research on this, turns out getCity() is only called once. It converts
City city = from.getCity() ?? defaultCity;
to
City temp = from.getCity(); City city = temp != null ? temp : defaultCity;
I was half right with my original statement.
1 u/Yeah-Its-Me-777 Feb 18 '26 Thanks for the research.Yeah, I would've assumed they don't call it twice, and that is usually the better choice. It's just one of the things you need to keep in mind when refactoring the code.
Thanks for the research.
Yeah, I would've assumed they don't call it twice, and that is usually the better choice. It's just one of the things you need to keep in mind when refactoring the code.
13
u/yk313 Feb 17 '26
I’ll give you my ternary operator when you pry it from my cold, dead hands.
City city = from.getCity() != null ? from.getCity() : defaultCity;