-
-
Notifications
You must be signed in to change notification settings - Fork 686
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
STYLE: Replace postfix by prefix increment in for
loops in Core/Common
#2493
Conversation
@@ -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) |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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)
... 😸
FYI, it looks like a Clang-Tidy option to maintain this style is underway: |
👍 |
@@ -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++) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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++)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Back to "work in progress": an update is underway! |
dff7824
to
ff9e1a7
Compare
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).
ff9e1a7
to
d7eea8a
Compare
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"
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"
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"
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
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
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
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"
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"
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"
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"
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"
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:
Manually replaced a few more postfix increments, which were not detected
by this regex command, as remarked by Mikhail Isakov (@issakomi).