James Cassell's Blog

Friday, February 22, 2008

Validating Input

When writing code, always validate the input. This may seem like common sense -- and it is under most circumstances -- but in one particular case, it is not. In the case of a computer science class when the instructor guarantees proper input, it is very tempting to not validate the input and simply assume that it is correct. This will save you a little time on each assignment, but is not worth it in the long run. I found this out today. I had a computer science project due at midnight last night. I didn't have it done on time, so I had to waste my remaining late day on it. I spent nearly 24 hours trying to debug my program that should have been working.

When I was debugging, I kept seeing things that could go wrong with improper input, but remembered that I only had to deal with proper input. My program seemed very brittle; the difference between a segmentation fault and the program running fine (but exiting early) was the difference between a break; and a continue; statement. It was at this that I randomly noticed that there was one line of the input was causing the crash. The input was improper despite assurances of the contrary the instructor.

I checked on-line to verify that I didn't mistakenly modify the file. Sure enough, the file on-line was correct, but the date on the on-line file was more recent than the date on my file. Apparently, the teacher noticed (or was informed of) the mistake, and updated the files on-line. What he did not do was send a general notice of the mistake and subsequent correction. Because of all this, I wasted nearly 24 hours of my time as well as a "late day" for turning in homework.

The moral of the story is that one should always check his input even if it has been guaranteed that it will be correct. The benefits of checking the input greatly outweigh the costs. Besides this, it is simply good practice, especially for any code that will be used in production. I had to deal with this when I was writing the contact form for my site; most of my time was spent writing the validation code to prevent any security problems.

Labels: , , ,

0 Comments:

Post a Comment

<< Home