Thou shalt not use any other loop-var than "i"!



  • Hi everybody,

    well, the subject of this posting should be part of the Ten Commandmends for programmers - at least my boss might think it should be. Compiling one of his projects, I stumbled across a C# compiler warning, asking me whether I really wanted to assign to the same variable. Never having seen this warning before, I went to see what the compiler was complaining about - I found the following piece of code:

    int i; 
    if(this.morePages) 
    { 
      i = this.nextRow; 
    } 
    else 
    { 
      i = 0; 
    } 
    

    for (i = i; i < rows; i++)
    {
    ...
    }

    I decided to leave it the way it is - you can never about the side effects...
     



  • I would not have used "i" for anything but the loop counter, and done something like

     

    int i=0; // defined here since I don't know if i gets used after the for() loop!

    for(i = ((this.morePages) ? this.nextRow : 0); i < rows; i++) { ... }



  • WTFisWTF?  The guy is trying to determine the start index for the loop.  Sure, he can name the variable whatever he wants, but i works nicely.  To get rid of the compiler warning, just change the loop to:

    for ( ; i < rows; i++)

    Better yet, use the one line of concise code that JCM provided.



  • Declaring an additional variable does not really cost you that much. :)

    for (int k=i; k<rows; k++) {
        ...

     

    I don't care about these commandments coz often they are not applicable (2 or more loops nested). If you really want to use "i" instead then swap k and i... 



  • [quote user="JCM"]

    I would not have used "i" for anything but the loop counter, and done something like

     

    int i=0; // defined here since I don't know if i gets used after the for() loop!

    for(i = ((this.morePages) ? this.nextRow : 0); i < rows; i++) { ... }

    [/quote]

    Well, I'd not have used for in the first place, but turned it into a while loop. But anyway who am I to doubt the boss...



  • unless i is being used later this should be:

    for (int i = ((this.morePages)?this.nextRow:0); i < rows; i++)

    {

      ... 
    }

     



  • [quote user="JCM"]

    I would not have used "i" for anything but the loop counter, and done something like

     

    int i=0; // defined here since I don't know if i gets used after the for() loop!

    for(i = ((this.morePages) ? this.nextRow : 0); i < rows; i++) { ... }

    [/quote]

     

    DOH! 



  • [quote user="GoatCheez"]

    unless i is being used later this should be:

    for (int i = ((this.morePages)?this.nextRow:0); i < rows; i++)

    {

      ... 
    }

     

    [/quote]

     

    Actually, I think it is clearer to do this.

    final int startRow;

    if (morePages) {
        startRow = nextRow;
    } else {
        startRow = 0;

    for (int i = startRow; i < rows; ++i) {
        ...
    }

     

    Although, from looking at just this snippet, I would probably find plenty of ways to refactor this , like nextRow is probably 0 if not morePages, so you could probably always just have i in [nextRow, rows).

     


Log in to reply