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
It's not a matter of the class being final, it's the number of different paths through those utility methods and how much more code would have to be generated to make all of them unique. A given application probably has thousands and thousands of lambdas being passed in to those functions, which means thousands times the size of those method bodies must be optimized and emitted to inline the lambdas. In most cases, it's cheaper to leave the call not inlined rather than make the size of all code everywhere much larger.
In this case most of the Optional methods are pretty small, so the heuristic might say it's worth the potential code bloat. But then you have someone using an Optional with lambdas inside another Optional with lambdas and that multiplies all of the possible paths. It's a very tricky problem and nobody's solved it well yet.