6 ms·
This misses the author's point. In his example the bug you want to detect is that the referenced object in question is not a Customer (or a Foo in your example
by jbrechtel 15y ago
This misses the author's point. In his example the bug you want to detect is that the referenced object in question is not a Customer (or a Foo in your example).
In your else block you can't report anymore information than the NullPointerException in the author's original example reported.
If you still use an 'as' operator at the callsite then you've lost all useful information. If you do a cast then you'll at least get an exception with WHAT it was you were trying to cast.
- noblethrasher 15y agoI don’t think that I’ve missed the author's point; but any time I see an if statement and polymorphic code used near each other, I question whether or not the programmer understands OOP. His example is not exactly using polymorphic code, but he is basically dispatching based on the runtime type. We know that a method that accepts parameters that are reference types should check to see if they are null. Since instance methods cannot check their first parameter (the this pointer), we make it impossible for outside code to call instance methods. Now callers never have to worry about the runtime type of an object or whether or not it’s null. If you’re going to circumvent the type system with explicit casts then, of course, you’re going to get invalid cast exceptions, but that’s for the client code to deal with.