-
-
Notifications
You must be signed in to change notification settings - Fork 3k
ArrayList: embrace the word "reserve" #22137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Follows the convention that a single word used to describe a function variant is desired if possible.
Matches the same word used for the corresponding append functions.
This word is shorter and has a more accurate definition.
I like the change. |
Would it make sense to always push "Reserved" to the end of the name? and also not abolish "ensure"? |
I also like the idea of calling the function that may no-op try list.ensureReserved(gpa, 1);
try list.ensureReserved(gpa, 1); I think it plays well with the usage of 'ensure' in |
But |
Whoops, didn't read the actual diff. I don't see how In my mind, 'reserved' space is talking about only the unused elements, so maybe I would just need to adjust how I view it. This means the semantics of |
I find the naming introduced in this PR less clear. To me, a call like |
My color of the bike shed (assuming ArrayListUnmanaged is the way):
Expanding:
Call site: list.push("foo").?;
litt.push("bar") orelse {
log.warn("dropping an item, not enough capacity", .{});
return;
}; EDIT: as applied to TigerBeetle codebase: tigerbeetle/tigerbeetle#2990 |
Ooh, that's really quite an interesting idea. My concern with those names would be that they would confuse people by making the capacity assumption unclear... but perhaps the Actually, I do have a concern with that. Beginners do often have a habit of discarding return values they don't understand, without reading docs. If they do that here, their ArrayList will just silently remain empty, leading to confusion. That... doesn't seem like a great experience for a first-time user, even if we could freely say "well, they misused the API!". Thinking about that further... I think having the caller trigger the IB is a worse API. I can't think of many (any?) realistic cases where the I almost think your idea would be better if the assertion were kept inside |
Renames "AssumeCapacity" to "Reserved", following the convention that a single word used to describe a function variant is desired if possible. Similarly, renames "ensure" to "reserve", so that the same word appears on both ends of those fundamentally connected functions.
⬇️
I generally like this, since I think it's a slimmer and more coherent API but I can think of one reason not to do this, which is that "ensure unused capacity" succeeds in communicating that a secondary call is a no-op while "reserve unused" incorrectly implies that there is an additional reservation counter.
⬇️
I think this naming convention shines when combined with more complicated stuff, for example, the
ArrayHashMap
methodputAssumeCapacityNoClobber
would becomeputReservedNew
.