Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 534
froadie Avatar asked Mar 19 '10 14:03

froadie


People also ask

Why are Boolean variables used as flags?

Concept: Boolean Flags Boolean values are regularly used to help maintain the state of a given piece of code. It is common to describe boolean variables as “boolean flags” - these often are used to turn on and off different behaviors that might be useful.

What is a boolean variable in Python?

The Python Boolean type is one of Python's built-in data types. It's used to represent the truth value of an expression. For example, the expression 1 <= 2 is True , while the expression 0 == 1 is False . Understanding how Python Boolean values behave is important to programming well in Python.

Which is an example of the boolean variable type?

Boolean operator examples>= – True if a number is greater than or equal to another. <= – True if a number is less than or equal to another. == – True if two values are equivalent. !=

What is Boolean data type in C++?

A boolean data type is declared with the bool keyword and can only take the values true or false . When the value is returned, true = 1 and false = 0 .


2 Answers

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!-)

like image 151
Alex Martelli Avatar answered Sep 20 '22 14:09

Alex Martelli


I have used it, though normally wrapping boolean logic into a reusable method (if called from multiple locations).

It helps readability and when the logic changes, it only needs changing in one place.

Other will understand it and will not find it strange (except for those who only ever write thousand line functions, that is).

like image 22
Oded Avatar answered Sep 24 '22 14:09

Oded