[wp-hackers] Why WP_Error Sucks
Mike Schinkel
mike at newclarity.net
Tue Jul 24 23:01:35 UTC 2012
There are good points on both sides of this issue; exception handling is both useful and a nightmare, depending on how it is used.
Exceptions are very useful when dealing with local complexity in visible scope when external factors result in failed assumptions. Consider the following alternate take on Ryan's example[1]:
https://gist.github.com/3173032
it implies an updated wp_remote_get() that will throw an exception if passed $args['exception'] => true, otherwise it will return a WP_Error() like it currently does. This would only require that functions in core which are likely to fail because of external factors be changed to request an exception thrown instead of returning an error (there are only a few such functions that might need this.) Basically this approach removes the injected dependency which is what I think most people have issue with.
This would be 100% backward compatible, it would address Ryan's reasonable concerns, and it would not infect core with exceptions. If best practices were documented in Codex, i.e. only use WP_Exceptions when everything about the exception's effects are visible in the current function then Codex tends to be rather influential among plugin developers to keep most everyone from _doing_it_wrong().
FWIW.
-Mike
P.S. The example shown doesn't illustrate the level of complexity where exceptions would really help, i.e. with numerous calls to wp_remote_get() or other functions that are likely to fail. Basically anywhere you have to do complex interactions with a web API, FTP server or other web service.
[1] http://journal.ryanmccue.info/165/why-wp_error-sucks/
On Jul 24, 2012, at 12:34 PM, Andrew Nacin wrote:
> On Tue, Jul 24, 2012 at 10:58 AM, Michael D Adams <mda at blogwaffe.com> wrote:
>
>> On Tue, Jul 24, 2012 at 5:56 AM, Ryan McCue <lists at rotorised.com> wrote:
>>> I'd be interested to hear your thoughts on WP_Error and the possibility
>>> of using exceptions as well. I'd also love to hear from anyone who has
>>> implemented exceptions in their own plugin code.
>>
>> Joel Spolsky's opinion:
>> http://www.joelonsoftware.com/items/2003/10/13.html (Exceptions are
>> worse than GOTOs)
>
>
> Ah, yes, I was looking for this link. Thanks, Mike. Anything that Otto, I,
> and others didn't cover, is dealt with there.
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
More information about the wp-hackers
mailing list