Anthony N.
Anthony N.

Reputation: 295

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.

Upvotes: 7

Views: 9789

Answers (2)

Leonard Pauli
Leonard Pauli

Reputation: 2673

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

Upvotes: 1

Alex Shesterov
Alex Shesterov

Reputation: 27525

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.

Upvotes: 10

Related Questions