Comment
Author: Admin | 2025-04-28
More type annotations. Where if you follow Herb Sutter’s advice in C++, that is not the case.So if I had to translate the Scala method to C++ it would be “start off with auto, but whenever you get the chance, make the types explicit.” But I don’t like this rule. It makes sense in Scala because in Scala type inference is the default. In C++ there is no default, so why recommend doing the version that will benefit from being changed later?As for Herb’s specific arguments: He is convinced that using auto does not decrease readability. He uses a template as an example of where we already don’t have type information. Which is ironic, because templates are notoriously unreadable. There are reasons why so many people are intimidated by templates, and the lack of type information is one of them.Another problem is that I search for types a lot. Whenever I come to a new piece of code I do a lot of searches for type names and function names to get a rough understanding of how pieces fit together. In Visual Studio I can search for all references of a type, but it is unreliable. I usually just do a plain text search because it is more reliable than using the smarter search. Using auto makes searching for types more difficult, which adds work for people when they want to learn how objects are used.Which is why I’m much more pragmatic about auto: I use it only if I think that the type information will not be terribly useful for people looking at my code. Meaning mainly for iterators.
Add Comment