Thursday, December 22, 2011

Account for Every Possible Eventuality

I spent a day this week working on a project which needed some serious housekeeping doing to it. While working adequately for the end user, it was filling our PHP error log with volumes of warnings, primarily associated with referencing unset indexes or variables. In my earlier programming days I was good about accounting for events which I expected to happen in the logical flow through my code, but I often neglected to account for the unexpected. Some of my housekeeping included:

  • Always check that a variable is set if any possibility exists that it might not be, before trying to use it.
  • Declare the default  timezone when using server date/time variables. While it may assume correctly, it creates warnings to remind you to do so.
  • Don't pass empty variables to be processed by methods when it is unnecessary to do so.
  • Consider testing for NULL values and create procedures or breaks in loops for handling that.
It's common sense really, with hindsight, but when you don't know, you don't know. It's a case of live and learn. My appreciation of the usefulness of error logs has grown notably over the past year and that has caused me to lament all of the unnecessary warnings that I was causing. This is what spurred me into action and now my future projects will be all the more watertight.

No comments:

Post a Comment

Please leave useful comments. Constructive criticism welcomed.