by a1o · 3y ago · 0 comments
Hey, I am curious, when do I want to do that instead of relying on auto? Mostly curious, as I have been using auto in C++ (since C++11) relatively heavily.
From Google's C++ Style Guide:
https://google.github.io/styleguide/cppguide.html#Type_deduc...
> The fundamental rule is: use type deduction only to make the code clearer or safer, and do not use it merely to avoid the inconvenience of writing an explicit type. When judging whether the code is clearer, keep in mind that your readers are not necessarily on your team, or familiar with your project, so types that you and your reviewer experience as unnecessary clutter will very often provide useful information to others. For example, you can assume that the return type of make_unique<Foo>() is obvious, but the return type of MyWidgetFactory() probably isn't.
I would typically use auto while working on something but then have to replace many of them before creating a code review. I wish Visual Studio could do that, it clearly already knows the type.
Not just reviews, but anything that involves reading the code (which is frequently usage and maintenance). Swimming in a file where everything is auto is a special kind of hell. It's like having no firm ground to step on.
I understand how type deduction works just fine, thank you. Your assumption that I (or many others) have this stance based on some lack of understanding of how type deduction works is extremely wrong, and completely missing the arguments people are actually making.
I agree that if you spend a lot of time reading code in something like GitHub, not having explicit types is annoying, but seriously, who does that?
- No review tool is capable of providing type inference hints.
- Not everybody's development workflow consists of using the IDEs.
- Development/debugging on remote machines does not even have a GUI to start with - so no IDEs there as well.
It also makes it harder to grep for usages of any given type. Of course IDEs could help with that too, but I don’t know any that provide that functionality.
I wouldn't use it for my project or any new project. It's out of date with how everyone else codes C++.
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
Isn't the return type of a widget factory method obviously Widget? This doesn't seem like a great example. Of course the method name could be wrong and not enforced at the language level like something between angle brackets, but idk.
My take is: if the code is type-agnostic (i.e. should remain identical when the type changes), and writing the type wouldn't help the reader, and the type itself isn't already a simple-to-write template (like T), then use auto. (For example, this is the case when you're just taking some value and passing it along, and have no reason to care about the type at all.) Otherwise, write it out.
The intent being that you should prefer to spell out the type unless it's genuinely adding a burden (such as being difficult to read, or adding an extra location to update if it ever changes) when the benefits are also negligible, because it's frequently useful information and a good sanity check.
1. you refactor, you change the type declaration in one place, auto handles the boring work of replacing the characters throughout your project.
2. you refactor, you change the type definition in one place, auto will handle replacing all the instances in your project, hell, it might even compile afterwards.
I believe you are describing 1. I find that easy enough to do with 'Sed' or IDE refactoring tools.
2, is more subtle and the new behaviour could now be worthy of scrutiny throughout the project. I find it difficult then to 'Grep' or IDE search through the project for all instances reliably when auto is in use. It is much easier for me with spelled out types.
I would trade the benefit of auto in 1) for the safety of spelled out types in 2) every single time.
In other words, you concentrate your review on the original and new type, instead of the usages.
But that's second priority to readability, because people read code much more often than they refactor it, and they need to be able to easily anchor their understanding when reading. Optimizing code for efficiency of editing instead of the ability to understand it is getting the priorities very wrong.
> Keep in mind hover documentation often isn't readily available for pull request reviewers. Most of the time, reviewers will use GitHub's online viewer to review pull requests.
`using` directives and typedefs are not forbidden, so this would avoid situations where a type is never explicitly declared.
Having worked in large code bases that used `auto` almost exclusively when possible… I’m not sure I agree. However, I understand.
https://docs.Godotengine.org/en/stable/contributing/developm...
var m = new Thing(); // you know its a thing
var t = a.b().c(); // what is it? var t = a.b().c(); // what is it?
Most likely bad code (on the right hand side) for (auto const & thing : container_of_things) {
thing->method ();
}
in this context (and this context alone) i love not knowing or having to explicit denote the type of "thing".