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

STYLE: Replace postfix by prefix increment in for loops in Core/Common #2493

Merged

Conversation

N-Dekker
Copy link
Contributor

@N-Dekker N-Dekker commented Apr 19, 2021

Followed common C++ guidelines, including:

Google C++ Style Guide: "Use the prefix form (++i) of the increment and
decrement operators unless you need postfix semantics."
https://google.github.io/styleguide/cppguide.html#Preincrement_and_Predecrement

Herb Sutter, Andrei Alexandrescu - C++ Coding Standards: 101 Rules,
Guidelines, and Best Practices: "Prefer the canonical form of ++ and --.
Prefer calling the prefix forms"
https://www.oreilly.com/library/view/c-coding-standards/0321113586/ch29.html

Applied at GNU bash, by the following command:

find . \( -iname *.cxx -or -iname *.hxx -or -iname *.h \) -exec perl -pi -w -e 's/(\s+for \(.*; .+); (\w+)\+\+\)/$1; ++$2)/g;' {} \;

Manually replaced a few more postfix increments, which were not detected
by this regex command, as remarked by Mikhail Isakov (@issakomi).

@@ -104,7 +104,7 @@ class ITK_TEMPLATE_EXPORT Array : public vnl_vector<TValue>
{
this->m_LetArrayManageMemory = true;
this->SetSize(r.GetSize());
for (SizeValueType i = 0; i < r.GetSize(); i++)
for (SizeValueType i = 0; i < r.GetSize(); ++i)
Copy link
Member

Choose a reason for hiding this comment

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

Ahhh, ++i looks so strange!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahhh, ++i looks so strange!

@dzenanz Are you serious? We could introduce a function, itk::Increment(i)... 😸

@N-Dekker
Copy link
Contributor Author

FYI, it looks like a Clang-Tidy option to maintain this style is underway:
"[clang-tidy] Add performance-prefer-preincrement check"
https://reviews.llvm.org/D72553

@N-Dekker N-Dekker marked this pull request as ready for review April 19, 2021 20:11
@dpshamonin
Copy link
Contributor

👍

@@ -83,7 +83,7 @@ itkNeighborhoodIteratorTest(int, char *[])

it3.Print(std::cout);
unsigned x, y, i;
for (y = 0, i = 0; y < 5; y++)
for (y = 0, i = 0; y < 5; ++y)
{
for (x = 0; x < 5; x++, i++)
Copy link
Member

@issakomi issakomi Apr 20, 2021

Choose a reason for hiding this comment

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

FYI, here postfix increment was not detected by regex, probably, (and several below the line)

Copy link
Member

Choose a reason for hiding this comment

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

Probably all multi-variable increments are missed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch @issakomi Do you have a suggestion how to catch and fix such cases by regex?

Obviously we must be 100% sure that the semantics don't change by the regex!

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, not yet sure how to automate this in reliable way. Looks like in Core/Common multi-variable for with postfix increment are below, at least with "for" and "," at a single line

./test/itkNeighborhoodIteratorTest.cxx:    for (x = 0; x < 5; x++, i++)
./test/itkNeighborhoodIteratorTest.cxx:    for (x = 0; x < 5; x++, i++)
./test/itkNeighborhoodIteratorTest.cxx:    for (x = 0; x < 5; x++, i++)
./test/itkNeighborhoodIteratorTest.cxx:    for (x = 0; x < 5; x++, i++)
./test/itkNeighborhoodIteratorTest.cxx:    for (x = 0; x < 5; x++, i++)
./test/itkNeighborhoodIteratorTest.cxx:    for (x = 0; x < 5; x++, i++)
./test/itkNeighborhoodIteratorTest.cxx:    for (x = 0; x < 5; x++, i++)
./test/itkNeighborhoodIteratorTest.cxx:    for (x = 0; x < 5; x++, i++)
./test/itkNeighborhoodIteratorTest.cxx:    for (x = 0; x < 5; x++, i++)
./test/itkObjectFactoryTest.cxx:    for (std::list<std::string>::const_iterator o = overrides.begin(); o != overrides.end(); ++o, ++n, ++d, e++)
./test/itkPriorityQueueTest.cxx:  for (; it != sequence.end(); ++it, i++)
./test/itkObjectFactoryTest2.cxx:      for (std::list<std::string>::const_iterator o = overrides.begin(); o != overrides.end(); ++o, ++n, ++d, e++)
./include/itkNeighborhoodIterator.hxx:    for (N_it = N.Begin(), this_it = this->Begin(); this_it < _end; this_it++, N_it++)
./include/itkNeighborhoodIterator.hxx:    for (N_it = N.Begin(), this_it = this->Begin(); this_it < _end; this_it++, N_it++)
./include/itkNeighborhoodIterator.hxx:    for (N_it = N.Begin(), this_it = this->Begin(); this_it < _end; N_it++, this_it++)

Copy link
Member

Choose a reason for hiding this comment

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

We don't have to change every instance now. Getting most of them is sufficient improvement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@N-Dekker N-Dekker marked this pull request as draft April 20, 2021 19:00
@N-Dekker
Copy link
Contributor Author

Back to "work in progress": an update is underway!

Followed common C++ guidelines, including:

Google C++ Style Guide: "Use the prefix form (++i) of the increment and
decrement operators unless you need postfix semantics."
https://google.github.io/styleguide/cppguide.html#Preincrement_and_Predecrement

Herb Sutter, Andrei Alexandrescu - C++ Coding Standards: 101 Rules,
Guidelines, and Best Practices: "Prefer the canonical form of ++ and --.
Prefer calling the prefix forms"
https://www.oreilly.com/library/view/c-coding-standards/0321113586/ch29.html

Applied at GNU bash, by the following command:

	find . \( -iname *.cxx -or -iname *.hxx -or -iname *.h \) -exec perl -pi -w -e 's/(\s+for \(.*; .+); (\w+)\+\+\)/$1; ++$2)/g;' {} \;

Manually replaced a few more postfix increments, which were not detected
by this regex command, as remarked by Mikhail Isakov (issakomi).
@N-Dekker N-Dekker marked this pull request as ready for review April 20, 2021 20:24
@N-Dekker N-Dekker merged commit f81578b into InsightSoftwareConsortium:master Apr 21, 2021
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Apr 21, 2021
Applied to ITK/Modules, at the GNU bash prompt:

    find . \( -iname *.cxx -or -iname *.hxx -or -iname *.h \) -exec perl -pi -w -e 's/(\s+for \(.*; .+); (\w+)\+\+\)/$1; ++$2)/g;' {} \;

(Excluded ITK/Modules/ThirdParty.)

Follow-up to:
pull request InsightSoftwareConsortium#2493
commit f81578b
"STYLE: Replace postfix by prefix increment in `for` loops in Core/Common"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Apr 21, 2021
Applied to ITK/Modules, at the GNU bash prompt:

    find . \( -iname *.cxx -or -iname *.hxx -or -iname *.h \) -exec perl -pi -w -e 's/(\s+for \(.*; .+); (\w+)\+\+\)/$1; ++$2)/g;' {} \;

(Excluded ITK/Modules/ThirdParty.)

According to Herb Sutter, Andrei Alexandrescu - C++ Coding Standards:
101 Rules, Guidelines, and Best Practices: "Prefer the canonical form of
++ and --. Prefer calling the prefix forms"

Follow-up to:
pull request InsightSoftwareConsortium#2493
commit f81578b
"STYLE: Replace postfix by prefix increment in `for` loops in Core/Common"
N-Dekker added a commit that referenced this pull request Apr 23, 2021
Applied to ITK/Modules, at the GNU bash prompt:

    find . \( -iname *.cxx -or -iname *.hxx -or -iname *.h \) -exec perl -pi -w -e 's/(\s+for \(.*; .+); (\w+)\+\+\)/$1; ++$2)/g;' {} \;

(Excluded ITK/Modules/ThirdParty.)

According to Herb Sutter, Andrei Alexandrescu - C++ Coding Standards:
101 Rules, Guidelines, and Best Practices: "Prefer the canonical form of
++ and --. Prefer calling the prefix forms"

Follow-up to:
pull request #2493
commit f81578b
"STYLE: Replace postfix by prefix increment in `for` loops in Core/Common"
N-Dekker added a commit to SuperElastix/elastix that referenced this pull request May 20, 2021
Follows common C++ guidelines, including:

Google C++ Style Guide: "Use the prefix form (++i) of the increment and decrement operators unless you need postfix semantics."
https://google.github.io/styleguide/cppguide.html#Preincrement_and_Predecrement

Herb Sutter, Andrei Alexandrescu - C++ Coding Standards: 101 Rules, Guidelines, and Best Practices: "Prefer the canonical form of ++ and --. Prefer calling the prefix forms"
https://www.oreilly.com/library/view/c-coding-standards/0321113586/ch29.html

Anticipates "[clang-tidy] Add performance-prefer-preincrement check":
https://reviews.llvm.org/D72553

Corresponding ITK pull requests, which were merged in April 2021:
 - InsightSoftwareConsortium/ITK#2505
 - InsightSoftwareConsortium/ITK#2499
 - InsightSoftwareConsortium/ITK#2493
N-Dekker added a commit to SuperElastix/elastix that referenced this pull request May 20, 2021
Applied to the root of elastix source tree, at the GNU bash prompt:

    $ find . \( -iname *.cxx -or -iname *.hxx -or -iname *.h \) -exec perl -pi -w -e 's/(\s+for \(.*;.*); (\w+)\+\+\)/$1; ++$2)/g;' {} \;

Follows common C++ guidelines, including:

Google C++ Style Guide: "Use the prefix form (++i) of the increment and decrement operators unless you need postfix semantics."
https://google.github.io/styleguide/cppguide.html#Preincrement_and_Predecrement

Herb Sutter, Andrei Alexandrescu - C++ Coding Standards: 101 Rules, Guidelines, and Best Practices: "Prefer the canonical form of ++ and --. Prefer calling the prefix forms"
https://www.oreilly.com/library/view/c-coding-standards/0321113586/ch29.html

Anticipates "[clang-tidy] Add performance-prefer-preincrement check":
https://reviews.llvm.org/D72553

Corresponding ITK pull requests, which were merged in April 2021:
 - InsightSoftwareConsortium/ITK#2505
 - InsightSoftwareConsortium/ITK#2499
 - InsightSoftwareConsortium/ITK#2493
N-Dekker added a commit to SuperElastix/elastix that referenced this pull request May 21, 2021
Applied to the root of elastix source tree, at the GNU bash prompt:

    $ find . \( -iname *.cxx -or -iname *.hxx -or -iname *.h \) -exec perl -pi -w -e 's/(\s+for \(.*;.*); (\w+)\+\+\)/$1; ++$2)/g;' {} \;

Follows common C++ guidelines, including:

Google C++ Style Guide: "Use the prefix form (++i) of the increment and decrement operators unless you need postfix semantics."
https://google.github.io/styleguide/cppguide.html#Preincrement_and_Predecrement

Herb Sutter, Andrei Alexandrescu - C++ Coding Standards: 101 Rules, Guidelines, and Best Practices: "Prefer the canonical form of ++ and --. Prefer calling the prefix forms"
https://www.oreilly.com/library/view/c-coding-standards/0321113586/ch29.html

Anticipates "[clang-tidy] Add performance-prefer-preincrement check":
https://reviews.llvm.org/D72553

Corresponding ITK pull requests, which were merged in April 2021:
 - InsightSoftwareConsortium/ITK#2505
 - InsightSoftwareConsortium/ITK#2499
 - InsightSoftwareConsortium/ITK#2493
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Jul 11, 2022
Applied to the root of ITK source tree, at the GNU bash prompt:

    find . \( -iname itk*.cxx -or -iname itk*.hxx -or -iname itk*.h \) -exec perl -pi -w -e 's/  (\w+)\+\+;/  ++$1;/g;' {} \;

Follow-up to:

pull request InsightSoftwareConsortium#2493
commit f81578b
"STYLE: Replace postfix by prefix increment in `for` loops in Core/Common"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Jul 11, 2022
Applied to the root of ITK source tree, at the GNU bash prompt:

    find . \( -iname itk*.cxx -or -iname itk*.hxx -or -iname itk*.h \) -exec perl -pi -w -e 's/  (\w+)\+\+;/  ++$1;/g;' {} \;

Follow-up to:

pull request InsightSoftwareConsortium#2493
commit f81578b
"STYLE: Replace postfix by prefix increment in `for` loops in Core/Common"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Jul 12, 2022
Applied to the root of ITK source tree, at the GNU bash prompt:

    find . \( -iname itk*.cxx -or -iname itk*.hxx -or -iname itk*.h \) -exec perl -pi -w -e 's/  (\w+)\+\+;/  ++$1;/g;' {} \;
    find . \( -iname itk*.cxx -or -iname itk*.hxx -or -iname itk*.h \) -exec perl -pi -w -e 's/  (\w+)\-\-;/  --$1;/g;' {} \;

Follow-up to:
pull request InsightSoftwareConsortium#2493
commit f81578b
"STYLE: Replace postfix by prefix increment in `for` loops in Core/Common"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Jul 13, 2022
Applied to the root of ITK source tree, at the GNU bash prompt:

    find . \( -iname itk*.cxx -or -iname itk*.hxx -or -iname itk*.h \) -exec perl -pi -w -e 's/  (\w+)\+\+;/  ++$1;/g;' {} \;
    find . \( -iname itk*.cxx -or -iname itk*.hxx -or -iname itk*.h \) -exec perl -pi -w -e 's/  (\w+)\-\-;/  --$1;/g;' {} \;

Excluded itkConceptChecking.h, as it is intended to check both the prefix *and*
the postfix forms. Excluded the unit tests, as they might intentionally test
the postfix forms.

Follow-up to:
pull request InsightSoftwareConsortium#2493
commit f81578b
"STYLE: Replace postfix by prefix increment in `for` loops in Core/Common"
hjmjohnson pushed a commit that referenced this pull request Jul 14, 2022
Applied to the root of ITK source tree, at the GNU bash prompt:

    find . \( -iname itk*.cxx -or -iname itk*.hxx -or -iname itk*.h \) -exec perl -pi -w -e 's/  (\w+)\+\+;/  ++$1;/g;' {} \;
    find . \( -iname itk*.cxx -or -iname itk*.hxx -or -iname itk*.h \) -exec perl -pi -w -e 's/  (\w+)\-\-;/  --$1;/g;' {} \;

Excluded itkConceptChecking.h, as it is intended to check both the prefix *and*
the postfix forms. Excluded the unit tests, as they might intentionally test
the postfix forms.

Follow-up to:
pull request #2493
commit f81578b
"STYLE: Replace postfix by prefix increment in `for` loops in Core/Common"
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.

5 participants