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

I have a piece of code in which there is a lot of if and else if. And now I just thought that when multiplying, the truth evaluates to 1, and false evaluates to 0. It would be safer and easier to read (because it is shorter) to replace:

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 bool. And this is just an example, for other cases I have much more if it would allow me to save more lines.

+5
source share
2 answers

No, if is a better version.

Causes:

  • This is more readable: the expressions are shorter and the lines are not too long. For example, I see a horizontal scroll bar on the screen for your multiplication expressions, while I don't need to scroll in if -snippet :)

  • This is faster because you avoid multiplication.

  • This is even faster because you call this._isFetched('studentInfoFetched') twice.

  • if semantically defines the program flow, while multiplication is semantically a mathematical expression that is used to fake the program flow in this case. With if s, the operators are grouped by condition, and you will immediately see what happens if a certain condition is met.

Then think that you need to create two more if . Multiplication would be completely unattainable.

+9
source

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

Better than time-bound comments are clear variable names, although they are good for a general description of the cause. Name constant (i.e. What is 0.04 + 0.2 ?) And 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 switches, although in this case, if may be a little easier to read, understand, and modify;

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

Not the best example is likely to be completely different if I had access / knowledge of what he did for this / source

0
source

Source: https://habr.com/ru/post/1235240/


All Articles