Wednesday, August 24, 2005

Why using Boolean to represent ternary states is a bad idea

The intention of my previous entry is not to advocate the usage of Boolean to represent ternary state but to document a usage I found while reading some open source code.

Use NULL to represent state is technically valid and common practice. Nullable column is often used in database design and many APIs return NULL.

However, people seem to really hate this idea. Why?

I believe the reason is that the usage violates the "least surprise" principle. As programmers, we are hardwired to think of boolean as binary state. The usage in this case, although valid, is against human psychology.

2 comments:

jay said...

I agree with you. Using NULL is common and useful as a quick fix.

However, I think some shy away from this because deciphering between a forgotten, and a specifically NULLed field can be an issue.

Anonymous said...

I had this exact problem when a programmer wanted me to take certain action based on a null condition.

Basically the problem turned into an argument, because nulls happen when not wanted too.

Java tiger and autoboxing may add a new dimension to this.