user1095108
user1095108

Reputation: 14603

Private methods vs Lambda in C++

My question refers to:

Using a lambda expression versus a private method

Now that lambda functors are part of C++, they could be used to unclutter a class' interface. How does lambda use vs private method use compare in C++? Do there exist better alternatives to unclutter class interfaces?

Upvotes: 7

Views: 3199

Answers (5)

Werner Erasmus
Werner Erasmus

Reputation: 4076

How does lambda use vs private method use compare in C++

One may capture the this pointer, therefore having access to a class's internals without cluttering its interface. Previously, under certain circumstances one would require either a member or a friend function (use in conjunction with bind and mem_fn) in stl algorithms.

Added, one should strive to keep interfaces minimal, as changing them is more costly. For this reason idioms like pimpl are popular, and for the same reason a lambda may be preferred over a member function. As mentioned by another person, one should consider that functions are also tools of reuse, but in that case I would prefer a pimpl with private functions, which negates reuse of lambdas for this purpose.

Upvotes: 3

Cort Ammon
Cort Ammon

Reputation: 10863

Lambda is a way to move logic into the flow of your code. The only purpose is improving readability, and they can help or they can hurt

  • If a private/lambda is short, it is often easier for a reader to comprehend it on the spot, rather than having to remember to look at a different function later.
  • If a private/lambda is long, and appears incongruous with the code calling it, this can distract the reader from a greater pattern in the code. Imagine if you were reading one of these StackOverflow answers and suddenly had to stop to read StackEnglish's description of null and void to make sense of the answer. You'd really rather just see a hyperlink of "optional information" than a copy/paste of the contents.
  • It is far easier to construct a lambda which holds onto upvalue arguments than it is to construct a private functor to do the same. This can increase readability due to lambdas having less boilerplate in accomplishing that goal.
  • lambdas are harder to share between sections of code. If you find yourself calling the same function from many places, private functions may be a better match.

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

Although lambdas can definitely replace some private member functions, thinking of them as means to uncluttering the interface of a class is taking too narrow a view of both lambdas and private member functions.

Private member functions (and functions in general) are the basic units of code reuse. They let you write a piece of logic once, and then hide it behind the name of the function.

Although lambdas can replace a private member function in certain context, they could replace an object of which that function is a member along with it, which is a whole lot more. Due to lambda's ability to capture the context around them, you get a way of creating code blocks that take not only your object with it, but also the state of your local variables. Before lambdas you needed to create a special class for that; lambdas let you create such classes on the fly, for much better readability.

Upvotes: 5

user1708860
user1708860

Reputation: 1753

It has a matter of personal taste in it if you ask me, the difference between the two is not huge.

Lambdas are really good for stl algorithms, while private methods, are not as comftorable for that, on the other hand, i find private methods much better if you want to reuse your code.

This question shows the best usages of lambdas imo : lambdas

Personally, i prefer private methods, espessialy in the case you presented. Lambdas should not be used instead of private methods. It clutters the function's body and diverts the eye from the main logic.

Although it unclutter the class, it clutters the function, which i find worse.

Upvotes: 2

Bret Kuhns
Bret Kuhns

Reputation: 4084

Do there exist better alternatives to unclutter class interfaces?

A method I use quite often is to provide a series of functions with file-scope in an unnamed namespace that act as "helpers" to my member functions.

For example:

// Foobar.cpp
#include "Foobar.h"

namespace {

    void someHelper()
    {
        std::cout << "I'm a helper!" << std::endl;
    }

}


void Foobar::someMemberFunc()
{
    someHelper();
    [...]
}

You can, of course, also declare lambdas and/or classes in this unnamed namespace as well. Anything declared in this unnamed namespace will only be accessible from within the same translation unit (eg, cpp file).

Upvotes: 5

Related Questions