Horrific Java Gem (Revisited)



  • Remember this? I just ran into a rather WTFy usage of it:

      Collection collect = null;
    

    try {
    collect = executeSQLString("<stuff to get the record we want, right?>");
    } catch (Exception sqlError) {
    JOptionPane.showMessageDialog(... "Server returned error" ...);
    return;
    }

    try {
    Object object = null;
    Iterator iterator = collect.iterator();

    while (iterator.hasNext()) {
      object = (Object)iterator.next();
      if((object.getClass().getName().endsWith("OracleResultSetImpl")) {
        ResultSet rs = (ResultSet)object;
        if (rs.next()) {
          // do stuff with result set
        } else {
          JOptionPane.showMessageDialog(... "Item not found in database" ...);
          return;
        }
      } else if ((object.getClass().getName().endsWith("PooledQueryConnection")) {
        Connection connection = (Connection) object;
        connection.close();
      }
    }
    

    } catch (Exception sqlException) {
    JOptionPane.showMessageDialog(... "could not run database query at this time" ...);
    return;
    }


  • Discourse touched me in a no-no place

    Ow. The incoherent Collection is a code stink on its own, but the object.getClass().getName().endsWith("OracleResultSetImpl") is a very special sort of stupid.

    Plus all those dialogs when things go wrong. Users love dialogs giving mysterious messages they can't copy or do anything about, oh yes...



  • @zelmak said:

    collect = executeSQLString("<stuff to get the record we want, right?>");

    Early on WTF before you even get into the meat of the WTF.



  • @zelmak said:

    object = (Object)iterator.next();

    Casting to Object is TRWTF. Or maybe not using foreach. Or iterating over the objects (future-proof in case they add another object to the collection!) but closing the connection as soon as it is reached (not so future-proof any more).

    And I haven't even touched the way this code actually works. Yikes.


Log in to reply