PERF: readability container size empty#414
Merged
hjmjohnson merged 1 commit intoInsightSoftwareConsortium:masterfrom Jan 15, 2019
Merged
Conversation
N-Dekker
approved these changes
Jan 15, 2019
Contributor
N-Dekker
left a comment
There was a problem hiding this comment.
Looks good to me; correctly follows Scott Meyers, Effective STL (2001) Item 4: "Call empty instead of checking size() against zero", as far as I can see
dzenanz
approved these changes
Jan 15, 2019
| std::string filename = name; | ||
|
|
||
| if ( filename == "" ) | ||
| if ( filename.empty() ) |
Member
There was a problem hiding this comment.
Do you intend to contribute 3rd party (GDCM etc) changes upstream?
Member
The emptiness of a container should be checked using the empty() method instead of the size() method. It is not guaranteed that size() is a constant-time function, and it is generally more efficient and also shows clearer intent to use empty(). Furthermore some containers may implement the empty() method but not implement the size() method. Using empty() whenever possible makes it easier to switch to another container in the future. SRCDIR=/Users/johnsonhj/Dashboard/src/ITK #My local SRC BLDDIR=/Users/johnsonhj/Dashboard/src/ITK-clang #My local BLD cd /Users/johnsonhj/Dashboard/src/ITK-clang run-clang-tidy.py -extra-arg=-D__clang__ -checks=-*,readability-container-size-empty -header-filter=.* -fix
8bc31c8 to
f11cb40
Compare
Member
I am not following - are errors expected? |
Member
|
I expected 3 builds with |
Member
Author
N-Dekker
added a commit
to SuperElastix/elastix
that referenced
this pull request
May 23, 2021
Ran Clang-Tidy (LLVM 11.1.0) `readability-container-size-empty`, as motivated at https://clang.llvm.org/extra/clang-tidy/checks/readability-container-size-empty.html : "The emptiness of a container should be checked using the empty() method instead of the size() method. It is not guaranteed that size() is a constant-time function, and it is generally more efficient and also shows clearer intent to use empty(). Furthermore some containers may implement the empty() method but not implement the size() method. Using empty() whenever possible makes it easier to switch to another container in the future." Follows ITK pull request InsightSoftwareConsortium/ITK#414 commit InsightSoftwareConsortium/ITK@c82a5f2 "PERF: readability container size empty", Hans Johnson, January 2019.
N-Dekker
added a commit
to SuperElastix/elastix
that referenced
this pull request
May 23, 2021
Ran Clang-Tidy (LLVM 11.1.0) `readability-container-size-empty`, as motivated at https://clang.llvm.org/extra/clang-tidy/checks/readability-container-size-empty.html : "The emptiness of a container should be checked using the empty() method instead of the size() method. It is not guaranteed that size() is a constant-time function, and it is generally more efficient and also shows clearer intent to use empty(). Furthermore some containers may implement the empty() method but not implement the size() method. Using empty() whenever possible makes it easier to switch to another container in the future." Got extra motivated by having my very first microsoft/STL commit on this topic: microsoft/STL@b95ba0e "Avoid calling size() in empty() member functions" (merged from pull request microsoft/STL#1836) Follows ITK pull request InsightSoftwareConsortium/ITK#414 commit InsightSoftwareConsortium/ITK@c82a5f2 "PERF: readability container size empty", Hans Johnson, January 2019.
N-Dekker
added a commit
to SuperElastix/elastix
that referenced
this pull request
May 23, 2021
Ran Clang-Tidy (LLVM 11.1.0) `readability-container-size-empty`, as motivated at https://clang.llvm.org/extra/clang-tidy/checks/readability-container-size-empty.html : "The emptiness of a container should be checked using the empty() method instead of the size() method. It is not guaranteed that size() is a constant-time function, and it is generally more efficient and also shows clearer intent to use empty(). Furthermore some containers may implement the empty() method but not implement the size() method. Using empty() whenever possible makes it easier to switch to another container in the future." Got extra motivated by having my very first microsoft/STL commit on this topic: microsoft/STL@b95ba0e "Avoid calling size() in empty() member functions" (merged from pull request microsoft/STL#1836) Follows ITK pull request InsightSoftwareConsortium/ITK#414 commit InsightSoftwareConsortium/ITK@c82a5f2 "PERF: readability container size empty", Hans Johnson, January 2019.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The emptiness of a container should be checked using the empty() method
instead of the size() method. It is not guaranteed that size() is a
constant-time function, and it is generally more efficient and also
shows clearer intent to use empty(). Furthermore some containers may
implement the empty() method but not implement the size() method. Using
empty() whenever possible makes it easier to switch to another container
in the future.
SRCDIR=/Users/johnsonhj/Dashboard/src/ITK #My local SRC
BLDDIR=/Users/johnsonhj/Dashboard/src/ITK-clang #My local BLD
cd /Users/johnsonhj/Dashboard/src/ITK-clang
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-,readability-container-size-empty -header-filter=. -fix