Thursday, June 29, 2023

Horror code: Double salto

 This article was posted originally on JRoller on February 24, 2012

A colleague noticed this code yesterday, which is the Java equivalent of jumping a double salto, then falling back at the same place from where you started:

  private List allHosts;

  public void myMethod(Host theHost) {
    Host myHost = allHosts.get(allHosts.indexOf(theHost));
    ...
  }

I did not put much context in my original post. The Host object was not comparable, so the indexOf operation will look for the object by reference. It would then retrieve the same Host object passed as a parameter. Since Host objects were always to be found in the list, and Exceptions were not even handled, we removed the line, and the program just worked as before.

No comments:

Post a Comment