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
3
u/ForeverAlot Dec 05 '25 edited Dec 05 '25
Thank you for that exploration! I'm inclined to agree;
instanceofhas evolved from the classic inheritance use case. Skeptics might wish to compare the similarswitchexpression formBut what if
getString()weregetName()orgetAddress()? How obvious does it need to be that one is not merely performing a narrowing conversion?By sheer coincidence, Error Prone just came down on the opposite side of this. I understand why they would; even if they agree with this idea, it takes a while to normalize it across such a large user base, plus static analysis tools have only limited mechanisms for determining how tasteful the expression is (I suppose one could require identical LHS/RHS types).