Home > Java, WTF > WTF again

WTF again

Looking at legacy code, someone actually wrote a class that extends ArrayList

public class UserInterfaceModel extends ArrayList {

just because (s)he wanted to find out if a specific element was in the list!

public class UserInterfaceModel extends ArrayList {
    private static final long serialVersionUID = 1L;

    public UserInterfaceModel() {}

    public void addElement(UserInterfaceElement uie) {
        this.add(uie);
    }

    //chek if contains UserInterfaceElement in collection
    public boolean isContainingElement(String name) {
        for (int i=0;i<this.size();i++) {
            if (((UserInterfaceElement)this.get(i)).getName().equalsIgnoreCase(name)) {
                return true;
             }
         }
         return false;
    }
}

public class UserInterfaceElement {

    private String name;
    private String url;
...
}

Instead, the said programmer could just implement the equals method in the UserInterfaceElement element (to check only the name value) and just use a List.

Amzing, just amazing.

Advertisements
Categories: Java, WTF
  1. Elizeu
    16 August 2017 at 9:32 pm

    I was searching for a solution and ended up finding your blog, I had a great time with all the “wtf” posts, unfortunately you stopped posting 😦

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: