cover-img

Code Smell 24 - Boolean Coercions

Booleans should be just True and False

11 November, 2022

2

2

0

Booleans should be just True and False

TL;DR: Don't do magic castings to boolean. You will regret it on a Friday night.

Problems

Hiding Errors

Accidental complexity coupled to one particular language.

Readability

Difficulty to hop among languages.

Solutions

1.

Be explicit.

2.

Work with booleans for boolean conditions. Not integers, not nulls, not strings, not lists. Just booleans.

Sample Code

Wrong

Right

Detection

This is a language feature. Some strict languages show warnings with this magic wizardry.

Tags

Coercions

Primitive

Conclusion

Some languages encourage doing some magic abbreviations and automatic castings. This is a source of errors and a Premature Optimization warning.
We should always be as explicit as possible.

Relations

More Info

It is not the language that makes programs appear simple. It is the programmer that make the language appear simple!

Robert Martin
This article is part of the Code Smell Series.

2

2

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.