Code Smell 24 - Boolean Coercions

Subscribe to my newsletter and never miss my upcoming articles

Booleans should be just True and false

Problems

  • Hiding Errors

  • Accidental complexity coupled to one particular language.

  • Readability

  • Difficulty to hop among languages.

  • IFs

Solutions

  1. Be explicit.

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

  3. Fail fast

Sample Code

Wrong

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 CodeSmell Series.

No Comments Yet