I work on a team of Java programmers. One of my co-workers suggests from time-to-time that I do something like ‘just add a type field’ (usu. ‘String type’). Or code will be committed laden with ‘if (foo instanceof Foo){...} else if( foo instanceof Bar){...}‘.
Josh Bloch’s admonition that ‘tagged classes are a wan imitation of a proper class hierarchy’ notwithstanding, what is my one-line response to this sort of thing? And then how do I elaborate the concept more seriously?
It’s clear to me that – the context being Java – the type of Object under consideration is right in front of our collective faces – IOW: The word right after the ‘class’, ‘enum’ or ‘interface’, etc.
But aside from the difficult-to-demonstrate or quantify (on the spot) ‘it makes your code more complicated’, how do I say that ‘duck-typing in a (more or less) strongly-typed language is a stupid idea that suggests a much deeper design pathology?
When you say ‘duck typing in strongly-typed languages’ you actually mean ‘imitating (subtype) polymorphism in statically-typed languages’.
It’s not that bad when you have data objects (DTOs) that don’t contain any behaviour. When you do have a full-blown OO model (ask yourself if this is really the case) then you should use the polymorphism offered by the language where appropriate.