cover-img

Code Smell 23 - Instance Type Checking

Trust your collaborators. Don't check who they are. Ask them to do instead.

7 November, 2022

0

0

0

Do you check who are you talking to?

TL;DR: Trust your collaborators. Don't check who they are. Ask them to do instead.

Problems

Metamodel interference

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

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

This article is part of the CodeSmell Series.

0

0

0

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.