Question

I was browsing a GNUstep project on github and came across this little loop...

for(; i < 9; i+=1)
{
  for(;j < 9; j+=1)
  {
      /* Inner loop code - snipped */
  }
}

Most, but not all incremental operations were done using '+=1' in the source code; which would make sense if it was consistent. If it was consistent I would put it down to simple personal preference, or even readability.

However, for a project that appears to have been worked upon by one person (it is, after all, a simple sudoku game) the differing style of incremental operations has got me a bit curious.

for(; i<10; i++)
{
  /* inner loop code - snipped */
}

Now I've read this question, and it confirmed my suspicions - The compiler should be generating the same low-level code regardless of the syntax used.

This got me pondering whether it's best practice or style guidelines, but the same coder has used the both methods of incrementing. Personally speaking, I use 'i++' without thinking - and I'm guessing that's natural for any coder, you just type it without thinking almost.

Am I missing something or is this "just because"? Is there any reason why you would mix both types of incrementing? If they both compile down to the same ASM then I would assume there's no situations when one performs better than the other.

Was it helpful?

Solution

Usually this happens when there are several programmers, each with his own coding preferences, and the project doesn't enforce strict coding style.

There is no difference.

OTHER TIPS

In this case they’re equivalent, and you’re right that for any good compiler the assembly generated should be equivalent.

Also note that this isn’t specific to Objective-C. Objective-C is based on C, so it’s exactly the same as in any C code.

The most likely reason, if this is a one man project, is that the programmer simply changed his mind about which style to use half way through.

What if the "one person" found a loop that was achieving what he expected in an other project and copy pasted it lazily without even change the += or ++ (depends on what he uses)?

The only difference that could pop in my mind in the use of one or the other is when going through arrays as pointed in the question you linked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top