Details
-
Sub-task
-
Status: Resolved
-
Minor
-
Resolution: Fixed
-
None
-
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.