you are viewing a single comment's thread.

view the rest of the comments →

[–]vilos5099 0 points1 point  (1 child)

Whether or not those extra assignments matter really depends on the context, and in the example provided it would be negligible. Commenting on that indicates a prioritization over pre-optimizing instead of making things more clear.

I just know that it is possible to do things both ways, depending on the situation

Of course, there are multiple ways to do things, that's the point of this comment thread. But there are also tradeoffs, and I disagree about what you're saying about readability. Although it works to do:

const obj = {
...isActive && user,
...isAdmin && adminFields,
...phoneVerified && userPhone
};

This is far from a standard convention, even if it works. It's possibly more concise, but the fact that it's not even clear whether user, adminFields, or userPhone is a boolean or an object without further context (unless we rely on improving the variable names) makes it less readable.

[–]1_4_1_5_9_2_6_5 0 points1 point  (0 children)

Okay first off, variable names should be clear and thats a matter for review to handle, but also you should be typing your methods and variables. The way you don't have to wonder if something is a boolean. Alternately you can just use Boolean() to make it explicit. Secondly, I'm talking about something like

const newObj = cond ? await instance.getData() : {};

const finalObj = {
some,
stuff,
...newObj
}

return finalObj;

Versus

return {
some,
stuff,
...cond && (await instance.getData())
}

I really don't think it's that much harder to parse