home about me

Better iterators with ii & jj

Using ii, jj and kk instead of i, j and k for iterator variables is preferable.

Even better are descriptive names, where possible. Often, that $i variable could just as well be called $blogpostIterator. We’re not in the age of floppy disks 💾 anymore, no need to shave off bytes by sacrificing code readability, unless you’re playing StackExchange Code Golf.

But sometimes, an iterator really is just an iterator, and sometimes they even need to be nested. Turns out simply using ii instead of i makes some things a tiny bit easier with just minimal (1 keystroke) extra effort.

Look at what happens when I select the i variable in this real-world example:

Screenshot of code using i as iterator variable

And look at what happens when I use ii instead:

Screenshot of code using ii as iterator variable

Easier on the eyes, isn’t it?