Article 6E28F CodeSOD: Simple Form Validation

CodeSOD: Simple Form Validation

by
Remy Porter
from The Daily WTF on (#6E28F)

"This is so easy, how could someone screw it up?" is a wonderful case of ambiguity in English. Because the question means two things, potentially. The first question is "how (is it possible) that someone could screw it up?" This question is is built upon the assumption that there are limits to how dense people can be, which is a faulty assumption: there are no limits.

The other interpretation is "in what way can someone screw it up?" This is a far more interesting question, as human error is and endless array of fractal snowflakes, no two exactly alike.

For example, Nathan had a co-worker who needed to take data from a text box on a form, and convert it into a numeric type. "Parsing form input" is such a common task, you have to wonder how does someone screw this up:

public static decimal validateFormVersion(decimal inFormVersion){decimal outFormVersion = 0;try{outFormVersion = inFormVersion;}catch (FormatException){ContextErrorCollection.AddToErrorCollection(errorMessage.INVALID_FORM_VERSION + inFormVersion);}return outFormVersion;}

This C# function takes the value from the form, and tries to assign it to a decimal variable. If that works, great, it's a valid number! If it doesn't, we handle the FormatException. It works just fine, except for two little things: first, conversions between strings and numerics doesn't work that way in C#, and second the parameter is already a decimal!

This is a non-converting converter. If there is any error, it happens outside of this function. This function just helpfully assigns a decimal to a decimal and returns the decimal.

How did they screw this up? By writing do-nothing code that doesn't solve the problem they thought they had.

proget-icon.png [Advertisement] Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.
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