Question on your Code: Why instantiate incrementers as -1 ??

Jul 20, 2012 at 2:18 PM
Edited Jul 20, 2012 at 2:22 PM

I'm thirsty for knowledge, and saw something simple in your code that I'm not able to figure out.

In the rangeToArray() method of your PHPExcel_Worksheet class (and I'm sure elsewhere), the row and column incrementers ($r and $c respectively) are instantiated with a value of -1, then incremented via ++$r and ++$c.

I'm just curious why. Is there something tricky happening here? :)  My first thoughts were that maybe they were validated against at some point so a negative might be needed for failure, but what I'm seeing is just simple use/incrementation.

In all the code I've been exposed to thus far, it has been more common to instantiate to zero, and do a $x++ type incrementation. What is the reason it's been done the way it has in this PHPExcel code?

NOTE: I'm hoping for a more exotic answer than "personal style" so I can learn something new and exciting .. yet "personal style" is my current guess :)

Coordinator
Jul 21, 2012 at 11:04 AM

preincrement is faster and more efficient (albeit only a few bytes of memory) than postincrement.... and in most cases you would never notice, but when there's several millions of iterations overall in a script it does shave a few milliseconds of execution times. When we get as many complaints about execution speed as we do, every little squeeze in performance makes a difference.

Jul 24, 2012 at 8:19 PM

Nice to know, thanks