-
Notifications
You must be signed in to change notification settings - Fork 19
Alternative to toSpliced()
#80
Description
Sorry it's too late to add this issue, I commented that on the yesterday meeting. Though I didn't want to block this proposal in the meeting, I think we still have chance to think about the alternative.
The problems of splice
, toSpliced
There are many complaints about the old splice
method in the community, the main issues of it are:
- Bad name:
splice
is a very uncommon word, as word usage frequency from COCA (Corpus of Contemporary American English) , "splice" is at the position of 20171, and as this comment, even native speakers might not know the meaning of that word anymore. - Bad api: It's so hard to figure out what the code like
a.splice(1, 2, 3, 4, 5)
really do. - Combination of the bad name and api: the name of
splice
looks very likeslice
, so those who don't know the word well would guess it's a mutation version ofslice
, but it not followslice
api.
toSpliced()
inherit all these warts, and might worse because it return semantics differ from splice
, this means if you would see code like:
let originalSlice = a.slice(start, end)
let newArray = a.toSpliced(start, end - start, ...items)
Not easy to understand and maintain, and it's even have random bug when start
or end
are negative!
Alternative
Do not add toSpliced
but add:
a.sliceReplace(start, end, items)
a.withSliceReplaced(start, end, items)
which follow the convention of slice
, solve the problems.
Note, this could be a separate proposal, but if we had withSliceReplaced(start, end, items)
, it's no need to add a bad api like toSpliced
anymore.