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

IMUGyroscope: Remove unused parameter and extract branching behaviour #13020

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tygyh
Copy link
Contributor

@tygyh tygyh commented Aug 17, 2024

Draft goals:

  • Find good names for the extracted methods
  • Figure out if removing update was right or if it was kept around for a good reason.

@tygyh tygyh marked this pull request as draft August 17, 2024 20:35
@Pokechu22
Copy link
Contributor

This PR would probably be easier to understand if it and the commit were titled something like "IMUGyroscope: Remove unused parameter and extract branching behaviour", as otherwise it sounds like something that affects the entirety of the codebase. (In particular, note that the PR title is shown on https://dolphin-emu.org/download/ for dev builds.)

@@ -23,7 +23,7 @@ class IMUGyroscope : public ControlGroup

StateData GetRawState() const;
// Also updates the state by default
Copy link
Member

Choose a reason for hiding this comment

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

Is this comment still correct after this change? Since update was apparently never set (so it was always true) this should probably be something like "Also updates the state" (whatever this means.)

@tygyh tygyh changed the title Remove unused parameter and extract branching behaviour IMUGyroscope: Remove unused parameter and extract branching behaviour Aug 17, 2024
// Set calibration to zero.
m_calibration = {};
RestartCalibration();
return std::nullopt;
Copy link
Contributor

Choose a reason for hiding this comment

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

This function should not return a std::optional just to always return std::nullopt. If IMUGyroscope::GetState needs to return std::nullopt after calling this function, then do so there.

Copy link
Contributor

@mitaclaw mitaclaw Aug 18, 2024

Choose a reason for hiding this comment

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

Bonus tip: If doing so in the ternary you've changed the code to is absolutely necessary, you could still pull it off with the comma operator. I don't think the ternary operator is mission-critical in this case, though.

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've never used the comma operator before. Could you show how you want me to fix this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Against my better judgement, I will show you.

  return AreInputsBound() ? GetStateInternal() : (Reset(), std::nullopt);

However, I don't recommend writing code like that. The following is what I would write instead:

  if (AreInputsBound())
    return GetStateInternal();
  Reset();
  return std::nullopt;

It may seem attractive to use the one-liner with the fancy language feature, but it's less readable when it uses such obscurities.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It may seem attractive to use the one-liner with the fancy language feature

It is the most attractive thing since magnets, but since the goal is readability, I'll go with the verbose option. :)

@@ -23,7 +23,7 @@ class IMUGyroscope : public ControlGroup

StateData GetRawState() const;
// Also updates the state by default
std::optional<StateData> GetState(bool update = true);
std::optional<StateData> GetState();
Copy link
Contributor

Choose a reason for hiding this comment

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

If you come across some code that does something and you don't understand why / it seems meaningless, your should first do a git blame of said code's history to search for an explanation.

379ffc2

Pinging @Filoppi to ask if this code scaffolding is still necessary for ongoing or future work.

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 know, theoretically I still have a PR with many fixes and improvements that would make use of this, and ideally I'll finish it one day, but it's hard to guarantee.
Tbh I don't see the point in removing this flag, it's irrelevant for performance and doesn't really hurt. It feels like this is changing the code for the sake of it (at least for this specific change).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tbh I don't see the point in removing this flag, it's irrelevant for performance and doesn't really hurt.

I like code which justifies its existence, either for performance, feature or readability reasons. Not being harmful enough to remove is not a good reason to me.

It feels like this is changing the code for the sake of it

The reason for changing this is for readability. See Martin Fowler's article about flag arguments for more details

Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be good to make it an enum for sure

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 only use enums when there are more than two code paths.

What values should the enum contain?

Copy link
Contributor

Choose a reason for hiding this comment

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

None, ForceUpdate? Something like that? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants