[Development] RFC: lambda or lambda return from lambda?

Marc Mutz marc.mutz at kdab.com
Mon Feb 1 15:10:01 CET 2016


On Monday 01 February 2016 10:23:34 Christian Kandeler wrote:
> On 02/01/2016 11:08 AM, Marc Mutz wrote:
> > We're seeing increasing use of lambdas in dev, and I'm a bit unhappy
> > about the result.
> > 
> > E.g. (not picking on Anton here, I have done the same before):
> >   auto firstEqualsName = [&name](const QPair<QByteArray, QByteArray>
> >   &header) {
> >   
> >        return qstricmp(name.constData(), header.first) == 0;
> >   
> >   };
> >   fields.erase(std::remove_if(fields.begin(), fields.end(),
> >   
> >                               firstEqualsName),
> >                
> >                fields.end());
> > 
> > This is one way to write a unary predicate. But it hides the fact that
> > the predicate depends on the parameter 'name' (an argument to the
> > function this lambda is defined in).
> > 
> > With classical function objects, one would have written - at the call 
site:
> >   fields.erase(std::remove_if(fields.begin(), fields.end(),
> >   
> >                               FirstEquals(name)),
> >                
> >                fields.end());
> > 
> > See the difference?
> 
> Yes, but it is offset by another difference: As opposed to the function
> object, the lambda is defined right above the call site (or at least
> very close to it), so you can easily see that it captures an additional
> variable.
> I therefore think that this is not a problem.

The point of giving names to things (variable, functions, classes) in 
programming is so you don't need to look at the implementation all the time to 
see what it's doing. You only need to look when you want to see _how_ it's 
doing what it does.

So if you think that this is not a problem, then it's not a problem for you, 
either, if local variables are named only a, b, c, ... And I disagree.

Thanks,
Marc

-- 
Marc Mutz <marc.mutz at kdab.com> | Senior Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
Tel: +49-30-521325470
KDAB - The Qt Experts



More information about the Development mailing list