Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

When writing code that contains repeated blocks, it is good practice to extract it in an outside function.

C++11 also allows me to extract the code into a lambda, within the function body. Are there any hidden pitfalls to this?

Should I always use external functions, or is it preferable to use lambdas?

Example:

using namespace std;

void to_refactor(const somedata& data)
{
    if (data.has_some_failed_condition())
    {
        ostringstream buffer;
        buffer << "to_refactor: " << data.name;
        buffer << ": " << data.details() << ".";
        throw runtime_error(buffer.str());
    }

    if (other_failed_condition())
    {
        ostringstream buffer;
        buffer << "to_refactor: " << data.name;
        buffer << ": other_failed_condition.";
        throw runtime_error(buffer.str());
    }

    // more stuff
}

Refactoring:

void to_refactor(const somedata& data)
{
    auto throw_runtime_error = 
        [](const string& name, const string& message)
        {
            ostringstream buffer;
            buffer << "to_refactor: " << name;
            buffer << ": " << message << ".";
            throw std::runtime_error(buffer.str());
        };

    if (data.has_some_failed_condition())
        throw_runtime_error(data.name(), data.details());
    if (other_failed_condition())
        throw_runtime_error(data.name(), "other_failed_condition");

    // more stuff
}

TLDR: In the code above, I would have normally ("normally"="before C++11") extracted throw_runtime_error as a function outside of to_refactor. Are there hidden pitfalls to keeping it as a lambda? Is it a good/better practice than using external functions?

share|improve this question
    
This is an interesting idea. I've don't think I've ever seen anonymous methods used like this –  Earlz Feb 15 '13 at 15:24
1  
@Earlz: Then you have never used a more function centric language like haskell or javascript :) –  Aleksandar Feb 15 '13 at 15:27
    
@Earlz: I came up with it because I have a larger function, with two blocks of code that are repeated each, but extracting them outside the function would mean passing lots of parameters into them (and as Aleksandar mentioned, it is a de-facto coding standard in javascript and python). –  utnapistim Feb 15 '13 at 15:29
    
I think it depends on what you're doing. In your example, you're doing something generic you will probably want to do elsewhere and what you don't want is to end up with the same lambda cut and pasted into serveral places. –  parkydr Feb 18 '13 at 16:47
    
@parkydr, perhaps I didn't give the best of examples. I hardcoded the function name in the lambda message in the hope it would be an example of localized code (I tried to show a lambda example that wouldn't be very useful outside the function). –  utnapistim Feb 19 '13 at 9:05

2 Answers 2

I think that using a lambda here makes the code more readable, and if you're not seeing similar patterns in other functions, then it makes sense to do this.

One thing to be careful of is making sure macros like __FUNCTION__ and __LINE__ are passed in as arguments, but you'd need to do that with making a normal function as well.

share|improve this answer

I do this quite a bit when using c++11.
Just be careful, if you find yourself writing the same lambda for different functions, then it's time to create an external function.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.