Comment 126 for bug 214366

Revision history for this message
In , Acelists (acelists) wrote :

(In reply to Ulf Zibis from comment #114)
> (In reply to Josiah Bruner [:JosiahOne] from comment #111)
> > No. The idea here is very similar to the common "priming read" concept
> > taught in introductory CS courses.
> In advanced CS practice I learned: No code duplicates, because if there
> comes a change along, one of the duplicates could be overseen.
Exactly.

>
> > newFolderName.Assign(folderName);
> > bool containsChild = true; // no need to preset it to true
> > int counter = 2; // yes, better than starting with 1 as before

But later below the loop I check whether i (counter) > 1 so the variable exists outside the loop and value of 1 is also significant to the code.

So what about this:
    uint32_t i;
    for (i=1;;i++)) {
      newFolderName.Assign(folderName);
      if (i>1) {
        newFolderName.AppendLiteral("(");
        newFolderName.AppendInt(i);
        newFolderName.AppendLiteral(")");
      }
      rv = ContainsChildNamed(newFolderName, &containsChild);
      NS_ENSURE_SUCCESS(rv, rv);
      if (!containsChild)
        break;
    }

Of course, instead of (i>1) test later on we could do !newFolderName.Equals(folderName) but that is probably much more expensive.