← Back to context

Comment by AdieuToLogic

24 days ago

> I've never heard of that being banned. It hasn't been banned anywhere i've written Java.

Once code such as the below is rejected in a code review, the next version often submitted is an "if-else-if" ladder using `instanceof` (which is effectively the same thing).

  TheReturnType r = null;

  try {
    DomainType1 instance = (DomainType1)obj;

    r = ...
    }
    catch (ClassCastException ex) {}

  if (r == null)
    try {
      DomainType2 instance = (DomainType2)obj;

      r = ...
      }
      catch (ClassCastException ex) {}

  if (r == null)
    try {
      DomainType3 instance = (DomainType3)obj;

      r = ...
      }
      catch (ClassCastException ex) {}

  ...

And yes, I have seen the above scenario played out in a professional setting.