Should programmers use boolean variables to "document" their code?
- by froadie
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?