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

‘This is the real Santa’s workshop’: a trip to Germany’s toy village | Germany holidays

I feel terrible … I’ve left the children at home and Seiffen, nicknamed Spielzeugdorf (The Toy Village), is literally a Christmas wonderland. Every street...

Snapshot: Viaweb, June 1998

January 2012A few hours before the Yahoo acquisition was announced in June 1998 I took a snapshot of Viaweb's site. I thought it might be...

THE 16TH ANNUAL JAMES GARFIELD CHRISTMAS MIRACLE! – The Bloggess

Hello and welcome to the SIXTEENTH ANNUAL James Garfield Miracle! (HOW??) “What is the James Garfield Miracle?” you may be asking. You must be new here. HELLO...

Amal Clooney Wore My Ideal French Outfit Combo in Provence

Amal Clooney certainly has a way of sparking FOMO. Her red carpet style is nothing short of glamorous, while her off-duty outfits are seriously...

Sofia Vergara Is Aging Confidently and Embracing Her 50s

There's also a global double standard around aging that extends beyond Latin American culture. Women are expected not to age, yet they're also judged...