Examples of both beautiful and ugly java code?

Posted by tputkonen on Stack Overflow See other posts from Stack Overflow or by tputkonen
Published on 2010-04-09T19:40:20Z Indexed on 2010/04/09 19:43 UTC
Read the original article Hit count: 197

Filed under:
|

I would like to demonstrate how difficult it is for a layman to identify high quality code from flawed code.

I'm thinking of doing this with the help of two java methods. Both of the methods should look like they do the same, pretty simple thing. However one of them should have several kind of flaws, for example:

  • iteration with array off by one error
  • string concatenations causing lots of objects to be created (as opposed to StringBuffer in the "good" code, which looks more complicated)
  • possibly null pointer exception (but it should not be trivial to spot)

Those are just some examples, all kinds of other issues including bugs and performance related structures are highly appreciated.

Methods should be around 10-20 lines of length, and the task they do should be something simple - preferably printing something in an iteration.

© Stack Overflow or respective owner

Related posts about java

Related posts about coding-style