8000 kernel/collections/queue: rename `remove_first` to `remove_first_matching` by lschuermann · Pull Request #4033 · tock/tock · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

kernel/collections/queue: rename remove_first to remove_first_matching #4033

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

Merged

Conversation

lschuermann
Copy link
Member

Pull Request Overview

Queue's dequeue_specific was renamed to remove_first in ab293c5 ("Rename dequeue_specific to remove_first").

While I don't disagree with the fact that dequeue_specific is an oxymoron and a bad name, arguably remove_first carries its own baggage. We're talking about collections where "first" has meaning, often used interchangably with "head". Looking over the code in #3577 I was quite surprised with the complexity of this method's implementation in RingBuffer, for what I thought would just be a dequeue operation---removing the first / head element.

This is an effort to clear up that confusion. This is not a hill worth dying on for me, so if anyone has strong feelings otherwise, feel free to close this.

Testing Strategy

N/A

TODO or Help Wanted

N/A

Documentation Updated

  • Updated the relevant files in /docs, or no updates are required.

Formatting

  • Ran make prepush.

…hing`

`Queue`'s `dequeue_specific` was renamed to `remove_first` in
ab293c5 ("Rename dequeue_specific to remove_first").

While I don't disagree with the fact that `dequeue_specific` is an
oxymoron and a bad name, arguably `remove_first` carries its own
baggage. We're talking about collections where "first" has meaning,
often used interchangably with "head". Looking over the code in tock#3577
I was quite surprised with the complexity of this method's
implementation in `RingBuffer`, for what I thought would just be a
`dequeue` operation---removing the _first_ / _head_ element.

This is an effort to clear up that confusion. This is not a hill worth
dying on for me, so if anyone has strong feelings otherwise, feel free
to close this.
@lschuermann lschuermann requested a review from alevy June 17, 2024 15:04
Copy link
Contributor
@bradjc bradjc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait dequeue_specific() changed name? Why does "to dequeue" have to mean the front of the queue?

I think the fix is to go back to dequeue_specific().

remove_first is a general operation for list-like structures and better follows conventions around naming such operations.

But this isn't a list-like structure, it is a queue.

@bradjc
Copy link
Contributor
bradjc commented Jun 17, 2024

Also I agree with Leon that remove_first() is a synonym for dequeue().

@lschuermann
Copy link
Member Author

I guess dequeue_specific doesn't mandate that it's the first element that's dequeued / removed, which is where remove_first is more specific. But otherwise I agree @bradjc.

@alevy
Copy link
Member
alevy commented Jun 17, 2024

I think remove_first where the argument is a lambda that takes an element and returns a bool, pretty clearly matches the implementation, which removes the first element that matches the predicate.

But I don't care very much.

@lschuermann lschuermann added the P-Upkeep This a relatively minor change, or one that is limited in scope, and requires less scrutiny. label Jun 17, 2024
@bradjc
Copy link
Contributor
bradjc commented Jun 18, 2024

dequeue_first_matching()?

@alevy
Copy link
Member
alevy commented Jun 18, 2024

remove_first_matching()

Again, I'm not dying on this hill, but dequeue means to remove the head of a queue.

Copy link
Member
@ppannuto ppannuto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove_first_matching feels like a winner to me.

@bradjc
Copy link
Contributor
bradjc commented Jun 19, 2024

Why do we have both verbs? Remove and Dequeue?

@alevy
Copy link
Member
alevy commented Jun 19, 2024

Why do we have both verbs? Remove and Dequeue?

Because they mean different things.

To refer to your original comments:

Why does "to dequeue" have to mean the front of the queue?

Because that is literally the definition of a queue (https://en.wikipedia.org/wiki/Queue_(abstract_data_type))

But this isn't a list-like structure, it is a queue.

Exactly. This method is entirely breaking the abstraction that this thing is a queue (a queue can only have the head removed), and exposing the fact that it is actually a list-like structure, than has an order of elements that can be removed otherwise.

Arguably, if we were interested in having more formalism here, we might have this structure and a number of traits:

trait Queue<T> {
    fn enqueue(&self, item: T);
    fn dequeue(&self) -> Option<T>;
}

trait ListLike<T> {
    fn remove_first_matching(...) -> Option<T>;
    fn remove_at(&self, i: usize) -> Option<T>;
    fn insert(&self, i: usize, item: T);
    ...
}

And this structure implements all of them.

But we probably don't need to have a bunch of different traits for just one data structure, so we can just put those methods in the impl directly, and name them based on the kind of interface they relate to.

@alevy alevy added this pull request to the merge queue Jun 19, 2024
Merged via the queue into tock:master with commit 0dcb702 Jun 19, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kernel P-Upkeep This a relatively minor change, or one that is limited in scope, and requires less scrutiny.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0