If You Don’t Understand Short Circuiting Your App Will Break

preview_player
Показать описание
🌎 Find Me Here:

#Shorts
Рекомендации по теме
Комментарии
Автор

Do not use short circuit just to ensure that a property exists. instead use optional chaining for that. It looks like this 'person?.name'

SirSidi
Автор

great explanation, thanks! another way i think of this is, the `&&` operator is _searching for truthiness_, so when it evaluates the first `false`, it immediately knows the whole expression is _not true_, thus terminating at that point and not reading the additional conditions. whereas with the first `true`, _that condition_ is true, but `&&` states that _both_ must be true for the entire expression to evaluate as true, thus it continues on to check the next condition.

zacharystudio
Автор

Performance tip: In a case/switch statement put the most likely options first, avoiding comparison to less likely values unless necessary.

mateoc
Автор

When I discovered this ages ago it became so useful to me, I used it within of statements but didn’t know you could do it like this

ReadieFur
Автор

I used the && a lot in React when I want to render a component conditionally.

Jean-ykeo
Автор

Short circuiting is also useful if you have 2 things to evaluate but one of those things takes more compute than the other. Do the light one first then it only does the expensive one as needed

JeremiahPayne
Автор

I would mention the difference between "&&" and "&" in this case, since later would also evaluate, even if the axiom does apply.

YamiSuzume
Автор

Great video Kyle - Short, specific and to the point. You should probably mention that while demonstrated with JS, the concept is quite universal - nearly all modern languages, including SQL, employs this shortcut (though some let you disable it with compiler switches). For object presence testing It's especially useful in languages where optional chaining is not a thing.

SmithRyanHE
Автор

You can use & one time instead && to check both sides.

aviv
Автор

Short circuiting is no longer necessary these days, in the LTS 14 version of NodeJS they released optional chaining, which is much better:

person?.name

And if you want a default value you can use the null coalescence operator

person?.name ?? "Unknown"

hadawardgz
Автор

This is also called lazy evaluation. An example of unexpected greedy evaluation is expressions within array literals:

let x = 0
[++x, x][1]
//returns 1 regardless if the index is 0 or 1.

This is because array literals evaluate and execute all expressions and functions within them, in the same order that the comma (", ") operator defines.

So be careful when you use array literals as a replacement for switch-case

Rudxain
Автор

The other important part is looking at what the return value is in each case. For both operators the return value may not be a boolean, it's the return value of the second statement in the event of a non short circuit

thegreatbambino
Автор

Note: Logical XOR (not bitwise XOR), can NEVER short circuit as both values need to be evaluated. C and C++ don't have a logical XOR operator built in due to this, as they've specified that a logical operator must be able to short circuit to exist, and should just be a function otherwise.

brazzmo
Автор

That is actually the default behaviour in most cases. The really interesting cases are the ones where it suddenly doesn't apply any more, e.g. in BASH scripts: command1 && command2 || command3 is _not_ a short-circuit, no matter how much it looks like one.

feynthefallen
Автор

Still rember the good old days when the order of boolean operation is undefined, depending on thr compiler, it could be LTR or RTL

NoProblem
Автор

It's also useful to run verifications in order of likelihood, putting the "deal breakers" first, and/or (pun intended) put the most expensive verifications later (so they are less likely to actually being calculated).

santiagomoebio
Автор

I didn't understand much of that (not a language I know yet) but I really like the presentation and editing. Look forward to the next video like this.

PhillStone
Автор

that’s why order in if-else conditions is important

Azserd
Автор

This concept is all over React Development... It's great to know. I remember seeing and understanding it logically, but I didn't understand why it was used so much. Great video!

busyrand
Автор

Thank you, it make me understand why the second statement after ampersand doesn't run, when 1st statement is false.

abdulhakim