Should programmers use boolean variables to "document" their code?

boolean, conventions, self-documenting

Solution

Splitting an expression that's too nested and complicated into simpler sub-expressions assigned to local variables, then put together again, is quite a common and popular technique -- quite independently of whether the sub-expressions and/or the overall expression are boolean or of just about any other type. With well-chosen names, a tasteful decomposition of this kind can increase readability, and a good compiler should have no trouble generating code that's equivalent to the original, complicated expression.

Some languages that don't have the concept of "assignment" per se, such as Haskell, even introduce specialized constructs to let you use the "give a name to a subexpression" technique (the `where` clause in Haskell) -- seems to bespeak some popularity for the technique in question!-)

Problem

I'm reading McConell's Code Complete, and he discusses using boolean variables to document your code. For example, instead of: ``` if((elementIndex < 0) || (MAX_ELEMENTS < elementIndex) || (elementIndex == lastElementIndex)){ ... } ``` He suggests: ``` finished = ((elementIndex < 0) || (MAX_ELEMENTS < elementIndex)); repeatedEntry = (elementIndex == lastElementIndex); if(finished || repeatedEntry){ ... } ``` This strikes me as logical, good practice, and very self-documenting. However, I'm hesitant to start using this technique regularly as I've almost never come across it; and perhaps it would be confusing just by virtue of being rare. However, my experience is not very vast yet, so I'm interested in hearing programmers' opinion of this technique, and I'd be curious to know if anyone uses this technique regularly or has seen it often when reading code. Is this a worthwhile convention/style/technique to adopt? Will other programmers understand and appreciate it, or consider it strange?

Original source