Code Smell 23 - Instance Type Checking
Do you check who are you talking to?
Problems
Solutions
Avoid kind, isKindOf, instance, getClass(), typeOf, etc..
Don't use Reflection and Metaprogramming for Domain Objects.
Replace IFs with polymorphism.
Avoid checking for 'undefined'. Use complete objects, avoid nulls and setters, favor immutability and you will never have undefined and ifs.
Sample Code
Wrong
Right
Detection
Since type checking methods are well known it is very easy to set up a code policy checking the uses.
Tags
- Metaprogramming
Conclusion
Testing for a class type couples the objects with accidental decisions and violates bijection since no such control exists on real world. It is a smell our models are not good enough.
Relations
More Info
Credits
Photo by Remy Gieling on Unsplash
This article is part of the CodeSmell Series.
No Comments Yet