« JavaBlog noise | Main | Columbia »

From the 'really bad code' collection..

I should start one.. really.

Today's gem:

public class Stupid { 

   private int _value = 0;

   public void setValue(int value) {
      if (value == _value) {
         _value = value;
      }
   }
}

Duh..huh.. huh.. why are we always seeing the same value?

Comments

What's wrong with being really sure the two values are really equal? This is defensive programming :-)

LOL :-)

Here's a neat one I discovered on Friday. The most recent entry in the CVS log for this file was "Modified getOriginalException() to return 'this' instead of null"

In diagnosing the problem, it helps to keep in mind that this occurs inside a class called WrappedException.

(One day, we'll get to work with JDK1.4, and not have to re-invent wrapped exceptions with every new project... yay.)

public Throwable getOriginalException() {
Throwable e = (originalException == null) ? this : originalException;
if (e instanceof WrappedException) {
return e.getOriginalException();
}

return e;
}

Right! I see the problem - it should be public synchronized void...

Hey, I am sure this source code is protected by patent right. It seems to me as if it is descended from the Microsoft Kernel :)

Hai,darling.Please send the e-mail for me and after we can identify the each other.