Article FNTP CodeSOD: You've Got My Number

CodeSOD: You've Got My Number

by
Jane Bailey
from The Daily WTF on (#FNTP)

256px-Luftballons_Hannover.JPG

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 2000) && (Program.Year 

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.

puppetlabs50.png[Advertisement] Manage IT infrastructure as code across all environments with Puppet. Puppet Enterprise now offers more control and insight, with role-based access control, activity logging and all-new Puppet Apps. Start your free trial today! TheDailyWtf?d=yIl2AUoC8zAyjLjY2knFdo
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments