[Development] RFC: more liberal 'auto' rules?

Olivier Goffart olivier at woboq.com
Fri Dec 4 20:14:16 CET 2015


On Friday 4. December 2015 13:12:00 Rafael Roquetto wrote:
> On Fri, Dec 04, 2015 at 02:25:14PM +0100, Olivier Goffart wrote:
> > On Friday 4. December 2015 14:11:48 Oswald Buddenhagen wrote:
> > > On Fri, Dec 04, 2015 at 02:07:10PM +0100, Marc Mutz wrote:
> > > > And as an aside, since it has been mentioned in this thread: in Python
> > > > _all_ variables are 'auto'. All. Without exception. Are Python
> > > > programmers more intelligent? Or do they just tolerate more pain? :)
> > > 
> > > i'd suggest the latter.
> > > no, really. people use external static checkers because the language
> > > lacks the feature.
> > > the lack of static typing is a common feature of scripting languages and
> > > makes them convenient to a degree, but it is an utter nightmare for any
> > > "real" software development. i really wouldn't want to go there.
> > 
> > But auto is still staticaly typed.
> > 
> > 
> > When you have code like
> > 
> >    foo->bar()->setFaz(m_factory->createFaz(foo->bar()->type()));
> > 
> > You don't see any type.
> > 
> > This code that use auto is not less readable. Even if you don't know
> > what's
> > the type of bar without looking it up.
> > 
> >   auto *bar = foo->bar();
> >   bar->setFaz(m_factory->createFaz(bar->type()));
> 
> Until you need to change your code and call any method of bar. Suppose I
> need to change the following code, to print the first item of the
> collection:
> 
>     auto *bar = foo->collection();
> 
>     /*TODO: print first item here */
> 
>     model->setCollection(bar); // we know bar is a collection, but of what
> kind?
> 
> 
> Choose the right alternative:
> 
>     a) qDebug() << bar->first();
>     b) qDebug() << bar->values().first();
>     c) qDebug() << bar[0]
>     d) none of the above
> 
> 
> To answer this, you will need to find out the return type of
> Foo::collection(); The compiler knows it, but the reader does not. And the
> answer depends on whether it is  a QList/QVector, QHash/QMap, etc... Had it
> been explicit, any programmer familiar with the Qt API (or any API in
> context) would be able to directly write down the right statement.

Ok, so i'll write it like this:

  AddresseeList *bar = foo->collection();

Is it better? do you know how to print the first element? You anyway need to 
look up what the AdresseeList typedef is. (Yes, it was made a typedef so one 
can more easily change what is the underlying, it's not so untypical)
You anyway have to know or lookup all the code you play around. 
Someone familiar with the codebase knows what Foo::collection() returns or can 
quickly look it up.
But the day somebody fixes the API and returns a  "const AddresseeList*" 
instead, you have to fix the locations you did not use auto.





More information about the Development mailing list