CodeSOD: Objectifying Yourself

This post was originally published on this site

The Daily WTF

“Boy, stringly typed data is hard to work with. I wish there were some easier way to work with it!”

This, presumably, is what Gary‘s predecessor said. Followed by, “Wait, I have an idea!”

public static Object createValue(String string) { Object value = parseBoolean(string); if (value != null) { return value; } value = parseInteger(string); if (value != null) { return value; } value = parseDouble(string); if (value != null) { return value; } return string; }

This takes a string, and then tries to parse it, first into a boolean, failing that into an integer, and failing that into a double. Otherwise, it returns the original string.

And it returns an object, which means you still get to guess what’s in there even after this. You just get to guess what it returned, and hope you cast it to the correct type. Which means this almost certainly is

To read the full article click on the 'post' link at the top.