More Enumeration Fail

I recently came across of some brilliant chunk of WTF-code. The reason was that some swedish users complained about the software not caring about their language settings.

First step was to review the method which stored the settings:

public class DBwriter extends DBaccess {

    public enum Lang {
	DE, EN, FR, NL, SV, IT;
    }

    public void saveLangSetting(long userId, Lang lang) {
	super.executePreparedStatement("INSERT INTO usersettings VALUES ('?','?')", userId,lang.name());
    }
}

Second step was the method which fetched the stored settings:

public class DBreader extends DBaccess {

    public enum Lang {
	DE, EN, FR, NL, ES, IT;
    }

    public Lang getUserLang(long userId) {

	Lang res = Lang.EN;
	try {
	    res = Lang.valueOf(super.executePreparedStatement("SELECT * from usersettings WHERE id=?", userId).getString("lang"));
	} catch (Exception e) {
	    // why should this happen?
	}
	return res;
    }
}

It was a famous moment to realize what was happening there. But it even got better in the moment I found the only usage of getUserLang:

Lang lang = reader.getUserLang(id);
session.setLanguage(lang.name());

I’m just proud the thousand monkeys on their thousand typewriters somehow managed to store some languages successfully.

Leave a Reply