Wednesday, July 25, 2007

Going backwards to get ahead

This is mostly a note to myself for later, but hey, why not make a blog post about it?

I got to thinking about the means I go through to enter a new gas tank into my main array. I basically start at the beginning of the array, compare the new odometer value to the odometer for the tank at that position in the array, and if the new one is higher, go to the next element. Rinse, repeat. So if the new tank has the highest odometer, it will wind up at the end of the array, or if it isn't it will get inserted in the middle somewhere. Since NSArray automatically bumps the indexes for downstream elements if a new object is inserted, this works pretty nicely and isn't that hard.

I've been reading through Wil Shipley's Pimp My Code series, and one underlying theme that emerges is reducing the number of activities a given action requires. Combine that with a general interest in accommodating what users are most likely to do.

So, when I sit down to enter in a new gas tank, where is it probably going to fall in the array? The most recent tanks should have the highest odometer, so towards the end, if not the very end. But my loop starts at the beginning to determine where that tank should be. The more tanks I add, the longer it will take to iterate that loop. I can go upwards of 70 tanks a year, and that's a lot of iterations. So I need to get that flipped around. The loop should start at the end of the array and count backwards. Chances are, it will insert the tank and exit the loop with only a single iteration. Even if I'm entering information out of order, it shouldn't have to count back too far to figure out the right location.

Add that to my list of things to accomplish tonight. I probably won't notice a difference right away, but it's the kind of thing that almost has to pay off down the road.

No comments: