Wednesday, May 21, 2014

Rant: On Error

What I saw

There it was, another function that started with "On Error Resume Next".  The programmer's reasoning was, and here I am paraphrasing so no one Googles it to see who said it, "I know what I want it to do and if the data in the cell doesn't work with the operation, it will just skip it."

Oh...and there was no "On Error GoTo 0".

Lest anyone thinks this was someone with little experience who was suggesting using Resume Next and simply would have not known enough to pair it with a GoTo 0, this person posted his credentials with the comment.

Reaction

Lazy.  Short-sighted.

For several reasons I have an aversion to "On Error Resume Next".  For the most part it is because it opens the code to unexpected results and conflicts of Error handling instructions.  As someone who started life maintaining someone else's code (when we truly cared about length of code because of the machines' limits), code that describes what is going on is helpful when trouble shooting.

That said, when I use  "On Error Resume Next" it is paired with "On Error GoTo 0", wrapping  the code that may cause a problem, thus turning error checking back on.

On Error Resume Next
  <Code that might meet an error>
On Error GoTo 0

No comments:

Post a Comment