Tuesday 23 February 2010

Inappropriate error checking

One interesting anti-pattern I've come across lately is the whole issue of peppering run-time code with things like: if(index >= count), return. Typically this is added to engines to make them more "user friendly", "robust" or "general".

What's wrong with this? Typically, the data that this condition is operating on is a static asset that has been pre-processed by a tool. We're using an expensive run-time check, often for every frame that we do something, to check a static condition that should be done at asset load or compilation time.

Code that can tolerate erroneous data breeds erroneous data. Erroneous data breeds over complicated code. And that is a maintenance and QA nightmare.

Sometimes you just need to throw bad data out as unacceptable. That's actually the most friendly, robust or defensive thing to do.

No comments:

Post a Comment