4 ms·
People have different views on this. My take is: if the code is type-agnostic (i.e. should remain identical when the type changes), and writing the type wouldn
by dataflow 3y ago
People have different views on this.
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.
- fluoridation 3y agoOn the other hand, spelling out types tends to make refactors unnecessarily complicated. Something that could otherwise be simple, like moving a type from one scope to another, or changing the return type of a function for a different but compatible type, can require changing arbitrarily many usages.
- eliasmacpherson 3y agoThis refactoring business, I see it going two ways, there are probably more. 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.
- fluoridation 3y agoIf you're replacing types in this manner you don't scrutinize the usages. You design (or choose) the new type such that it actually is compatible with the old type. For example std::vector and std::list can be replaced with each other, because they either have the same functions with analogous behavior, or you get compiler errors if you use any of the non-overlapping functions. What you can't do is replace an std::vector with a class whose clear() fills the container with default-constructed instances of the objects. In other words, you concentrate your review on the original and new type, instead of the usages.
- eliasmacpherson 3y ago> You design (or choose) the new type such that it actually is compatible with the old type. Yes I agree, in that case sure, but when refactoring the case can arise that a new type must no longer compatible with the old type, then auto becomes a hindrance. std::vector and std::list have different behaviour regarding the validity of iterators after deletion (EDIT: and insertion so it seems!), to pick an example. Maybe I'm just paranoid?
- fluoridation 3y ago>the case can arise that a new type must no longer compatible with the old type Then you make sure that whatever causes an incompatibility also causes a compilation error. You can't rely on text searches and IDEs for something like that. >std::vector and std::list have different behaviour regarding the validity of iterators after deletion Fair enough, perhaps not the best-chosen example. I was thinking about them purely as collections, rather than as part of resource management. Checking their behavior with automated tools becomes much more difficult once people start taking pointers into elements. But then again, that's true of any class. If, for example, a member function returns a reference to a member and someone gets its address, now that location is implicitly relying on the internal stability of the class in a way that's invisible to the type system.
- eliasmacpherson 3y ago
- dataflow 3y agoWhen you can use auto without hampering readability, and while improving maintainability, by all means do so. This is often the case when the actual type doesn't matter to the reader, for example. I basically said this in my earlier comment. 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.
- fluoridation 3y agoSure, but like I said in a sibling comment, I don't believe that knowing the types of the objects is actually helpful in understanding a piece of code. You'll need to know the actual types to know its exact performance characteristics, but if you can't understand what the code does even at a high level when you can't see any types then there's a problem anyway.
- dataflow 3y ago> but if you can't understand what the code does even at a high level when you can't see any types then there's a problem anyway. Who says this implies you can't understand what the code does at a high level? Often you know the high level behavior but need to figure out how it works so you can update the code. If anything, the high level behavior is often easier to understand, since you have the API documentation etc. available. And in the cases where that is the case, what does "there's a problem anyway" mean? That the reader is too stupid to figure it out?
- fluoridation 3y agoNo, that the code is poorly documented, or that the names are poorly chosen.
- dataflow 3y agoSometimes, but not necessarily. Even when that's the case (and nobody always gets all the names perfect in the first iteration), you still face the problem of understanding it before you can fix that, so throwing your hands up is not a solution. Regardless, the rest of my comment stands.