-
Notifications
You must be signed in to change notification settings - Fork 6
refactor: use guard pattern on Option #19
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: main
Are you sure you want to change the base?
Conversation
|
@olivier-dj what do you think about this use of guards? It fits the example you gave in issue #16 , removing one layer of indentation and checking each Option one after the other, and it doesn't seem to add confusion either. If that's right, I'll find more places that fit the pattern and update them. |
Looks perfect to me, I find it much more readable ! |
This commit is changes from running `cargo fmt`
061c456 to
43ecc78
Compare
|
Made the changes were it felt relevant. Feel free to point out where it doesn't fit. |
Propagate None using the question mark operator instead of with a conditional.
Using the question mark operator to simplify the code.
In this particular case, the new guard pattern makes the code easier to work with. Note that the code went from implicit returns to explicit return statements. The compiler actually throws an error, and clippy doesn't complain about needless_return.
Make the code cleaner by using the guard pattern.
43ecc78 to
1ffb0c2
Compare
|
The first commit is diffs from running |
olivier-dj
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tested a bit the changes with cargo bench & the examples?
src/contact_manager/seg.rs
Outdated
| let Some(tx_end) = self.get_tx_end(tx_start, bundle.size, free_seg.end) else { | ||
| continue; | ||
| }; | ||
| let delay = Self::get_delay(tx_end, &self.delay_intervals); | ||
| return Some(ContactManagerTxData { | ||
| tx_start, | ||
| tx_end, | ||
| delay, | ||
| expiration: free_seg.end, | ||
| arrival: tx_end + delay, | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm I am unsure this is better. For readability this is debatable, and does the compiler drop the extra control flow associated to the "continue" ? Just binary size concerns, this may add an additional goto statement. (I bet LLVM drops the overhead)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm alright, I've dropped the commit
src/contact_plan/from_asabr_lexer.rs
Outdated
| match Self::add_node( | ||
| node, | ||
| &mut nodes, | ||
| &mut max_node_in_in_nodes, | ||
| &mut known_node_ids, | ||
| &mut known_node_names, | ||
| ) { | ||
| Ok(_) => {} | ||
| Err(msg) => { | ||
| return Err(msg); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just Self::add_node(....)?; (with ?) should work no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right! Lol. I've edited the commit.
src/pathfinding/node_parenting.rs
Outdated
| }; | ||
|
|
||
| let mut push = false; | ||
| if let Some(know_route_ref) = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By replacing the push variable logic below by an inline function, we should be able to get right of this if Some()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed a commit that replaces
let mut push = false;
if let Some(know_route_ref) =
tree.by_destination[receiver.node.borrow().info.id as usize].clone()
{
let mut known_route = know_route_ref.borrow_mut();
if D::cmp(&route_proposition, &known_route) == Ordering::Less {
known_route.is_disabled = true;
push = true;
}
} else {
push = true;
}
if push {
let route_ref = Rc::new(RefCell::new(route_proposition));
tree.by_destination[receiver.node.borrow().info.id as usize] =
Some(route_ref.clone());
priority_queue.push(Reverse(DistanceWrapper::new(route_ref)));
}with
if tree.by_destination[receiver.node.borrow().info.id as usize]
.as_ref()
.map_or(true, |known_route_ref| {
let mut known_route = known_route_ref.borrow_mut();
if D::cmp(&route_proposition, &known_route) == Ordering::Less {
known_route.is_disabled = true;
true
} else {
false
}
})
{
let route_ref = Rc::new(RefCell::new(route_proposition));
tree.by_destination[receiver.node.borrow().info.id as usize] =
Some(route_ref.clone());
priority_queue.push(Reverse(DistanceWrapper::new(route_ref)));
}| let Some(ptr) = first_hop_ptr else { continue }; | ||
| let Some((_, rts)) = first_hops_map.get_mut(&ptr) else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe:
let (Some(a), Some(b)) = (oa, ob) else {
return;
};
What do you think of this pattern ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried:
let (Some(ptr), Some((_, rts))) = (first_hop_ptr, first_hops_map.get_mut(&ptr)) else { continue; }Would've been nice, however unfortunately ptr is out of scope in the right-hand side of the equal sign...
"cannot find value ptr in this scope" in .get_mut(&ptr)
| continue; | ||
| }; | ||
| rts.push(current_route.clone()); | ||
| } else if let Some(next_route) = route_borrowed.next_for_destination.get(&dest) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
complicated to replace those?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean place the continue on the right? Yeah it's not very consistent as-is, but cargo fmt places the continue on a newline, due to the character limit..
1ffb0c2 to
1911d57
Compare
1911d57 to
07b4058
Compare
dfdb087 to
554ae38
Compare
|
I have run 3 benchmarks (just as in #18 (comment)) and get similar alternating results. |
The guard pattern sometimes makes the code easier to work with.
Note on this first commit, that the code went from implicit returns to explicit return statements.
The compiler actually throws an error, and clippy doesn't complain about needless_return.
This may be a missing feature in the Rust compiler.