DEV Community

Discussion on: Compact or Verbose Codestyle?

Collapse
 
homer1994 profile image
homer1994

Two, hands down, with dataObj variable inlined and the method called something more descriptive. One is too obscure. I've been doing JavaScript development for over a decade and every time I have to look up the bizarre JavaScript && and || return logic

Collapse
 
fpuffer profile image
Frank Puffer

I would not call it bizarre and it is not specific to Javascript. You can do this in lots of languages like Java, C++ and even Perl.

The fact that you can use && and || as control structures is a consequence of short circuit evaluation and that is something every programmer should understand.

I do agree that these operators have not been introduced for this purpose and that this style can be less readable.

Collapse
 
nomaed profile image
Nomæd

They are quite handy when you need to get a property nested in an object, like let foo = obj && obj.child && obj.child.foo || "", especially when a default fallback value is needed.
But when it's used as a condition, it's a bit annoying and IMO much less readable at first glance.