4 ms·
I used to be in exception camp, but I have lately turned to the anti-exception camp. Exceptions have a nasty way of polluting your architecture, they live ortho
by fetbaffe 7y ago
I used to be in exception camp, but I have lately turned to the anti-exception camp. Exceptions have a nasty way of polluting your architecture, they live orthogonal to the rest of the system but still part of the API and have a tendency to mess with every part in every layer & module.
With exceptions is it easy to end up in a situation where your model code changes UI behavior. Let say your model throws ItemNotFoundExcpetion, are you going to catch that in your global handler & return a 404? The proper way is probably to catch it in your controller & rethrow a ResourceNotFoundException to the UI. Thus you need to do that for every exception, catch & rethrow.
And not only you need to translate exceptions between layers & you probably want to translate exceptions between modules. The number of exception classes in your project starts to explode. You have to create hierarchies and groups of exceptions. And many of them are just copies of others.
In a PHP project I experimented with a similar approach as Go, unfortunately PHP does not a have good tuple type support. You can return an array from a function, however the items will be untyped so you have to rely on phpdoc.
What I did instead was to use an out variable for the error.
function parseInt(string $value, ?string &$error): ?int;
$int = parseInt('10', $error);
if ($int === null) {
echo $error;
}
Worked pretty okay. Still undecided if this is a good approach.
- jrimbault 7y agoIt's a classic approach in some C codebases (Sun I think used it extensively ?). My gripe with it is (only for php), it's not visible from the calling side that we are passing a pointer.
- fetbaffe 7y agoAgree, lets hope this rfc gets approved. https://wiki.php.net/rfc/explicit_send_by_ref https://wiki.php.net/rfc/explicit_send_by_ref https://externals.io/message/108699 https://externals.io/message/108699