DEV Community

Discussion on: Making Sense of Syntax

Collapse
 
pentacular profile image
pentacular

!!x is just a less readable version of x != false.

{...a, ...b }.c is just a less efficient version of b.c ll a.c.

Collapse
 
perpetual_education profile image
perpetual . education

It depends who you talk to! ;)

Collapse
 
juliang profile image
Julian Garamendy

hmmm

const a = { c: 42 }
const b = { c : 0 }

const x1 = !!{...a, ...b}.c // false
const x2 = !!{...b, ...a}.c // true
const x3 = b.c || a.c // 42
const x4 = a.c || b.c // 42
Collapse
 
pentacular profile image
pentacular

Fair point -- although to really answer this we need to understand what the original code is trying to achieve to see what the actual problem domain is.

b.c ?? a.c might be sufficient, depending on how you want to deal with nullish values.

('c' in b ? b.c : a.c) might also do.

But whatever you're trying to do, it's unlikely that merging two objects to look up a property is the most reasonable option. :)

Thread Thread
 
perpetual_education profile image
perpetual . education

we need to understand what the original code is trying to achieve

This is what the code should describe (not just to you - but to your average programmer)

Thread Thread
 
pentacular profile image
pentacular

I don't think that's a reasonable expectation.

The code will describe the what and the how, but I have yet to see a programming language which encapsulates the why of it.

If your argument is that it should contain comments which explain why this is being done, then I agree.

In which case, I guess your point is that the snippet provided is defective because it lacks this. :)