Syntax for testing bool state

I suppose this could go down the rabbit's hole fast, but was curious:

If defining a global bool as true

bool production = true;

Then is it purely personal preference how to test between these two ways? I know there are other things to debate here, like curly braces..... this is just about testing the bool though.

if (production) {
	publishToGoogleSheets();
}

vs

if (production == true) {
	publishToGoogleSheets();
}

Thanks

So easy to test!!! Identical results.

of course they produce identical results. Its more of a question of which way is more accepted

DEpends on if my coffee is hot or cold.

I figured.

Consistency in a single program is what is important.

How are we all sposed to know that? :expressionless:

a7

I'm going to say that the result of a Comparison Operator is a bool. So, the two 'if()' statements are identical.

It is a personal thing.
I prefer the first one, but I would change the variable name. Production is not something that can be true or false.
Something like:

bool isRed;
bool has4Weels;
bool available;
bool busy;

I hate this:

bool production = true;
...
if (production) {
	publishToGoogleSheets();
}

If you want to do that, make it really clear what the logical state represents:

bool productionEnabled = true;
...
if (productionEnabled) {
	publishToGoogleSheets();
}

Or 'productionIsOn' or whatever fits the use case.

...and, the compiler will optimize them to the same machine code.

I use this. It is a result of my relentless attempt to reduce ink, besides "== true" being redundant.

You would never

if ((a > b) == true) {

I hope.

Further and possibly less defensible or even "wrong", I use

if (counter) {

where your boss may want

if (counter != 0) {

The "!" in

 if (!sunny) {

is easy to miss, nevertheless I do that, too, not

if (sunny != true)  {

and TBH I get why ppl

if (3 == wiseManCount) {

but I would never. Sry.

Turn up compiler warnings and need heed them…

a7

It all comes to whether or not you are going to share your code and whether or not you are collaborating on a project with other programmers.

Have you ever read your own code of a year ago?

yep and was thinking who on earth writes like this?

Since an if statement tests whether the condition evaluates to true, an == true seems redundant to me. We can keep adding == true until the cows come home.

In C/C++ is not a type bool.
If you find a "bool" it is potentially a typedef.
And bool might be "int == 0" vs "int != 0".

So, true for a bool is all what is not zero, false is value 0.
int i = 0;
bool b = false;
if (i)
if (b)
behaves the same.

Think about: bool is still an int but used just as "if 0 --> false", "if not 0 --> true",
the same as "if (i)" or "if (! i)"

Often, you can use any int value also as a bool. It is just checking the value for: "is it 0"
or "is it NOT zero" (this is what true and false mean).

Check again. It's 2022.

a7

Is this a new lanuage or 2 languages one of which has bool and one doesn't?

So to make it clear - bool is a type of its own in C++, it denotes a truth value that can only be true or false (other values will get promoted into a truth value based on the usual non zero is true rule )

You would not say if it’s raining is true, you would just say if it’s raining, so the == true is useless esp if you named you variable appropriately to make the code easy to understand and maintain. (The good news is that the compiler will get rid of == true anyway so ….)