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

Please don't do this (code fragment)



  int i;
  for (i = 0; i > -1; i += 1) {
    // ...
    if (terminal_condition)
      break;	
    // ...
  }


Let me be more explicit.  I'm reading the slocate code to figure out
why it isn't producing a database on my machine.  There is a loop that
reads

  int i;
  // ...
  for (i = 0; i > -1; i+=1) {
    file = fts_read(dir);
    if (!file)
      break;
    // ...
  }		

The code example from the source is even more inappropriate than my
first example since there is no reason to avoid testing the terminal
condition in the loop construct.  While there will be times when it is
difficult to compose a succinct for declaration, this is not one of
those cases.

  int i;
  for (i = 0; (file = ftp_read (dir)) != NULL; ++i) {
    // ...
  }

Moreover, i is never used.  The loop could be reduced to

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



Reply to: