0

When working with functions that run a few short "procedures" in sequence that are not used anywhere else and it makes sense to keep it all inline, in a single function, a large part of the code often ends up being separated into pairs of variable declarations, and then certain blocks where said variables are properly setup. As an example:

void Func() {
    SomeStruct1 s1; 
    std::array<float, 3> array1{};
    {
        // set s1 and array1
    }

    SomeStruct2 s2;
    {
        // set s2, uses s1 and array1
    }

    int val1 = 0;
    float val2 = 0;
    {
        // set val1, val2, uses s2
    }
}

This seems suitable, as all I'm really looking for is to setup a few variables, and scope blocks let me keep things relatively safe, minimizing code, while not adding any functions definitions etc. that would add conceptual complexity.

But recently I've considered that perhaps I could take this a step further, and write code in the following way, maintaining code locality but increasing encapsulation even more:

void Func2() {
    const auto [s1, array1] = [] 
    {
        SomeStruct1 s1;
        std::array<float, 3> array1{};
        // set s1 and array1
        return std::make_tuple(std::move(s1), array1);      
    }();

    const SomeStruct2 s2 = [&s1, &array1]
    {
        SomeStruct2 s2;
        // set s2, uses s1 and array1
        return s2;
    }();

    const auto [val1, val2] = [&s2]
    {
        int val1 = 0;
        int val2 = 0;
        // set val1, val2, uses s2
        return std::make_tuple(val1, val2);
    }();
}

On one hand this seems a lot safer, on the other it adds some clutter and it isn't a standard way of coding. If it were possible to restrict scope blocks and perhaps allow exposing certain variables inside it, that would seem even better - this is really just a way to get that sort of behavior with what's possible in c++.

But it makes a lot of sense, and I'm now considering whether this sort of style might not be worth giving a real try - it seems quite reasonable to use it pretty much anywhere (when it comes to sequential code that doesn't seem fitting to break up into whole functions).

Can anyone see any issues with this, due to overusing it, etc.? E.g. situations where replacing scope blocks with this pattern would likely be detrimental? What are the positives/negatives to a style that would heavily favor this method, compared to just sticking with scope blocks?

3
  • 1
    "while not adding any functions definitions etc. that would add conceptual complexity" You've added function definitions. Why do you think moving them outside Func2 would "add conceptual complexity"? The traditional advice would be to split up big functions into smaller functions, which you are doing here (you keep a lexically big function). Commented Oct 19, 2022 at 8:53
  • @Caleth Instead of "definitions" I should have said separate, nonlocal declaration + definition. Keeping code local and un-nested is something I find much more readable, within reason. Splitting things into functions has many benefits, but understanding things is not always one of them. In my example, the blocks are generally ~5-15 lines and I just prefer to keep blocks when it comes to short "procedures" that will not be reused elsewhere. Commented Oct 19, 2022 at 9:10
  • There's a school of thought that calls a 5 line function "long" Commented Oct 19, 2022 at 9:15

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.