Mar 15 2007

Good Coding: A Tip on Error Messages

If you are outputting a variable in your error message that you want to see when doing any future debugging, you might do something like this:


 if ( $tablename_invalid ) { print "Tablename $table is invalid\n"; }

So you would get error messages that read somethink like Tablename Employeee is invalid. That seems all fine and good, yes?

Well, no. Not really.

What happens if you’re function somehow never defines $table? You’ll get Tablename is invalid

That doesn’t seem like a big deal right now, but in 6 months time when you get a message like that, you’re going to have to delve into the code to figure out that your variable is undefined.

Better to write your error message like this:


  if ( $tablename_invalid ) { print "Tablename '$table' is invalid\n"; }

The only change is adding single quotes around the variable name. Now when you trigger the error because the variable is undefined, you’ll get Tablename '' is invalid, which is far more helpful.

Your future self will thank you.

Add a Comment

  Textile Help

<< Evolution Mail - Today's Accidental Discovery

XAMPP - Apache won't start and I just installed SVN >>