Skip to content

Conversation

@theotchlx
Copy link
Contributor

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.

@theotchlx
Copy link
Contributor Author

theotchlx commented Nov 28, 2025

@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.

@olivier-dj
Copy link
Contributor

@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`
@theotchlx theotchlx force-pushed the refactor/control-flow-guards branch 2 times, most recently from 061c456 to 43ecc78 Compare December 6, 2025 02:57
@theotchlx
Copy link
Contributor Author

theotchlx commented Dec 6, 2025

Made the changes were it felt relevant.
Ready for review - I did my best to check that I did not affect business logic flow anywhere.

Feel free to point out where it doesn't fit.

@theotchlx theotchlx marked this pull request as ready for review December 6, 2025 03:00
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.
@theotchlx theotchlx force-pushed the refactor/control-flow-guards branch from 43ecc78 to 1ffb0c2 Compare December 6, 2025 16:29
@theotchlx
Copy link
Contributor Author

The first commit is diffs from running cargo fmt.
The next two commits are diffs from using the ? operator to simplify the code.
The following commits are diffs from using the guards pattern to simplify the code.

Copy link
Contributor

@olivier-dj olivier-dj left a 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?

Comment on lines 150 to 160
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,
});
Copy link
Contributor

@olivier-dj olivier-dj Dec 6, 2025

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)

Copy link
Contributor Author

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

Comment on lines 174 to 184
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);
}
Copy link
Contributor

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?

Copy link
Contributor Author

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.

};

let mut push = false;
if let Some(know_route_ref) =
Copy link
Contributor

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()

Copy link
Contributor Author

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)));
                        }

Comment on lines +186 to +187
let Some(ptr) = first_hop_ptr else { continue };
let Some((_, rts)) = first_hops_map.get_mut(&ptr) else {
Copy link
Contributor

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 ?

Copy link
Contributor Author

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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

complicated to replace those?

Copy link
Contributor Author

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..

@theotchlx theotchlx force-pushed the refactor/control-flow-guards branch from 1ffb0c2 to 1911d57 Compare December 26, 2025 22:17
@theotchlx theotchlx force-pushed the refactor/control-flow-guards branch from 1911d57 to 07b4058 Compare December 26, 2025 23:34
@theotchlx theotchlx force-pushed the refactor/control-flow-guards branch from dfdb087 to 554ae38 Compare December 28, 2025 19:10
@theotchlx
Copy link
Contributor Author

theotchlx commented Dec 29, 2025

I have run 3 benchmarks (just as in #18 (comment)) and get similar alternating results.

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.

2 participants