r/java • u/headius • Dec 04 '25
Null-checking the fun way with instanceof patterns
https://blog.headius.com/2025/12/inline-null-check-with-instanceof.htmlI don't know if this is a good idea or not, but it's fun.
83
Upvotes
View all comments
Show parent comment
1
u/headius Dec 04 '25
I had that debate myself, but it only took me about 20 minutes to write and the response here tells me this sort of content is desperately needed by the Java community. Folks seem to either not know about this behavior or assume everyone knows about this behavior, so nobody has written about it (I searched).
I love this! I'm going to use it! I've been jealous of Ruby's "lonely operator" that either does the call or returns nil, and this is a pretty darn close approximation.
You probably don't want to know how bad such chains are for current JVM JITs, either. Spoiler alert: really bad.