CodeSOD: Objectifying Yourself

Date:

Share:

"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 called like this:

boolean myBoolField = (Boolean)createValue(someStringContainingABool);

Which makes the whole thing useless, which is fun.

Gary found this code in a "long since abandoned" project, and I can't imagine why it ended up getting abandoned.

[Advertisement]
Keep all your packages and Docker containers in one place, scan for vulnerabilities, and control who can access different feeds. ProGet installs in minutes and has a powerful free version with a lot of great features that you can upgrade when ready.Learn more.

Source link

Subscribe to our magazine

━ more like this

Authors & Innovators 2025 – Feld Thoughts

I love to read. I love everything about books. LLMs will not replace good writing anytime soon, although they have mastered the art of...

25 Womens Fall Jackets: Trench, Leather, Teddy, Puffer

Whether you want something to throw on for early-morning dog walks or a more buttoned-up (excuse the pun) style for work or evening plans,...

Clockwise #628: You Always Have a Lot To Say

Support this show Enjoy Clockwise Unwound: Ad-free episodes and an extra Overtime topic every week. #628: You Always Have a Lot To Say October 22nd, 2025 · 29 minutes Voice...