1. 16
  1.  

  2. 5

    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.

    const languages = ["JavaScript", "TypeScript", "CoffeeScript"];
    const reversed = languages.reverse();
    console.log(reversed);
    

    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?

    const copyAndUpdate1 = original.with(2, 'New Value');
    const copyAndUpdate2 = [...original][2] = 'New Value';
    
    1. 2
      const copyAndUpdate1 = original.with(2, 'New Value');
      const copyAndUpdate2 = [...original][2] = 'New Value';
      

      copyAndUpdate1 is the array, whereas copyAndUpdate2 is the string 'New Value'.

      1. 1

        Oh, duh, you’re right

      2. 1

        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.

        1. 1

          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.

          1. 5

            Ruby uses .sort for copy-sort and .sort! for sort-in-place, which is a convention I think more languages should do.

            1. 1

              Ruby has definitely spoiled me. I love being able to have ! and ? at the end of method names.

              1. 1

                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.

          2. 3

            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.

            1. 4

              Looks like Firefox have built support for it and just require it to be unflagged. Hopefully that’s soon!

            2. 3

              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).