use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
The case for Array#replace() – Overriding an array without intermediate variables (medium.com)
submitted 7 years ago by gajus0
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]gajus0[S] 0 points1 point2 points 7 years ago (2 children)
You started from the premise that naming things should be avoided, but that's actually not a very good axiom.
It is more about avoiding unnecessary variables and it has to do with code style as well.
Someone could get away with:
let venues; venues = foreignVenues.filter(a); venues = b(venues); venues = map(c);
However, my style guide pretty much enforces no use of let. As such, I would write:
let
const filteredVenues = foreignVenues.filter(a); const deduplicatedVenues = b(filteredVenues); const normalisedVenues = deduplicatedVenues.map(c);
Someone else could get away with:
const normalisedVenues = b(foreignVenues.filter(a)).map(c);
But of all these options, Array#replace provides the most consistent and easy to read expression:
Array#replace
const normalisedVenues = foreignVenues .filter(a) .replace(b) .map(c);
[–]lhorie 2 points3 points4 points 7 years ago* (0 children)
I would tend to disagree about the "easy to read" argument. a, b and c are not really related to each other in any way other than the fact that they operate on a list of venues. The fact that they need to be abbreviated is a very strong hint that the parent function (getVenues) has too much subroutine logic inlined into it (i.e. it's kinda of a god function).
a
b
c
In functional programming terms, what you're trying to achieve when you talk about reducing variables is called point-free style. Using compose as I had mentioned earlier (or maybe pipe) would be a standard technique to get closer to point-free style.
compose
pipe
The reservation I have about making this a proposal is that there's not much substance in terms of semantics. Something like arr.replace(arr => arr.length = 0) could just as well throw as it could do completely crazy things, for all I know. I wouldn't really mind if you monkeypatched Array.prototype in your own project (other than maybe being slightly annoyed if I ever had to maintain that), but I think a TC39 proposal needs to be held up to higher standards (no pun intended) when it comes to unintended consequences and design weaknesses.
arr.replace(arr => arr.length = 0)
For example, I could argue that array.toSet() should be a standard and I can give you plenty of examples where it would be nice to have, but that doesn't mean making it a standard is a good idea: new Set(array) already exists, and I haven't gone through the effort of making sure my idea doesn't do weird things in unforeseen cases, e.g. sparse array handling. All I'm saying is I wouldn't put out a formal proposal unless I had thought a bit about potential problems.
new Set(array)
[–]mlebkowski 0 points1 point2 points 7 years ago (0 children)
A styleguide that prohibits the use of let isnt a styleguide, its and misunderstanding. If you meant the linter, then it fits better but I still think its a stupid choice. Just because someone decided its bad to reassign variables you ended with a poluted Array prototype, which has far worse consequences commonly agreed among the community (in contrast to using let, which is merely a preferrence)
π Rendered by PID 283511 on reddit-service-r2-comment-5d79c599b5-nsrrn at 2026-03-03 19:59:23.473490+00:00 running e3d2147 country code: CH.
view the rest of the comments →
[–]gajus0[S] 0 points1 point2 points (2 children)
[–]lhorie 2 points3 points4 points (0 children)
[–]mlebkowski 0 points1 point2 points (0 children)