The Daily WTF
Mark sends us a very simple Java function which has the job of parsing an integer from a string. Now, you might say, “But Java has a built in for that, Integer.parseInt,” and have I got good news for you: they actually used it. It’s just everything else they did wrong.
private int makeInteger(String s) { int i=0; try { Integer.parseInt(s); } catch (NumberFormatException e) { i=0; return i; } i=Integer.parseInt(s); return i; }
This function is really the story of variable i, the most useless variable ever. It’s doing its best, but there’s just nothing for it to do here.
We start by setting i to zero. Then we attempt to parse the integer, and do nothing with the result. If it fails, we set i to zero again, just for fun, and then return i. Why not just return 0? Because then what would poor i get
To read the full article click on the 'post' link at the top.