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

Convert variables to structured bindings #13018

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

Conversation

tygyh
Copy link
Contributor

@tygyh tygyh commented Aug 17, 2024

No description provided.

Source/Core/DolphinTool/ConvertCommand.cpp Outdated Show resolved Hide resolved
Source/Core/UpdaterCommon/UpdaterCommon.cpp Outdated Show resolved Hide resolved
Source/Core/UpdaterCommon/UpdaterCommon.cpp Outdated Show resolved Hide resolved
@@ -319,8 +318,8 @@ TodoList ComputeActionsToDo(Manifest this_manifest, Manifest next_manifest)
void CleanUpTempDir(const std::string& temp_dir, const TodoList& todo)
{
// This is best-effort cleanup, we ignore most errors.
for (const auto& download : todo.to_download)
File::Delete(temp_dir + DIR_SEP + HexEncode(download.hash.data(), download.hash.size()));
for (const auto& [_filename, hash] : todo.to_download)
Copy link
Member

Choose a reason for hiding this comment

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

Even though this is unused, please don't use variables that start with an underscore. The C++ standard reserves many of those (with varying rules and scopes) and could break at any given moment.
I don't think this particular one is considered reserved, but it feels out-of-place to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How should I mark unused variables? There are very many when using structured bindings.

Copy link
Member

Choose a reason for hiding this comment

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

I'd just name them as you'd name a used one, plus potentially apply [[maybe_unused]] if it raises an unused variable warning (see the other remark.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are no warnings raised, I just want it to be clear which parts are actually in use.

Source/Core/UpdaterCommon/UpdaterCommon.cpp Outdated Show resolved Hide resolved
const auto op = *op_it;
std::string build_info_path =
temp_dir + DIR_SEP + HexEncode(op.new_hash.data(), op.new_hash.size());
const auto [_filename, old_hash, new_hash] = *op_it;
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

One could even argue that this needs [[maybe_unused]] to prevent unused variable warnings. Or use std::tie which can use std::ignore. Or wait for C++26 with P2169R0 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Neither of the two suggestions work.

Copy link
Member

Choose a reason for hiding this comment

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

Just leave them without the underscore then. I don't really see a need to distinguish them in naming just because they're (intentionally) unused or not.

@@ -284,8 +283,8 @@ bool CheckBuildInfo(const BuildInfos& build_infos)
// The existing BuildInfo may have been modified. Be careful not to overly trust its contents!

// If the binary requires more recent OS, inform the user.
auto os_check = OSVersionCheck(build_infos.next);
if (os_check.status == VersionCheckStatus::UpdateRequired)
auto [status, _current_version, _target_version] = OSVersionCheck(build_infos.next);
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

@Tilka
Copy link
Member

Tilka commented Aug 18, 2024

Imo structured bindings mainly make sense when dealing with generic types like std::pair to assign meaningful names in place of first/second. I don't see a benefit when dealing with custom classes that already having meaningful names for data members.

@tygyh
Copy link
Contributor Author

tygyh commented Aug 18, 2024

I don't see a benefit when dealing with custom classes that already having meaningful names for data members.

Most of the SBs are just those meaningful names without the class instances as prefixes, so the lines just become a little bit shorter. I'll try to use SBs only where I think they make the code easier to read instead of applying it everywhere. (Some SBs have 10+ members and only one is used)

@BhaaLseN
Copy link
Member

Shorter code doesn't necessarily mean more readable code. Or that the original wasn't readable in first place. The goal isn't to make things as short as possible, but as readable and maintainable as possible.

@tygyh
Copy link
Contributor Author

tygyh commented Aug 18, 2024

The goal isn't to make things as short as possible, but as readable and maintainable as possible.

I agree, but I don't think the class instance names add enough context to warrant being read most of the time.

@Tilka
Copy link
Member

Tilka commented Aug 18, 2024

Consider this: If I change the declaration order of struct members for some reason, I now have to update each structured binding that unpacks this struct. And if the member types match, I could easily miss an instance without triggering an error!

@tygyh
Copy link
Contributor Author

tygyh commented Aug 18, 2024

Consider this: If I change the declaration order of struct members for some reason, I now have to update each structured binding that unpacks this struct. And if the member types match, I could easily miss an instance without triggering an error!

I didn't know that. Anything which can cause silent errors I agree should be used sparingly, if at all.

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.

6 participants