-
-
Notifications
You must be signed in to change notification settings - Fork 665
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
PERF: readability container size empty #414
PERF: readability container size empty #414
Conversation
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.
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
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.
Excellent!
@@ -720,7 +720,7 @@ bool GDCMImageIO::CanWriteFile(const char *name) | |||
{ | |||
std::string filename = name; | |||
|
|||
if ( filename == "" ) | |||
if ( filename.empty() ) |
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.
Do you intend to contribute 3rd party (GDCM etc) changes upstream?
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
I am not following - are errors expected? |
I expected 3 builds with |
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.
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.
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.
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