[wp-hackers] Why WP_Error Sucks

Otto otto at ottodestruct.com
Tue Jul 24 14:15:50 UTC 2012


On Tue, Jul 24, 2012 at 8:43 AM, Ryan WP Mailing Lists
<ryan.wpmailinglists at gmail.com> wrote:
> As a caregivers from my .net programming I use and am comfortable with the
> concept of exceptions. WP_Error on the otherhand I just don't quite get. In
> fact I admit I don't implement it in my plugins, not that it matters since
> I have never made a pouting for public release but still.
>
> I like the idea and honestly now that PHP supports it with version 5 why
> not as it is the dare I say "correct" method of error handling.


Okay, I would let this go, but... argh. "Correctness" is relative.

IMO, Exceptions suck rocks. This applies to every language they're
implemented in.

The base problem I have with Exceptions is that they should be used
for error handling, but what they invariably end up being used for is
program logic.

Exceptions, when you break it down, are a way to have a return value
from a function that is outside the normal scope of what the function
returns. If a function is supposed to return an integer value, but
something happens where it needs to signal the calling code something
else, then there's two ways to handle it:
a) special return values - this is harder in a strongly typed language
where the return value must fit into an int, for example. PHP is not
such a language.
b) Exceptions, where you "throw" an error condition and expect the
calling code to wrap in a try/catch to handle that special condition.

With a, at least you can define things as such that the calling code
will have to properly handle them. With exceptions, the calling code
can optionally handle the problem or just pass the Exception up the
call stack.

What this generally means is that you have some lazy coder ignoring
the Exceptions, and then a higher level code doing try{ } catch (
Exception e ) { } to just catch them all like some demented Pokemon
cartoon.

Furthermore, Exceptions should be exceptional, meaning that they
should be errors, basically. If I call a function and it throws an
Exception, then I'm presumably expected to handle that exception case
in some manner, to resolve the error. However, if I could have done
that, then I probably should have done it to begin with, and if I was
capable of eliminating the error possibility, then I don't need to
handle the exception case anyway. Meaning that all *real* Exceptions
in the world end up going back up the call stack and just presenting
some error to the user. So when Exceptions are useful, then they're
useless. By design.

While you can attribute a lot of this to shitty programmers, I think
Exceptions themselves encourage this sort of poor and lazy coding,
honestly. They don't encourage "defensive" coding, by which I mean to
write your code so that the error conditions are handled from the
actual program logic. Instead, the existence of Exceptions and
exception handling encourages developers to simply catch "expected"
exceptions and then ignore them in order to make the compiler happy.
This leads to poor code, difficulty of debugging, and general
crappiness. Look at virtually all Java code. Most .NET code is worse,
but harder to find anybody using (bad Java is somewhat everywhere).

This comes about mostly because Exceptions intentionally break
encapsulation. In order to handle an exception properly, I need to
know something about the implementation of the code that threw the
exception to begin with. Poor object oriented design.

So yeah, I hate 'em. Especially in PHP where we're not limited to
strict typed return values. I can return an int, a string, an object,
whatever I like. As long as it's defined well, and it's easy for the
calling code to discriminate between the expected return value vs a
WP_Error object, then Exceptions don't offer me much except for that
breaking of encapsulation, which I don't see as necessarily a good
thing.

-Otto


More information about the wp-hackers mailing list