The State of the Arts

by in Error'd on

Daniel D. humblebrags that he can spell. "Ordering is easy, but alphabet is hard. Anyway for this developer it was. Can anyone spot which sorting algo they used?" Next he'll probably rub it in that he can actually read unlike the TDWTF staff. I guess we'll never know.


Tern on the Flames

by in Representative Line on

There's nothing inherently wrong with the ternary operator. It's just the kind of thing that gets abused.

Now, we all know how it should be used. We frequently would write something like this:


Currency Format

by in CodeSOD on

"Dark Horse" inherited some PHP code. They had a hundred lines to submit, but only sent in a dozen- which is fine, as the dozen lines tell us what the other hundred look like.

$suite_1_1 = number_format($item -> {'suite_1_1_'.$the_currency}, 2, '.', '');
$suite_1_2 = number_format($item -> {'suite_1_2_'.$the_currency}, 2, '.', '');
$suite_1_3 = number_format($item -> {'suite_1_3_'.$the_currency}, 2, '.', '');
$suite_1_4 = number_format($item -> {'suite_1_4_'.$the_currency}, 2, '.', '');

$suite_2_1 = number_format($item -> {'suite_2_1_'.$the_currency}, 2, '.', '');
$suite_2_2 = number_format($item -> {'suite_2_2_'.$the_currency}, 2, '.', '');
$suite_2_3 = number_format($item -> {'suite_2_3_'.$the_currency}, 2, '.', '');
$suite_2_4 = number_format($item -> {'suite_2_4_'.$the_currency}, 2, '.', '');

$suite_3_1 = number_format($item -> {'suite_3_1_'.$the_currency}, 2, '.', '');
$suite_3_2 = number_format($item -> {'suite_3_2_'.$the_currency}, 2, '.', '');
$suite_3_3 = number_format($item -> {'suite_3_3_'.$the_currency}, 2, '.', '');
$suite_3_4 = number_format($item -> {'suite_3_4_'.$the_currency}, 2, '.', '');

Required Requirements

by in CodeSOD on

Sean was supporting a web application which, as many do, had required form fields for the user to fill out. The team wanted to ensure that the required fields were marked by an "*", as you do. Now, there are a lot of ways to potentially accomplish the goal, especially given that the forms are static and the fields are known well ahead of time.

The obvious answer is just including the asterisk directly in the HTML: <label for="myInput">My Input(*)</label>: <input…>. But what if the field requirements change! You'll need to update every field label, potentially. So someone hit upon the "brillant" idea of tracking the names of the fields and their validation requirements in the database. That way, they could output that information when they rendered the page.


Catch or Else

by in CodeSOD on

Today's anonymous submitter asks a question: "How do you imagine the rest of the codebase to be like?"

Well, let's look at this snippet of TypeScript and think about it:


Planing ahead

by in Error'd on

Yes, the title misspelling was an intentional attempt at punnery. It's a compulsion, I'm sorry.

I might have advised Adam R. not to try to plan flights 4 years in advance, if asked. But he didn't ask, and so he discovered this. I'll let Adam explain.
"I was looking at flights to a certain town in eastern Western Australia. (I'm planning ahead for the July 2028 solar eclipse.) A popular travel website informed me that this evening flight across the border from Northern Territory was an overnight flight. Yes, the time zones in Australia are weird, with a 90-minute difference between WA and NT, but a westbound flight that lands before it takes off should not be an unexpected edge case. "


Location Chooser

by in CodeSOD on

Today's anonymous submitter inherited an application with a huge list of bugs and feature requests for missing features. While tracking down a bug, our submitter learned a lot about why "Allow additional stores to be viewable in the store selector," was an unimplemented feature.

if (inv.inv_storeloc == 0) {
	out.println("<option selected value=\"0\">Select</option>");
	out.println("<option value=\"1\">Location 1</option>");
	out.println("<option value=\"2\">Location 2</option>");
} else if (inv.inv_storeloc == 1) {
	out.println("<option selected value=\"1\">Location 1</option>");
	out.println("<option value=\"2\">Location 2</option>");
} else {
	out.println("<option value=\"1\">Location 1</option>");
	out.println("<option selected value=\"2\">Location 2</option>");
}


Yes, No, NO NO NO NO

by in CodeSOD on

Mike was doing work for a mobile services provider. He found this in their code:

private static YesNoType toYesNo(String isYes)
{
		if (isYes != null)
		{
				if (isYes.equalsIgnoreCase("Y"))
				{
						return YesNoType.fromString("Yes");
				}
				else
				{
						return YesNoType.fromString("No");
				}
		}
		else
		{
				return YesNoType.fromString("No");
		}
}

/**
 * @param isYes
 * @return
 */
private static YesNoType toYesNo(boolean isYes)
{
		if (isYes)
		{
				return YesNoType.fromString("Yes");
		}
		else
		{
				return YesNoType.fromString("No");
		}
}

/**
 * @param isYes
 * @return
 */
private static String fromYesNo(YesNoType isYes)
{
		if (isYes != null)
		{
				String resultStr = isYes.toString();
				if (resultStr.equalsIgnoreCase("Yes"))
				{
						return ("Yes");
				}
				else
				{
						return ("No");
				}
		}
		else
		{
				return ("No");
		}
}

/**
 * @param isYes
 * @return
 */
private static boolean isYesNo(YesNoType isYes)
{
	boolean isBroadbandUser =  false;
	if (isYes != null && isYes.toString().equalsIgnoreCase("Yes"))
	{
		isBroadbandUser = true;
	}
	return isBroadbandUser;
}

Archives