The Array object has always had some oddities. Methods like sort, reverse, and splice change the array in place. Other methods like concat, map, and filter create a copy of the array and then operate on the copy.
The real sin of sort, reverse and splice is that they change the array in place and also return the original array. If myArray.sort() returned null or undefined then it would be much more obvious that .sort() was operating in-place.
If .sort() and friends didn’t return an array then console.log(reversed) would print null or undefined and any further attempts to operate on reversed would generate an error.
That said, these changes are welcome, though I’m not sold on the need for .with. Aren’t these equivalent?
Agreed. Making it obvious how it behaves would save a lot of trouble. At Sonar we have a JavaScript rule to enforce that you don’t do that because it is simply misleading. I have a pull request in to update the wording of that to suggest using toSorted/toReversed too.
Sadly, since it’s hard to make backwards incompatible changes to JS, we’re likely stuck with sort and reverse’s behaviour, but hopefully we can move forward with these new methods, and the Tuple/Record proposal for immutable objects too.
In Python, list methods that mutate the list return None, specifically for this reason. When I jump from Typescript to Python, it can sometimes feel clunky when I can’t just chain a few of these methods, but when I jump from Python to Typescript, I have to actually spend mental effort confirming that a given method mutates or not. When reading, Python’s way makes it obvious it must mutate, and the JS way I just assume it’s immutable if the return value is used. It’s a minor problem, but it’s there.
I’ve never been able to get my brain to read Ruby’s ! and ? methods properly, but that use does sound great. It feels like one of those things that I’d be routinely ranting about other languages not having if I was a Ruby dev.
uBlock Origin rule to hide that distracting, useless-because-technically-literate-readers-already-know-how-to-use-‘copy’ social sidebar: www.sonarsource.com##h4:has-text("SHARE"):upward(div).
The real sin of
sort
,reverse
andsplice
is that they change the array in place and also return the original array. IfmyArray.sort()
returnednull
orundefined
then it would be much more obvious that.sort()
was operating in-place.If
.sort()
and friends didn’t return an array thenconsole.log(reversed)
would printnull
orundefined
and any further attempts to operate onreversed
would generate an error.That said, these changes are welcome, though I’m not sold on the need for
.with
. Aren’t these equivalent?copyAndUpdate1
is the array, whereascopyAndUpdate2
is the string'New Value'
.Oh, duh, you’re right
Agreed. Making it obvious how it behaves would save a lot of trouble. At Sonar we have a JavaScript rule to enforce that you don’t do that because it is simply misleading. I have a pull request in to update the wording of that to suggest using
toSorted
/toReversed
too.Sadly, since it’s hard to make backwards incompatible changes to JS, we’re likely stuck with
sort
andreverse
’s behaviour, but hopefully we can move forward with these new methods, and the Tuple/Record proposal for immutable objects too.I’m with you on the mutate-and-return problem.
In Python, list methods that mutate the list return None, specifically for this reason. When I jump from Typescript to Python, it can sometimes feel clunky when I can’t just chain a few of these methods, but when I jump from Python to Typescript, I have to actually spend mental effort confirming that a given method mutates or not. When reading, Python’s way makes it obvious it must mutate, and the JS way I just assume it’s immutable if the return value is used. It’s a minor problem, but it’s there.
Ruby uses
.sort
for copy-sort and.sort!
for sort-in-place, which is a convention I think more languages should do.Ruby has definitely spoiled me. I love being able to have ! and ? at the end of method names.
I’ve never been able to get my brain to read Ruby’s ! and ? methods properly, but that use does sound great. It feels like one of those things that I’d be routinely ranting about other languages not having if I was a Ruby dev.
Been following these for a few years, great to see them coming in. Doesn’t look like firefox support is in yet tho :( wonder when that’s coming.
Looks like Firefox have built support for it and just require it to be unflagged. Hopefully that’s soon!
uBlock Origin rule to hide that distracting, useless-because-technically-literate-readers-already-know-how-to-use-‘copy’ social sidebar:
www.sonarsource.com##h4:has-text("SHARE"):upward(div)
.