DEV Community

Discussion on: 9 Neat JavaScript Snippets For Algorithms And More

Collapse
 
nazareth profile image
َ

Self documenting code > neat shortcuts.

“It’s harder to read code than to write it.” — Joel Spolsky

I would say if you do end up writing this, you should most definitely include more comments to explain it.

return {
a: 'Whatever',
...(b && { c: 'ok'}),
};

Collapse
 
mostlyfocusedmike profile image
Mike Cronin

Ah man this one truly walks the line for me between readable advanced vs flashy over compensation. The main reason I included it is because it is literally like 10 characters long and if you know what short circuiting and spreading are, it makes total sense and you hopefully you go "ah, that's pretty clever." That was my exact reaction, personally anyway. I think the other reason I like it so much is that I just can't get over how clunky the if statement alternative is.

If the cost of using this is just ...(b && { c: 'ok'}), // optionally add this obj or some other little comment, no problem. I definitely think modern coding can use more comments. I honestly wonder if this optional pattern will go the way of destructuring, which was originally confusing but now an essential skill, or reduce which it seems most people decided wasn't worth the brevity and shouldn't be used.