[cmake-developers] [ctest][coding-style] for-loop local variable usage

Alexander Neundorf neundorf at kde.org
Sun Jun 1 17:27:29 EDT 2008


On Saturday 31 May 2008, Maik Beckmann wrote:
> Hello,
>
> Call me paranoid, but I've been bitten by non local loop variables several
> times and thus I get nervous when seeing something like this
> {{{
>   int cc; // <- declared here
>   values.resize(last);
>   for ( cc = 0; cc < last; ++ cc ) // <- cc used here
>     {
>     values[cc] = 0;
>     }
>
>   for(size_t i=0; i < args.size(); ++i) // how i like it
>     {
>     if ( state > 0 && state < last )
>       {
>       values[state] = args[i].c_str();
>       cmCTestLog(this->CTest, DEBUG, "Set " << strings[state] << " to "
>         << args[i].c_str() << std::endl);
>       state = 0;
>       }
>     else
>       {
>       bool found = false;
>       for ( cc = 0; cc < last; ++ cc )  // <- cc used again
>         {
>         if ( strings[cc] && args[i] == strings[cc] )
> ...
> }}}
> This really alarm my spider sensors.
>
> Sometimes variables having a higher scope are necessary , i.e. when doing
> some fancy search algorithms.  But if isn't need I like to patch it aways
> to avoid upcoming trouble when modifying such code.

I think there is no fixed rule for that in the CMake/Kitware coding style, so 
it is up to the individual developer how to write that.

Alex



More information about the cmake-developers mailing list