Reduce iterator variable scopes
-------------------------------

                 Key: LUCY-146
                 URL: https://issues.apache.org/jira/browse/LUCY-146
             Project: Lucy
          Issue Type: Sub-task
            Reporter: Marvin Humphrey
            Priority: Minor


C99 allows you to declare iterator variables within "for" loops, but C89 does
not:

{noformat}
    // C99
    for (int i = 0, max = 10; i < max; i++) {
        // ...
    }

    /* C89 */
    int i, max;
    for (i = 0, max = 10; i < max; i++) {
        /* ... */
    }
{noformat}

The C99 idiom is superior because it limits the scope of the iterator
variable.  

We have a number of C89-style iterator variables which should be switched
over.  In some cases, the iterator variable is declared at the top of a scope
and then reused within several loops.  That's the sort of pattern we'd like to
avoid.

The following crude grep command detects some candidates which ought to be
reviewed:

{noformat}
grep -r "for ([a-z] =" lucyalt | grep -v -e "svn\|charmonizer\|modules\|\.pm"
{noformat}

Care must be taken when performing this refactoring, because some code may
in fact be *relying* on iterator variables holding values across multiple
loops -- such as in PriorityQueue.c.  It will be good when this refactoring
pass is finished because such code will stand out more.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to