[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Please don't do this (code fragment)



On Sunday 13 January 2002 10:01 pm, Samuel Tardieu wrote:
> On 13/01, elf@florence.buici.com wrote:
> |   int i;
> |   for (i = 0; i > -1; i += 1) {
> |     // ...
> |     if (terminal_condition)
> |       break;
> |     // ...
> |   }
>
> [...]
>
> | Moreover, i is never used.  The loop could be reduced to
> |
> |   while ((file = fts_read (dir)) != NULL) {
> |     // ...
> |   }
>
> Those are not equivalent: the first loop, while ugly, has a guard against
> endless looping if fts_read always returns non-NULL for any reason (not
> knowing what fts_read contains, it is hard to tell whether there is a
> reason for this or not).
>
but it can still be reduced to a single while loop while retaining the same 
behavior as the original code:

while(file = fts_read(dir)) {
  // ...
}



Reply to: