« QUIZ: Does this code have issues? | Main | Memory Leaks made easy »

SOLUTION: Does this code have issues?

Thank you for all your comments on my previous post.

Non-Issues

Using the ++ increment operator

The usage of the ++ increment operator was done deliberately and is not an issue. The difference between the two is the point in time when the value of the variable is returned. The postfix notation (i++) needs to save the value of i first and then increments. Using the ++i version helps even the dumbest compiler to generate the fastest possible code, while i++ needs at least a second operation. Today's compiler techniques should be good enough to detect that ++i and i++ are the same and the return value of i++ is not used in this situation. Using the prefix notation is more a matter of taste here.

Using Typed Iterators

Using typed Iterators is surely a big advantage in Java 5. I'm a big fan of that style. But this not a real issue.

The Issue

Many of you where correct that the modification of the list will be the issue. If you modify the code in the loop the issue could be easily spotted. It is more difficult to spot the problem if you just iterate through the list to get the values. Code Reviewers should be careful and consider whether the list is concurrently modified. Antony Reynolds provided the best answer here.

Further Reading

  • Java Concurrency in Practice, Brian Goetz
  • Concurrent Programming in Java, 2nd Edition, Doug Lea


Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About This Entry

This page contains a single entry from the blog posted on August 31, 2007 1:36 PM.

The previous post in this blog was QUIZ: Does this code have issues?.

The next post in this blog is Memory Leaks made easy.

Many more can be found on the main index page or by looking through the archives.

Top Tags

Powered by
Movable Type and Oracle