What is the correct use of auto?

Georg Wrede georg at nospam.org
Mon Apr 14 10:37:18 PDT 2008


Unknown W. Brackets wrote:
> Why?  I'm not sure I see the relevance of your analogies to the point.
> 
> Surely no one shall die if they use auto.  Also, not knowing what 
> something is (which can cause you no harm directly) is very different 
> from not knowing where incoming danger might be.
> 
> I think you're trying to say that having autos in examples is like 
> having mines, in that they are possible points of confusion to newcomers 
> (who will not know what those things are.)  You may also be trying to 
> say that using auto in examples teaches people to use auto more than 
> they should.

I'm ok with auto used, actually, as much as possible. I think it does 
make the code easier to read and it definitely makes a big difference 
when you later decide to change some return type.

> However, I would say that auto should not be used, except in any case 
> where the type auto is representing:
> 
>     - is unimportant to surrounding code (it is being paper-pushed.)
>     - is unlikely to be used in any way other than the represented way 
> (e.g. has no other useful methods than are already being called.)
>     - is documented in some comment in the example.
>     - is an example type, with no actual importance to the example (e.g. 
> to be passed to a method that takes any value, which is what the example 
> is showing.)

Ok, I'll try to explain my point again: I do agree with the above. Now, 
the problem lies in, let's say auto was banned from examples. It hardly 
makes the _point_ of an example harder to understand, so banning auto 
should be harmless.

Now, not banning auto in examples, results in two things: only some 
people try to assess whether auto is ok for this particular example, and 
most not, and secondly, even those who try, often don't get the right 
answer, so the choice becomes arbitrary.

To see whether auto is appropriate for the example, requires several 
things *simultaneously* falling right:

  - the person has learned several languages, and is able to remember 
many of the situations where the examples in those particular languages 
have been hard to understand or have created collateral misunderstandings

  - the person has had experience /teaching/ several languages to not 
only one, but many kinds of audiences

  - the person is interested in conveying the message of the example 
beyond only the nominal issue at hand

  - the person is able to really enumerate the audiences getting exposed 
to the particular example

  - the person is able to quantify the needs of those particular audiences

  - the person has actually beta tested the example with representats of 
each audience

  - the person simply has to have the required mental cababilities for 
the above, which are separate from the ability to program in the first place

  - the person is not pressed for time (like a deadline, or other jobs 
he may be busy with), which he would need to carefully assess the merits 
or dismerits of auto in a particular example

There are other things as well, but this list gives the hint that 
judging auto for an example is truly a non-trivial task. (Of course, 
there are other judgements to be made, too, not only those pertaining to 
auto.)

A lot of textbooks are written every week. Only some of them are good. 
This illustrates that it is not easy at all to decide what and how 
should be written.


Now, banning auto from examples, both saves much hard work for the 
writer, it also makes for a consistent representation of the language.

Someone who really wants to learn fluency in a language has to (and 
will) go through masses of good source code. At the latest, at that 
point it will become evident that auto is actually used pretty much 
everywhere that it possibly can. And it should.



More information about the Digitalmars-d mailing list