DEV Community

Discussion on: Code Smell 145 - Short Circuit Hack

Collapse
 
moopet profile image
Ben Sinclair

I'd use that for finding a deep value but not performing an action:

if (user?.foo?.bar) {
  user.login();
  // or...
  logUserIn(user);
  // or whatever
}
Enter fullscreen mode Exit fullscreen mode

Also, the implication in your example is that isValid() returns a user object when that's not obvious - it looks like it should be a boolean.

Collapse
 
feliciousity profile image
Felicia Ann Kelley

It has to be a boolean, boolean is how the data is first made.

Thread Thread
 
kentaro_tanaka_5b2893f1d1 profile image
Kentaro Tanaka

The condition essentially evaluates to a boolean value, determining whether the nested property user.foo.bar exists and is truthy.

I am also engineer. Can we discuss more details?