Boolean Blindness — Code Smells Catalog Skip to content

Boolean Blindness

Lexical Abusers Names Code Smell Within Class

Does filter(true) mean take or drop? When a function operates on raw booleans, it destroys the information about what those values represent. The type system knows; the reader doesn't.

1 min read 1 source

Overview

In the Haskell community, there is a well-(un)known question about the filter function - does the filter predicate means to TAKE or to DROP (check example)? Boolean Blindness smell occurs in a situation in which a function or method that operates on bool-s destroys the information about what boolean represents. It would be much better to have an expressive equivalent of type boolean with appropriate names in these situations. For the filter function, it could be of type Keep defined as Keep = Drop | Take.

This smell is in the same family as Uncommunicative Names and Magic Numbers.

Problems

📖
Comprehensibility

Neither in real life one can answer just yes/no without ever confusing interlocutor to every single closed question that may possible.

Example

1data Bool = False | True
2filter :: (a -> Bool) -> [a] -> [a]
HASKELL

Refactoring

  • Introduce New Type

Sources

Browse All 56 Smells