The case for Array#replace()

Overriding an array without intermediate variables

There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors. Therefore, if we can reduce the amount of time we spend naming purposeless variables, we will increase our productivity. In the context of working with JavaScript, the most common case when I run into needing an intermediate variable is when I need to operate on the array itself in the method chain.

Consider the following example:

Suppose the requirement has been added to ensure that all venues are unique. You could use Array#filter(), e.g.

My first objection to this approach is that routines such as eliminating duplicate records should be abstracted using utilities. Of course, you can implement your own utility for this purpose, e.g.

This slightly improves readability of the subject code:

Suppose that a new requirement has been added: Now the program must check for duplicate records, and in case there are duplicate records — log the duplicate records and raise an error.

For the purpose of finding the duplicate records, you can use a utility such as find-duplicates. However, the input of find-duplicates is the subject array. Therefore, we need an intermediate array.

There is no way around it — we need to interrupt the method chain and create an intermediate variable. This goes back to my original point: naming things is hard; we can increase our productivity if we reduce the amount of intermediate variables that we need to name.

Lets introduce a potential solution:

Array#replace()

Array#replace() method allows to operate on the array itself, i.e. we are able to obtain the entire array within a method chain and translate it or return a new value altogether, e.g.

This becomes useful anywhere when you require to operate on the entire array within a method chain, e.g. we can rewrite our original example using Array#replace without breaking an intermediate variable/ breaking the method chain:

Implementing strict find method

Another example use case would be to implement a find method that throws when it cannot resolve a value, e.g.

This is a common pattern in my code, both in terms of how I use Array#find and how I name/ rename variables depending on what the variable holds.

If we hadArray#replace I could abstract this logic using a utility such as:

Which would permit me to chain query and lookup methods without needing intermediate variables, e.g.

To find more use cases, simply pick any existing array utility and fit it into this example. Whether that is chunking, pairing or zipping — all the methods where input is the entire array will require that you break your existing method chain just to operate that method on the array.

Closing notes

I am unsure whether “replace” is the right name – the method name is open to the discussion. However, I am certain that this functionality enables abstraction that reduce code verbosity.

I ran into a need for this particular method multiple times. The last time I have started a Twitter survey:

The results weren’t favourable (26% vs 74%). Perhaps I didn’t provide enough context. The intent of this article is to build a use case and propose it to TC39.

Let me know your thoughts – How would you use Array#replace or how would you avoid using it?

Tech / Product Founder — building https://contra.com/