Skip to content

Forbid usage of some complex Option:: methods. #5754

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fulmicoton
Copy link
Collaborator

No description provided.

.map_or(lower_bound, |current| current.max(lower_bound)),
);

self.start_timestamp = self.start_timestamp.max(Some(lower_bound));
Copy link
Contributor

Choose a reason for hiding this comment

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

i know this is correct, but i find relying on Ord of Option to not be something easy to read. If we're moving away from map_or (which i don't think we should ban), i prefer the more verbose "min" variant line 1540, or even better, a .map(max).unwrap_or(val) (you might not like that though)

rational: to me, this operation is "take the larger of the values, or lower_bound if start_timestamp is unset", and this is how the code used to read. With just max, you need some thinking to make sure this is correct, and there isn't really a human sentence to describe what it does. With .unwrap_or(bound).max(bound) we say "take the current value, or the bound if unset, and compare that to the bound", which is the same, but with a stranger wording

Copy link
Member

Choose a reason for hiding this comment

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

If we're optimizing for Paul's definition of readability, we should rename start_timestamp to start_timestamp_opt and then:

self.start_timestamp_opt = if let Some(start_timestamp) = start_timestamp_opt {
  Some(start_timestamp.max(lower_bound))
} else {
  Some(lower_bound)
}

Comment on lines +4 to +12
# These functions hurt readability (according to Paul)
"std::option::Option::is_some_and",
"std::option::Option::is_none_or",
"std::option::Option::xor",
# "std::option::Option::and_then",
# .map(..).unwrap_or(..) or let Some(..) else {..}
"std::option::Option::map_or",
# .map(..).unwrap_or_else(..) or let Some(..) else {..}
"std::option::Option::map_or_else",
Copy link
Contributor

Choose a reason for hiding this comment

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

i don't agree with this list (except probably for xor). i generally like functional-style operation chaining. Can other people weight in on what we should do? cc @guilload

Copy link
Member

@guilload guilload Apr 17, 2025

Choose a reason for hiding this comment

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

I don't think we should vote on this issue, because the U.S. western democracies show us that it does not work. Sorry, I'm feeling cheeky this morning.

I also generally like functional-style operation chaining, so I'm biased. However, I do agree that Rust is taking it too far sometimes.

I also don't think we should take this issue too seriously because once the conversation is about personal taste and preference, it's hard to have a productive exchange.

Generally, I'm fine with allowing none of the functional constructs, some of them, or all of them. In the first case, it might make the code more readable for some people. In the second, we try to strike a balance and code might be more enjoyable to write for people like Trinity and me. Finally, in the third case, it's just beneficial for Paul's brain plasticity ;)

As main contributors of Quickwit, I invite you two to have a face to face call to find a middle ground that works for both of you.

Copy link
Collaborator

Choose a reason for hiding this comment

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

For me they are all hard to read except is_some_and.

Generally map + unwrap_or is very readable to me (and also kinda standardized)

I think this is should not be about personal taste, but readability, e.g. is_some_and is readable to me, but I don't like it.

Copy link
Contributor

Choose a reason for hiding this comment

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

okay, so let's replace .map_or() and its variants with .map().unwrap_or() then

@fulmicoton-dd fulmicoton-dd force-pushed the forbid_option_complex_methods branch from a69ea8f to ca6263e Compare April 17, 2025 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants