local lambdas: request for coding standard clarification/judgement call

Do local lambdas get named like variables or like functions?

E.g.

void foo() {
auto helper = {…};
// or
auto Helper = {…};
}

My gut is that it should be lowercase (named like a function) since I got a weird feeling in my stomach seeing an upper-case name being called like a function in new code.

– Sean Silva

Do local lambdas get named like variables or like functions?

E.g.

void foo() {
  auto helper = [](...){...};

It should be "help" here (verb), not "helper" (noun), but this is the direction I like. It's a local function!

Because lambdas can be stateful, they are not simple functions. I see lambdas as a shorthand for writing out a functor.

I would write “Helper” because it is an object of the closure type while the function itself is “operator()”.

Because lambdas can be stateful, they are not simple functions.

Functions are also stateful; C++ isn't a functional language.

I see lambdas as a shorthand for writing out a functor.

Right.

I think the question here is whether instances of functors should
be treated as functions or variables.

I would write "Helper" because it is an object of the closure type while the function itself is "operator()".

IMO, it's clearer if callable types (whether they be functions,
member functions, function pointers, or functors) share coding
style.

I think either choice should be clarified in the coding standards.

Because lambdas can be stateful, they are not simple functions. I see
lambdas as a shorthand for writing out a functor.

I would write "Helper" because it is an object of the closure type while
the function itself is "operator()".

I agree with your analysis about "what is a lambda", but I'm not sure it
really captures the intuitive aspect in a lot of cases. The original thing
that got me thinking was a piece of code like this:

  auto eat = [this](TokenKind K, int AdvancePast = 1){
    CurKind = K;
    Buf.advance(AdvancePast);
  };
  switch (Buf.peek()) {
  case ';':
    eat(TokenKind::Semicolon);
    return;

Here the lambda is definitely a verb, as opposed to the helper/Helper
scenario which is a noun, and it seems like verbs should be lowercase. One
possibility is that lambdas are upper-case or lower-case depending on
whether they are verbs or nouns (and cross your fingers that you aren't the
person who writes the clang-tidy check for this convention!).

-- Sean ilva

>
> Do local lambdas get named like variables or like functions?
>
> E.g.
>
> void foo() {
> auto helper = [](...){...};

It should be "help" here (verb), not "helper" (noun), but this is the
direction I like. It's a local function!

I didn't mean anything by the name here besides the case; I should have
just written "bar". I think David's point about the lambda being the object
of closure type while the function itself is "operator()" could be used to
argue for naming it as a noun, although I expect that most local lambdas
will more naturally be verbs (the original case which got me to write this
post was a local lambda called "eat").

-- Sean Silva