Uploaded image for project: 'Lucy'
  1. Lucy
  2. LUCY-144 Refactor away C89 idioms
  3. LUCY-146

Reduce iterator variable scopes

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • None
    • 0.3.1
    • None
    • None

    Description

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

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

      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:

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

      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.

      Attachments

        Activity

          People

            logie Logan J. Bell
            marvin Marvin Humphrey
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: