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

Use iterator pattern instead of manually traverse List::Element * #102354

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

Conversation

YYF233333
Copy link
Contributor

This is mostly a style change. for (List<>::Element *E = list.front(); E; E = E->next()) is lengthy and hard to read, for (const Type &E : list) make it clear that we are doing a one-way scan and not mutating the container or elements.

An additional benefit is that we have removed the type name of List from the loop, making it possible to replace the container type in the future. In the previous pattern, we would have to modify the for loop if we wanted to replace List with Vector or LocalVector.

@AThousandShips AThousandShips changed the title Using iterator pattern instead of manually traverse List::Element * Use iterator pattern instead of manually traverse List::Element * Feb 3, 2025
@@ -466,8 +466,8 @@ bool OS::is_restart_on_exit_set() const {
Vector<String> OS::get_restart_on_exit_arguments() const {
List<String> args = ::OS::get_singleton()->get_restart_on_exit_arguments();
Vector<String> args_vector;
for (List<String>::Element *E = args.front(); E; E = E->next()) {
args_vector.push_back(E->get());
for (const String &E : args) {
Copy link
Member

@adamscott adamscott Feb 3, 2025

Choose a reason for hiding this comment

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

We shouldn't reuse the same variable name here. Before, E was short for List<String>::Element, but now, it means arg.

Suggested change
for (const String &E : args) {
for (const String &arg : args) {

This comment is good for all the replacements in the PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or whatever is applicable in that context; if nothing feels appropriate, something generic like item or element works just as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the naming suggestion. I'm not very familiar with these code, so I prefer to use the original names to avoid issues like name conflicts. If you have a better name in mind, please feel free to suggest it.

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

Successfully merging this pull request may close these issues.

4 participants