user6245072
user6245072

Reputation: 2161

Helper functions: lambdas vs normal functions

I have a function which internally uses some helper functions to keep its body organized and clean. They're very simple (but not always short) (they're more than just 2), and could be easily inlined inside the function's body, but I don't want to do so myself because, as I said, I want to keep that function's body organized.

All those functions need to be passed some arguments by reference and modify them, and I can write them in two ways (just a silly example):

With normal functions:

void helperf1(int &count, int &count2) {
    count += 1;
    count2 += 2;
}

int helperf2 (int &count, int &count2) {
    return (count++) * (count2--);
}

//actual, important function
void myfunc(...) {
    int count = count2 = 0;

    while (...) {
        helperf1(count, count2);
        printf("%d\n", helperf2(count, count2));
    }
}

Or with lambda functions that capture those arguments I explicitly pass in the example above:

void myfunc(...) {
    int count = count2 = 0;

    auto helperf1 = [&count, &count2] () -> void {
        count += 1;
        count2 += 2;
    };

    auto helperf2 = [&count, &count2] () -> int {
        return (count++) * (count2--);
    };

    while (...) {
        helperf1();
        printf("%d\n", helperf2());
    }
}

However, I am not sure on what method I should use. With the first, one, there is the "overhead" of passing the arguments (I think), while with the second those arguments could be (are them?) already included in there so that that "overhead" is removed. But they're still lambda functions which should (I think, again) not be as fast as normal functions.

So what should I do? Use the first method? Use the second one? Or sacrifice readability and just inline them in the main function's body?

Upvotes: 2

Views: 1084

Answers (3)

Hassen Dhia
Hassen Dhia

Reputation: 585

Using Lambda is more readable but they are actually there for more serious reasons , Lambda expressions are also known as "anonymous functions", and are very useful in certain programming paradigms, particularly functional programming, which lambda calculus ( http://en.wikipedia.org/wiki/Lambda_calculus )

Here you can find the goals of using lambdas :
https://dzone.com/articles/why-we-need-lambda-expressions

If you won't need the two helper functions somewhere else in your code, then use your lambda method , but if you will call one of them again somewhere in your project avoid writing them each time as lambdas , you can make a header file called "helpers.(h/hpp)" & a source file called "helper.(c/cpp)" then append all the helper functions there then you gain the readability of both the helper file and the caller file

You can avoid this unskilled habit and challange yourself by writing complex code that you have you read it more than once each time you want to edit it , that increases your programming skills and if you are working in a team , it won't be a problem , use comments , that will let them show more respect to your programming skills (if your complex code is doing the expected behaviour and giving the expected output)

And don't be concerned about performance until you find yourself writing a performance critical algorithm , if not , the difference will be in few milliseconds and the user won't notice it , so you will be loosing you time in an optimization that compiler can do by itself most of the time if you ask him to optimize your code .

Upvotes: 0

GeorgeT
GeorgeT

Reputation: 504

Performance wise, there is no real issue here. Nothing to decide, choose whatever.

But, Lambda expressions won't do you any good for the purpose you want them. They won't make the code any cleaner.

As a matter of fact I believe they will make the code a bit harder to read compared to a nice calculator object having these helper functions as member functions properly named with clean semantics and interface.

Upvotes: 0

chtz
chtz

Reputation: 18827

Your first and foremost concern should be readability (and maintainability)! Which of regular or lambda functions is more readable strongly depends on the given problem (and a bit on the taste of the reader/maintainer).

Don't be concerned about performance until you find that performance actually is an issue! If performance is an issue, start by benchmarking, not by guessing which implementation you think is faster (in many situations compilers are pretty good at optimizing).

Upvotes: 1

Related Questions