-
-
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
STYLE: Use auto
for declaration of variables initialized by New()
#2826
STYLE: Use auto
for declaration of variables initialized by New()
#2826
Conversation
Dankjewel Niels! Can you quickly explain why, in your opinion, the new version has a "better" style than the older one? It is not obvious to me and it would be easier to adopt the new style if I'm convinced it's better. |
@SimonRit Graag gedaan Simon! I think the use of Personally I find initialization by
versus:
Hans Johnson (@hjmjohnson) has already applied Clang-Tidy option modernize-use-auto before, with commit 8a2a15f "STYLE: Use auto for variable creation", 9 February 2018. This pull-request is basically a follow-up to his commit. It's just that Clang-Tidy does not recognize Does that make things more obvious to you? |
Got it, thanks! The benefit is not obvious to me but that does not matter, it is just yet another indication that I'm aging. |
Actually as I'm aging myself, my typing slows down a little, and I appreciate avoiding redundant typing even more than before! Having said so, I do believe that the new generation of C++ programmers (those who started learning C++ when C++11 was already standardized) has very much embraced C++11
Good catch @SimonRit ! An amend is underway, including those variable declarations that do not contain |
9833d38
to
b9f8666
Compare
auto
for declaration of variables initialized by New()
This aging C++ programmer tries to avoid using VariableVectorInterpolatorTypePointer = typename VariableVectorInterpolatorType::Pointer;
VariableVectorInterpolatorTypePointer interpolator{ VariableVectorInterpolatorType::New() }; ) My reasoning has to do with the fact that the compiler can catch a type mismatch in milliseconds (microseconds?) but it sometimes takes me hours to track down a bug caused by an unexpected type. But this is not a show stopper -- unless you are persuaded too. 😄 |
Thanks for your comment @Leengit
Wow, so unfortunate to hear you prefer those two lines to a simple one-liner, using auto interpolator = VariableVectorInterpolatorType::New(); I can understand that using |
Yes, I agree 110% that FWIW, my favorite approach, for when I am unsure what the type should be, is: auto returnValue = MyFunctionWithUnknownReturnType();
char ***z = returnValue; When I compile that, my compiler tells me what type it is that it cannot convert to |
Not saying this is what you've done here, but, in general: this is the wrong thing to optimize for. Code is read much more than written, and readability should be favored over minimising typing. I'm generally not a fan of |
Thanks for your comment @seanm Not saying that you're suggesting otherwise, but I do care very much about code readability. For example, by carefully choosing identifier names, by using functional abstraction, and by commenting code that might not be self-documenting. In this particular case, with this pull request I think readability is actually improved. Just like it is documented about the Clang-Tidy modernize-use-auto check (which was already applied to ITK, by Hans):
So really, I'm just doing my best to improve the code. |
Replaced initializations of the form `T::Pointer var = T::New()` (sometimes starting with `typename`) with `auto var = T::New()`, to reduce code redundancy. In accordance with C++ Core Guidelines, August 19, 2021: "ES.11: Use `auto` to avoid redundant repetition of type names" https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-auto Using Notepad++ v8.1.4, Find in Files (Regular expression): Find what: typename (\w+)::Pointer[ ]+(\w+) = \1::New\(\); Replace with: auto $2 = $1::New\(\); Filters: itk*.hxx;itk*.h;itk*.cxx And then again without `typename`: Find what: (\w+)::Pointer[ ]+(\w+) = \1::New\(\); Corresponds with ITK pull request InsightSoftwareConsortium/ITK#2826
For the record, it appears that this pull request is in accordance with the ITK Coding Style (from the ITK Software Guide), and specifically the section The auto Keyword, added by Matt McCormick (@thewtex) commit InsightSoftwareConsortium/ITKSoftwareGuide@51e9227 which says:
So if you agree, please say so by approving this pull request 😃 |
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.
I think this is one of those perfect spots for auto, the type can be inferred by the reader in the same line.
@phcerdan Thanks Pablo! I saw that you originally suggested to Matt to include ITK's |
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.
I will myself need to get used to it, but IMHO this improves readability.
As for the ITK SW Guide commit pointed in #2826 (comment):
- Adding examples to each of the points in the
The auto Keyword
section would be extremely helpful and maybe enlightening (if compared to not usingauto
). - Besides the examples whose code is automatically taken from the ITK code base, the ITK SW Guide contains code blocks interspersed across the
LaTeX
source, and those should be changed too to avoid confusing contributors.
Thanks.
@jhlegarreta Thanks for your approval Jon!
Addressed by pull request InsightSoftwareConsortium/ITKSoftwareGuide#163 please check! |
In accordance with the ITK Coding Style, section The auto Keyword: > The `auto` keyword should be used to specify a type in the following cases: > - The type is duplicated on the left side of an initialization when > it is mandated on the right side, e.g. when there is an explicit > cast or initializing with `new` or ITK's `::New()`. ... Suggested by Jon Haitz Legarreta Gorroño as part of the review of pull request InsightSoftwareConsortium/ITK#2826 "STYLE: Use `auto` for declaration of variables initialized by `New()`"
Replaced initializations of the form `T::Pointer var = T::New()` (sometimes starting with `typename`) with `auto var = T::New()`, to reduce code redundancy. In accordance with C++ Core Guidelines, August 19, 2021: "ES.11: Use `auto` to avoid redundant repetition of type names" https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-auto Using Notepad++ v8.1.4, Find in Files (Regular expression): Find what: typename (\w+)::Pointer[ ]+(\w+) = \1::New\(\); Replace with: auto $2 = $1::New\(\); Filters: itk*.hxx;itk*.h;itk*.cxx And then again without `typename`: Find what: (\w+)::Pointer[ ]+(\w+) = \1::New\(\); Corresponds with ITK pull request InsightSoftwareConsortium/ITK#2826
b9f8666
to
11b250c
Compare
Replaced initializations of the form `T::Pointer var = T::New()` (sometimes starting with `typename`) with `auto var = T::New()`, to reduce code redundancy. In accordance with C++ Core Guidelines, August 19, 2021: "ES.11: Use `auto` to avoid redundant repetition of type names" https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-auto Using Notepad++ v8.1.4, Find in Files (Regular expression): Find what: typename (\w+)::Pointer[ ]+(\w+) = \1::New\(\); Replace with: auto $2 = $1::New\(\); Filters: itk*.hxx;itk*.h;itk*.cxx And then again without `typename`: Find what: (\w+)::Pointer[ ]+(\w+) = \1::New\(\); Follow-up to commit de713e7 "STYLE: Use auto for variable creation", Hans Johnson, 9 February 2018.
11b250c
to
5ab79bc
Compare
Replaced initializations of the form `T::Pointer var = T::New()` with `auto var = T::New()`, in accordance with ITK Coding Style section "The auto Keyword", at https://github.com/InsightSoftwareConsortium/ITKSoftwareGuide/blob/v5.2.1/SoftwareGuide/Latex/Appendices/CodingStyleGuide.tex#L1507-L1511 Using Notepad++ v8.1.4, Find in Files (Regular expression): Find what: (\w+)::Pointer[ ]+(\w+) = \1::New\(\); Replace with: auto $2 = $1::New\(\); Corresponding ITK pull request: InsightSoftwareConsortium/ITK#2826
Replaced initializations of the form `T::Pointer var = T::New()` with `auto var = T::New()`, in accordance with ITK Coding Style section "The auto Keyword", at https://github.com/InsightSoftwareConsortium/ITKSoftwareGuide/blob/v5.2.1/SoftwareGuide/Latex/Appendices/CodingStyleGuide.tex#L1507-L1511 Using Notepad++ v8.1.4, Find in Files (Regular expression): Find what: (\w+)::Pointer[ ]+(\w+) = \1::New\(\); Replace with: auto $2 = $1::New\(\); Corresponding ITK pull request: InsightSoftwareConsortium/ITK#2826
Replace initializations of the form `T::Pointer var = T::New()` (sometimes starting with `typename`) with `auto var = T::New()` in `Examples` to reduce code redundancy In accordance with C++ Core Guidelines, August 19, 2021: "ES.11: Use `auto` to avoid redundant repetition of type names" https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-auto Following PR InsightSoftwareConsortium#2826 and commit 828453d.
Replace initializations of the form `T::Pointer var = T::New()` (sometimes starting with `typename`) with `auto var = T::New()` in `Examples` to reduce code redundancy In accordance with C++ Core Guidelines, August 19, 2021: "ES.11: Use `auto` to avoid redundant repetition of type names" https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-auto Following PR #2826 and commit 828453d.
Replaced initializations of the form
T::Pointer var = T::New()
(sometimes starting with
typename
) withauto var = T::New()
, toreduce code redundancy.
In accordance with C++ Core Guidelines, August 19, 2021:
"ES.11: Use
auto
to avoid redundant repetition of type names"https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-auto
Using Notepad++ v8.1.4, Find in Files (Regular expression):
And then again without
typename
:Follow-up to commit de713e7
"STYLE: Use auto for variable creation", Hans Johnson (@hjmjohnson), 9 February 2018.