开发者

Using same name for kind of variable commonly re-occurring in a nested scope

开发者 https://www.devze.com 2023-03-13 14:26 出处:网络
I often write stuff like: const auto end = some_container.end(); for( auto it = some_container.begin(); it != end; ++it )

I often write stuff like:

const auto end = some_container.end();
for( auto it = some_container.begin(); it != end; ++it )
{
    const auto &item_container = *it;
    const auto end = item_container.end()
    for( auto it = item_conta开发者_如何学Pythoniner.begin(); it != end; ++it )
    {
        do_awesome_stuff_with_the_iterator();
    }
}

Except for the name of the second variable end and it, which up until now, I have given different names. Is it bad style/practice to "reuse" the same name for another variable in a sub-scope? I understand you won't be able to access the outer end and it variables, but that's not necessary. I don't think this is confusing (strange suffixed names are uglier in my eyes), but is there a concrete reason not to do this?


Since you have this tagged c++0x, why don't you simply use std::foreach() with a lambda function?

I don't have a lambda-aware compiler handy, but something like this should do:

// Beware, brain-compiled code ahead!
std::foreach( some_container.begin()
            , some_container.end()
            , [](some_element& v) {
              std::foreach( v.begin()
                          , v.end()
                          , [](another_element& u) {
                            do_awesome_stuff_with_the_element(u);
                          }
            }
            );

With the new range-based for loop, however, this could become even more readable:

// Beware, brain-compiled code ahead!
for (auto& v: some_container)
    for(auto& u : v)
        do_awesome_stuff_with_the_element(u);


The only problem with reusing variable names in your case as you said is that you don't have access to outer scope variables.

And not in your case but it realy may reduce code readabilty if you use variables with same names for diffrent purposes (in your case it's not the problem since both end and it work in a same way).

There is also a third problem that variables with same name often make debuging process difficult, although if you only use logging and not debugger tools it may not be your concern.


It depends on context. In this case, I would suggest that reusing it is questionable, but end is probably okay. You use each instance of end in only one, unambiguous location. But somebody reading the inner loop might get confused by which it you mean, since the use is more removed from the declaration.

Of course, somebody reading either loop might get confused by what it means to begin with; I would chose a better name from the start, and then reusing them should be a more rare occurance.


I think reusing the same variable names is a sign the the names are not descriptive enough for that function.

If you are going to be iterating over multiple containers, the variable names should indicate what they are iterating over, either related to the container or the types contained in the container.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号