Monday, April 6, 2020

spreader is better

At work I joined a book club for Simplifying JavaScript: Writing Modern JavaScript with ES5, ES6, and Beyond - mostly it was fullstack engineers but the review was good for me... Like in last month's case, we had a BIG list like

const filters = [
'filter1',
'filter2',
'filter3',
'filter4',
'filter5',
];

and I wanted to conditionally add a filter in the middle (one that we were testing out)...

const filters = [
'filter1',
'filter2',
'filter3',
...(isTestRunning()?['filterX']:[]),
'filter4',
'filter5',
];

The spread operator is such an interesting answer to an asymmetry that has long bothered me in most languages with a C-like syntax - that, classically, a function can take in many named arguments, but only returns one thing. Obviously you can work around that by returning some kind of compound object, but the spread operator is really a conceptually interesting way of opening up a kind of a parallel pipeline for inputs and outputs.

(I would also give Javascript credit for really making JSON work... I mean it's a little stupid that all  valid JS isn't valid JSON but still, the elegance of the syntax, the fact that SO much in programming life is just sets (ordered or no) and maps, and that if you make those two things really concise, you can make an endrun around more nerdy, controlling syntaxes based in XML, with all those pointy tags.)

No comments:

Post a Comment