DEV Community


Discussion on: 17 Pro JavaScript tricks you didn't know

almostconverge profile image
Peter Ellis • Edited

The trouble with some of these is that while they may save a negligible amount of writing time, they certainly do increase reading time. Reason being, nobody reads character by character, it's closer to token by token.

A good example is your Removing duplicates snippet: you're replacing over 20 tokens (some of them loops and ifs, which are even more taxing to parse), with 5 simple ones. Brilliant! Best of all, it doesn't matter how skilled you are, the "pro" option is easier to read for everyone. And this is key.

Because if we look at the convert string/number and back snippet. num.toString() is two simple and verbose tokens in a not-at-all surprising sequence. You don't even have to know that num is a number to understand what's going on. Great! How about "" + num? Well, in terms of tokens it isn't any shorter. However it also includes a non-intuitive use of +. As you're not using the main effect of + (i.e. adding/concatenating) two things but a secondary effect (type coercion).

Same goes for the reverse direction, except there you also add another confounding construct: = +. Which will definitely have most people stop for a moment to re-parse it, because we very rarely put two separate operators after each other.

Some people say "ah, but once you get used to them, it becomes second nature and you can read it as fast as the noob versions". That is true, but note how it isn't actually faster: what you've achieved is that you spent a lot of time learning to read structures "noobs" can't read.

And that's my biggest criticism of lists like these. I don't think it was your intention but the noob/pro distinction creates a gate-keeping effect, where certain coding practices are followed not for any tangible improvement in code quality but to signal belonging to a group, in other words, they're a shibboleth.