David Brown <
david...@hesbynett.no> wrote:
> I find it strange that you expect to understand code line by line,
> rather than looking at the context.
I don't find it strange at all. The clearer the code is, the better.
If you can understand what a line of code is doing without having
to look at the surrounding code, all the better. (Sometimes this is
just not possible, of course, but if with a simple change you can
make it so, why not?)
> This is especially true given that
> full types in C++ can regularly take more than a single line to write out.
There we go again with the brevity argument.
My disdain for overt brevity has not appeared out of the blue. It's the
consequence of years of having to have read tens of thousands of lines
of code written by other people.
Length does not matter. Clarity does.
> When I use "auto", it is because the details of the type don't matter
> significantly, or they are clear from the context, or that they are too
> convoluted to be convenient to write out manually, or that the code is
> intended to be somewhat general. I think a combination of the first two
> is the most common.
*Maybe* if what 'auto' expands to is *extremely* clear to see from the
code, then *perhaps* it's acceptable. However, my point is that 'auto'
is being *overused* a lot. In other words, in many situations where
it's *not* clear at all what it's expanding to (often requiring going
to an entirely different file to see what it actually is. IDEs help
with this, but IMO code should be readable without the help of any
IDEs.)
>> Compare it to, say:
>>
>> for (const std::string& elem: <something>)
>>
>> Aha! Now we know a lot more about what <something> is, and what to
>> expect in subsequent lines of code. Just that one little change is
>> giving us a lot more information and clarity about what's happening here.
>
> Compare it to :
>
> void foo(std::vector<std::string> somethings) {
>
> for (const auto &elem : somethings) ...
Then, in the future, as you further develop the code, you add a couple
dozen lines before that loop. Will you then change that 'auto' to the
actual type? Unlikely.
> Why bother re-writing std::string inside the for loop? It doesn't make
> the code any clearer or safer - it is unnecessary repetition.
It's not unnecessary repetition if it makes the code easier to understand.
You write the same words in your English prose again and again and again.
You don't see that as a problem. Why do you find it a problem in code?
> (Programmers should have KISS tattooed on the inside of one eyelid, and
> DRY tattooed on the inside of the other.)
No, because if you keep it stupid, then your code will be stupid, and
nobody will understand it.
Keep your code smart, not stupid.
> And if they are changed - maybe "foo" gets changed to wide strings - the
> rest of the code is unchanged with "auto". But if you have manually
> written out the full type, you now have to remember to make changes in
> more places or you've got highly unexpected conversions lying around.
Conversely, if you accidentally write the wrong type when refactoring
code, subsequent 'auto' keywords may hide your mistake, while explicit
types could have caught it immediately.