-
Notifications
You must be signed in to change notification settings - Fork 20
Alternative to toSpliced()
#80
Comments
these apis are too long |
For me, |
Subjective reasons without any argumentation:
We could say the same about any method that takes more than 2 argument and used not too often. You options does not looks better. The signature of
I check code base of many my projects - in most (~90%) cases, I know the number of arguments that should be removed in
In most cases, items are just some variables. I remember only some cases where it was required to add elements from a collection with various number of elements. In case of inserting just one value, I will be forced to wrap it to array brackets - why?
|
During the plenary we brought up 2 points for
For those reasons, we don't intend to drop |
Too many arguments could be the problem, but in this specific case, it's not just because of too many args, but the mixing of the position/length params and inserted items. Compare
It's too easy to say "well-known", u could say any ES5 era methods are well-known, but does people really could use Note, my suggestion is not inventing a new thing, but follow the real well-known method
I think this is a good argument. Which API is better, (start, end) or (start, length), this is a question. I'm neutral on that, but two very similar api choose different style is definitely a problem. For example, in the old days of JS, most people can never figure out the difference of Consider About calculation, u may need calc in either case. One important point is,
The reason is And array bracket Of coz, if the case is inserting just one value, it seems not very optimized. But I assume the common case of "inserting one value" is just replace one element, so instead of
I hope we could at least have some agreement about how we could discuss api design problem, thank you. |
splice's signature is not well-known at all; it's wildly complex. Even setting aside those who accidentally add the "p" and intend |
@ljharb if a such mehod is too complex for you and you confuse it with |
@hax you position makes sense, but I agree with @rricard from the comment above. This proposal just provide non-mutating equals of already existent methods, with the same naming changed by the pattern and signatures, so I think that |
@zloirock sure. but all of my experience working with engineers of all skill levels over my entire career tells me that "splice makes sense to me" is the extreme minority case. |
Awesome, so instead of any argumentation why it's so "horrific", you wrote something about the skill level... My argumentation above. And why do you think that my skill and experience level is worse than your? -) |
I think I'm not making myself clear, apologies. I'm saying that, while it's great that you find it clear, I have seen a great many people ranging from "brand new engineers" to "extreme expert engineers, much more qualified than myself" find the Thus, my experience there suggests that you are in a niche category of people who find it clear. That may not be accurate, of course, but it's what my experience suggests. |
Clear and reasonable. Anyway, my position above. |
Hi @hax Where I think using the const newData = getNewData();
return currentList.toSpliced(/* at: */ index, /* delete: */ 0, ...newData); vs const newData = getNewData();
return currentList.withSliceReplaced(/* start: */ index, /* end: */ index, ...newData); |
I very much dislike the splice method as well, and find its API to be very unintuitive. My issue with it is that it's a jack-of-all-trades (and it's poorly named, and it's unclear which arguments do what). You can use it to remove elements from the middle of an array, or to insert elements into the middle, or a combination of the two actions at the same time. I'd rather just have a separate function to do these two actions (and yes, I know there's performance concerns with making them separate functions, but if I was really worried about performance in the particular place I'm coding, I would use the in-place modification functions). It might be too late to really add an improved |
As #88 , |
Putting Array.prototype = Core-1 (non-mutating):
Core-2 (mutating):
Array-1 (non-mutating):
Array-2 (mutating):
TA:
|
So based on that nomenclature, we're discussing whether toSpliced is in "Core-1" vs "Array-1"? |
In #88 yes (maybe I should have posted my comment there instead). It was more to help me visualise @hax 's comment.
"has toSpliced but no splice" - My feeling is that there is a consistency here. TypedArrays have no methods that can impact "inconsistency between TypedArray and Array/Tuple" - there is some president for this. Only TypedArrays have So I don't think it's 100% clear what would be the most consistent API here. |
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: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.a.splice(1, 2, 3, 4, 5)
really do.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 fromsplice
, this means if you would see code like:Not easy to understand and maintain, and it's even have random bug when
start
orend
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 liketoSpliced
anymore.The text was updated successfully, but these errors were encountered: