Funny code

I have been working on a piece of code that was not performing well and produced undesired side effects. At first it did not make sense to me at all, and only few minutes later I realized that the author used a very unusual way to iterate over a collection. That is what it boils down to:

LinkedList<DataItem> list = ...;
for (int i = 0; i < list.size(); i++) {
    DataItem item = list.getFirst();
    // ... do work

The actual code also had an option to iterate in another direction, using addFirst/removeLast pair. I struggled with it because I thought the collection is being manipulated for a good reason - reordered or reorganized according to some logic. The "do work" part was quite complex and had a lot of logic. But that was just a very unusual way to iterate over a collection. Which also lead to nasty side effects if the loop was broken before completion for some reason.

I often say that if you take 2 Java developer and ask them to solve the same trivial problem they would produce virtually identical code. I am often wrong.

Posted On


Tags: / /