Skip to content
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

Refactor process_handle_drop_internal() in bevy_asset #10920

Merged
merged 4 commits into from
Dec 12, 2023

Conversation

matiqo15
Copy link
Member

@matiqo15 matiqo15 commented Dec 9, 2023

Objective

Solution

  • Use early returns when possible.
  • Reduced from 9 levels of indents to 4.

@matiqo15 matiqo15 added A-Assets Load files from disk to use for things like images, models, and sounds C-Code-Quality A section of code that is hard to understand or change labels Dec 9, 2023
@matiqo15 matiqo15 requested a review from Nilirad December 9, 2023 14:35
Copy link
Contributor

@Nilirad Nilirad left a comment

Choose a reason for hiding this comment

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

Apart from a duplicated comment after the function, it looks good at a first glance. I don't have the time right now to examine it to the detail, since it involves a very complex control flow.

Next time, try to break down the PR in multiple commits like I did in #10911. It will take more time, but it will make the review much easier and reliable.

crates/bevy_asset/src/server/info.rs Outdated Show resolved Hide resolved
Comment on lines 581 to 605
if !watching_for_changes {
path_to_id.remove(&path);
return true;
}

for loader_dependency in info.loader_dependencies.keys() {
if let Some(dependants) = loader_dependants.get_mut(loader_dependency) {
dependants.remove(&path);
}
}

let Some(label) = path.label() else {
path_to_id.remove(&path);
return true;
};

let mut without_label = path.to_owned();
without_label.remove_label();

if let Entry::Occupied(mut entry) = living_labeled_assets.entry(without_label) {
entry.get_mut().remove(label);
if entry.get().is_empty() {
entry.remove();
}
};
Copy link
Contributor

Choose a reason for hiding this comment

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

This passage is too difficult to understand and duplicates the path_to_id.remove(&path) line, making it more obscure. It is more appropriate to extract it instead of inverting it. It is essentially a sort of subroutine anyway.

Basically you should revert the old if watching_for_changes block, and extract all the code inside it to a new function. Then you can invert the body of the new function as you usually do. If you use rust-analyzer there is a specific action to extract selected code into a function.

Once that's done, I think it's probably OK to go.

Copy link

@JustAnotherCodemonkey JustAnotherCodemonkey left a comment

Choose a reason for hiding this comment

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

I will say that all the individual instances of the if statement exit conditions (for example),

path_to_id.remove(&path);
return true;

is expressed multiple times which could make making certain changes dangerous as one could easily miss one of these, leading to bugs. Then again, this sort of thing is what testing is for.

It also feels like a "DNRY" (Do'Nt Repeat Yourself) thing although the only solution I can see is going back to what we had before which, as is the reason for the PR, has major readability issues. This bit specifically is the one thing that really bugs me personally but this is really the only issue I have with the code as it is.

Overall though, the code is, I do believe, identical in effect and it is extremely successful at reducing maximum indentation in an idiomatic way.

Copy link
Contributor

@Nilirad Nilirad left a comment

Choose a reason for hiding this comment

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

Looks OK to me. I would have avoided putting that amount of whitespace but it's not a critical issue, it just tends to inflate a little bit the function. Nice job anyway 👍🏻

@Nilirad Nilirad added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Dec 10, 2023
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Dec 12, 2023
Merged via the queue into bevyengine:main with commit a7a5d17 Dec 12, 2023
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Code-Quality A section of code that is hard to understand or change S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants