loading...

re: Making Sense of Syntax VIEW POST

TOP OF THREAD FULL DISCUSSION
re: !!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.
 

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
 

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. :)

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)

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. :)

Code of Conduct Report abuse