1

I recently had to maintain a legacy project, the code was a mess with no coding pattern however one thing caught my attention, in some cases Boolean types were created in three different ways:

const bool = true; //conventional
const bool = 1;
const bool = !0;

I know that not having a coding pattern is a code-smell but I was wondered are there any benefits in using a practice different from the one commonly used? Any performance gain on some compiler or interpreter? I really was instigated with that. Would you like to know if anyone knows about it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
1fabiopereira
  • 542
  • 2
  • 4
  • 18

3 Answers3

3

You should always use the regular way to define a boolean, which is true and false (like already mentioned by Bathsheba).


In JavaScript, most values lead to truthy values in an if-condition. The exception to this rule are the following values (source):

  • false
  • 0 (zero)
  • "" (empty string)
  • null
  • undefined
  • NaN (a special Number value meaning Not-a-Number!)

Your example:

 const bool = true; // the regular way to define a boolean
 const bool = 1; // a Number, this is truthy in an if-condition, but still, it is just a Number
 const bool = !0; // negates a number -> it actually converts it to a boolean during the negation. So, it is actually the same as bool = false;

If you are programming in another language, there are not always Booleans around. If there is a boolean in the programming language you are writing, you should definitely be using it.

Some examples:

  1. In Java, there is the primitive datatype boolean (true and false) and there is a Boolean wrapper class, which also contains methods for conversion etc. (Boolean.TRUE and Boolean.FALSE). Other values can not be evaluated in an if-condition.
  2. In Perl, there are no boolean values. You can however use the number values 0 and 1 for a comparison. It is similar to JavaScript, as it also evaluates most values as truthy, except the following (source):
    • 0
    • '0'
    • undef
    • '' # Empty scalar
    • () # Empty list
    • ('')
Community
  • 1
  • 1
ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
2

Use true and false. Anything else is not as clear.

You write a line of code once. You will read it hundreds of times.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

This post relates specifically to C/C++, though is reflective of most languages.

The C++ standard states (§4.7/4):

If the source type is bool, the value false is converted to zero and the value true is converted to one.

The reason that they are all allowed is simply due to the way that the compiler converts the 'truthy' values into a single bit representation.

They all take up the same amount of space when run through a compiler. The reason for the convention is simply for readability - for this reason, not all 'compiler-valid' representations should be considered equally preferable. As mentioned in Google's C++ Style Guide:

Optimize for the reader, not the writer

Our codebase (and most individual components submitted to it) is expected to continue for quite some time. As a result, more time will be spent reading most of our code than writing it. We explicitly choose to optimize for the experience of our average software engineer reading, maintaining, and debugging code in our codebase rather than ease when writing said code.

In short, readability is everything.

Mike Roberts
  • 266
  • 1
  • 12