Luftballons Hannover

Today's snippet needs very little introduction. In the words of the submitter:

[My predecessor] is what I would consider, among the worst programmers in the world. While his programs actually do work and do what they should, his techniques and programming decisions are very questionable. The [below] code snippet is from a program he wrote after he spend about a year at this company.

The function had one goal: validate a pair of textboxes to ensure they each contain a date, usually in a format like "12 2012" for December 2012. It demonstrates the kind of short-sightedness that usually gets ground out of a developer inside of their first year, like the impulse to test only the happy path through the code and not any possible error conditions. It's generally best to assume your users are malicious idiots who will type things like "none" or "99 luftballons" instead of a proper date.

If that were all he did, though, this wouldn't be worthy of TDWTF. Have a look-see:

private void button1_Click(object sender, EventArgs e)
{
	int digitornot = 0; //'Tis a digit or not?

	if ((textBox1.Text.Length == 1) || (textBox1.Text.Length == 2) || (textBox2.Text.Length == 4))
	{ 
		foreach (char x in textBox1.Text + textBox2.Text)
		{
			if (Char.IsDigit(x))
			{
				digitornot = 1;
			}
		}

		if (digitornot == 1)
		{
			Program.Month = Convert.ToInt16(textBox1.Text);
			if ((Program.Month > 0) && (Program.Month < 13))
			{
				Program.Year = Convert.ToInt16(textBox2.Text);
				if ((Program.Year > 2000) && (Program.Year < 2050))
				{
					this.Close();
				}
				else
				{
					MessageBox.Show("Wrong input!\r\nCheck format!");
				}
			}
			else
			{
				MessageBox.Show("Wrong input!\r\nCheck format!");
			}
		}
		else
		{
			MessageBox.Show("Wrong input!\r\nCheck format!");
		}
	}
	else
	{
		MessageBox.Show("Wrong input!\r\nCheck format!");
	}
}

Not only did the author use integers when a boolean would be more appropriate, he also neglected to name any of his input fields, making maintenance a nightmare. The check for digits will allow all kinds of crud through, which will then crash the program when it tries to convert non-integers to Int16. The submitter has no idea why the year is being compared to 2050. Presumably, the Rapture will happen before then, so no future dates need be considered beyond that point.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!