Tom G. recently joined a team that maintained a fairly large Java client/server application. His first task was fairly simple: prepare for a switch to a different server farm by going through the code to make sure it was portable and wouldn't be affected by a new server, IP address, and so on.

After a few days of browsing through line-after-line of tedious code, Tom found a pretty unique "helper" class: Compare.java. In addition to swathes of unnecessary comparison code, there was equalsAllowNull. Enough was enough, so Tom got up, walked over to his colleague who worked on another project all together, and vented about ridiculousness of Compare.java, and the rest of the code.

//this method will allow me to compare strings and Integers, will be 
//used in all the equals methods in all report components for which 
//i write test cases works like a regular equalsIgnoreCase except:
//  - passing 2 nulls returns true,
//  - passing 1 null 1 non-null string returns false,
//  - passing 2 strings falls back on equalsIgnoreCase()
public static boolean equalsAllowNull(String s1, String s2){
   return equalsAllowNull((Object)s1, (Object)s2);
}
public static boolean equalsAllowNull(Object s1, Object s2){
   if((s1 instanceof String) && (s2 instanceof String)) {
      return (s1 != null)
         ?((s2 != null)
            ?(((String)s1).equalsIgnoreCase(((String)s2)))
            :false)
         :true;
   } else if ((s1 instanceof Integer) && (s2 instanceof Integer)) {
      return (s1 != null)
         ?((s2 != null)
            ?(((Integer)s1).equals(((Integer)s1)))
            :false)
         :true; 
   } else {
      System.err.println(
         "this is an error from equalsAllowNull() method only "
         + "accepts String or Integer, what the hell did you do?! ");
      return false;
   }
}

The developer behind Compare.java must have heard Tom’s rant, as later that day, he sent a message to the team that the code in Compare.java was “tweaked” to be more efficient.

//this method will allow me to compare strings and Integers, will be 
//used in all the equals methods in all report components for which 
// i write test cases works like a regular equalsIgnoreCase except:
//  - passing 2 nulls returns true,
//  - passing 1 null 1 non-null string returns false,
//  - passing 2 strings falls back on equalsIgnoreCase()
public static boolean equalsAllowNull(Integer s1, Integer s2){
   return (s1 != null)
      ?((s2 != null)
         ?(((Integer)s1).equals(((Integer)s1)))
         :false)
      :true;    
}
public static boolean equalsAllowNull(String s1, String s2){    
   System.out.println("strcmp 1:"+s1+" 2:"+s2);
   return (s1 != null)
      ?((s2 != null)
         ?(((String)s1).equalsIgnoreCase(((String)s2)))
         :false)
      :true;
}

Unfortunately, he didn’t hear Tom loud enough.