Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe (and better) to multiply with boolean?

I have a piece of code that has a lot of if and else if. And I just thought now that in multiplication, true evaluates to 1 and false evaluates to 0. Would it be safe and easier to read (because it's shorter) to substitute :

if(!this._isFetched('studentInfoFetched')) { 
        tempAddedTime += 1;
        estimatedTimePerStudent += 0.04 + 0.2;
}
if(formInputValues.student_expiration){
        tempAddedTime += (!this._isFetched('studentExpirationFetched'))? 14 : 0;
        estimatedTimePerStudent += 1; 
}  

for :

    tempAddedTime += 1 * (!this._isFetched('studentInfoFetched')) + 14 * (!this._isFetched('studentExpirationFetched')) * (formInputValues.student_expiration);
    estimatedTimePerStudent += 0.24 * (!this._isFetched('studentInfoFetched')) + 1 * (formInputValues.student_expiration);

Note :_isFetched returns a bool. And this is just an example, for other cases I have a lot more if's so it would be saving me more lines.

like image 343
Anthony N. Avatar asked Nov 04 '15 20:11

Anthony N.


People also ask

Can you multiply booleans?

Multiplication is valid in Boolean algebra, and thankfully it is the same as in real-number algebra: anything multiplied by 0 is 0, and anything multiplied by 1 remains unchanged: This set of equations should also look familiar to you: it is the same pattern found in the truth table for an AND gate.

What is the point of booleans?

Boolean Operators are simple words (AND, OR, NOT or AND NOT) used as conjunctions to combine or exclude keywords in a search, resulting in more focused and productive results. This should save time and effort by eliminating inappropriate hits that must be scanned before discarding.

Can you multiply Boolean in C++?

C++ Multiplication of two BooleansYou can multiply two boolean values using multiplication operator. The operator converts false to 0, true to 1 and then performs multiplication operation.

Can you multiply bool and int in C++?

It's perfectly fine according to the standard (§4.5/6): A prvalue of type bool can be converted to a prvalue of type int , with false becoming zero and true becoming one. I like the branch-free approach of multiplying by !


2 Answers

No, the ifs - version is better.

Reasons:

  • It's much more readable: expressions are shorter, and the lines are not too long. For example, I see a horizontal scrollbar on my screen for your multiplication expressions, while I don't have to scroll in the if-snippet :)

  • It's faster because you avoid the multiplication.

  • It's even faster because you avoid calling this._isFetched('studentInfoFetched') twice.

  • ifs semantically define program flow, while the multiplication is semantically a mathematical expression which is used to fake the program flow in this case. With ifs, statements are grouped by condition, and you see at a glance what happens if a certain condition is met.

Then, consider that you have to create two more if clauses. The multiplication would become totally unmaintainable.

like image 102
Alex Shesterov Avatar answered Oct 09 '22 10:10

Alex Shesterov


The code should be easy to read, quick to understand, and fast to change.

Better than comments that go out off time is clear variable names, although they're good for a general desctiption of why. Name constant (ie. what is 0.04 + 0.2??), and name expressions for brevity in context (also avoids unnecessary function calls).

// Estimate process time
const infoFetched = this._isFetched('studentInfoFetched')
const infoFetchTime = 0.04 + 0.2
const canExpire = formInputValues.student_expiration
const expirationFetched = this._isFetched('studentExpirationFetched')
const expirationFetchTime = 14

if (!infoFetched) tempAddedTime += 1
if (hasExpired && !expirationFetched) tempAddedTime += expirationFetchTime

if (!infoFetched) estimatedTimePerStudent += fetchTime
if (hasExpired) estimatedTimePerStudent += 1

I usually like to multiply booleans as toggles, although in this case, the if's might be a little bit easier to read, understand, and change;

tempAddedTime +=
    !infoFetched* 1 +
    (hasExpired && !expirationFetched)* expirationFetchTime

estimatedTimePerStudent +=
    !infoFetched* fetchTime +
    hasExpired* 1

Not the best example, would probably be quite different if I had access/knowledge about what it itended to do / the source

like image 25
Leonard Pauli Avatar answered Oct 09 '22 12:10

Leonard Pauli