Skip to main content
Tweeted twitter.com/StackSoftEng/status/881819965468409856
added 21 characters in body
Source Link
jwimberley
  • 171
  • 1
  • 6

I'm looking to improve the readability of a lengthy C++ function. This function contains a number (> a dozen) variables that are used throughout. The main logic of the code is a long list of condition checks (sometimes nested, sometimes with loops) followed by various actions. Because of the lengthy number of lines detailing the actions to be performed mixed in with the logical/looping glue, it can be quite hard to follow what's going on. There's no simple way to siphon off these actions into separate functions without arbitrary, ugly function signatures noting whatever subset of variables happen to be needed. One drastic solution might be to replace the function with a singleton class, with the function's variables and sub-actions becoming private member variables and private member. However, I'm looking for a simpler alternative. My idea is to define a list of actions at the top of my function through lambda functions, and then below this perform the function's logic using these one-off lambdas. This would look something like (entirely schematically);

void function() {
    // variables
    int a, b, c, d, ...;

    // actions to be performed
    auto func1 = [&] () { ... code using a,b, etc. };
    auto func2 = [&] () { ... };
    auto func3 = [&] () { ... };
    ...

    // main logic
    if (<condition 1>) {
        if (<condition 2>)
            func1();
        else
            func2();
    } else {
        func2();
        func3();
    }
    ... // etc
}

These lambda functions would occasionally save code, in the the cases where they replace repeated code fragments, but would usually just improve readability -- at least to my eyes. Is this a good practice in general? Do others find that this improves readability, and what is the cost of using these lambda functions?

I'm looking to improve the readability of a lengthy C++ function. This function contains a number (> a dozen) variables that are used throughout. The main logic of the code is a long list of condition checks (sometimes nested, sometimes with loops) followed by various actions. Because of the lengthy number of lines detailing the actions to be performed mixed in with the logical/looping glue, it can be quite hard to follow what's going on. There's no simple way to siphon off these actions into separate functions without arbitrary, ugly function signatures noting whatever subset of variables happen to be needed. One drastic solution might be to replace the function with a singleton class, with the function's variables and sub-actions becoming private member variables and private member. However, I'm looking for a simpler alternative. My idea is to define a list of actions at the top of my function through lambda functions, and then below this perform the function's logic using these one-off lambdas. This would look something like (entirely schematically);

void function() {
    // variables
    int a, b, c, d, ...;

    // actions to be performed
    auto func1 = [&] () { ... };
    auto func2 = [&] () { ... };
    auto func3 = [&] () { ... };
    ...

    // main logic
    if (<condition 1>) {
        if (<condition 2>)
            func1();
        else
            func2();
    } else {
        func2();
        func3();
    }
    ... // etc
}

These lambda functions would occasionally save code, in the the cases where they replace repeated code fragments, but would usually just improve readability -- at least to my eyes. Is this a good practice in general? Do others find that this improves readability, and what is the cost of using these lambda functions?

I'm looking to improve the readability of a lengthy C++ function. This function contains a number (> a dozen) variables that are used throughout. The main logic of the code is a long list of condition checks (sometimes nested, sometimes with loops) followed by various actions. Because of the lengthy number of lines detailing the actions to be performed mixed in with the logical/looping glue, it can be quite hard to follow what's going on. There's no simple way to siphon off these actions into separate functions without arbitrary, ugly function signatures noting whatever subset of variables happen to be needed. One drastic solution might be to replace the function with a singleton class, with the function's variables and sub-actions becoming private member variables and private member. However, I'm looking for a simpler alternative. My idea is to define a list of actions at the top of my function through lambda functions, and then below this perform the function's logic using these one-off lambdas. This would look something like (entirely schematically);

void function() {
    // variables
    int a, b, c, d, ...;

    // actions to be performed
    auto func1 = [&] () { ... code using a,b, etc. };
    auto func2 = [&] () { ... };
    auto func3 = [&] () { ... };
    ...

    // main logic
    if (<condition 1>) {
        if (<condition 2>)
            func1();
        else
            func2();
    } else {
        func2();
        func3();
    }
    ... // etc
}

These lambda functions would occasionally save code, in the the cases where they replace repeated code fragments, but would usually just improve readability -- at least to my eyes. Is this a good practice in general? Do others find that this improves readability, and what is the cost of using these lambda functions?

Source Link
jwimberley
  • 171
  • 1
  • 6

Using lambdas to improve readability of a C++ function

I'm looking to improve the readability of a lengthy C++ function. This function contains a number (> a dozen) variables that are used throughout. The main logic of the code is a long list of condition checks (sometimes nested, sometimes with loops) followed by various actions. Because of the lengthy number of lines detailing the actions to be performed mixed in with the logical/looping glue, it can be quite hard to follow what's going on. There's no simple way to siphon off these actions into separate functions without arbitrary, ugly function signatures noting whatever subset of variables happen to be needed. One drastic solution might be to replace the function with a singleton class, with the function's variables and sub-actions becoming private member variables and private member. However, I'm looking for a simpler alternative. My idea is to define a list of actions at the top of my function through lambda functions, and then below this perform the function's logic using these one-off lambdas. This would look something like (entirely schematically);

void function() {
    // variables
    int a, b, c, d, ...;

    // actions to be performed
    auto func1 = [&] () { ... };
    auto func2 = [&] () { ... };
    auto func3 = [&] () { ... };
    ...

    // main logic
    if (<condition 1>) {
        if (<condition 2>)
            func1();
        else
            func2();
    } else {
        func2();
        func3();
    }
    ... // etc
}

These lambda functions would occasionally save code, in the the cases where they replace repeated code fragments, but would usually just improve readability -- at least to my eyes. Is this a good practice in general? Do others find that this improves readability, and what is the cost of using these lambda functions?