Code Smell 23 - Instance Type Checking

Subscribe to my newsletter and never miss my upcoming articles

Do you check who are you talking to?

Problems

Solutions

  1. Avoid kind, isKindOf, instance, getClass(), typeOf, etc..

  2. Don't use Reflection and Metaprogramming for Domain Objects.

  3. Replace IFs with polymorphism.

  4. Avoid checking for 'undefined'. Use complete objects, avoid nulls and setters, favor immutability and you will never have undefined and ifs.

Sample Code

Wrong

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