On 2024-02-07 09:08, Larry Garfield wrote:

"The right tool for the job" is indeed the strongest argument for lightweight 
exceptions.  It's a tool we lack right now.

I'm thinking not of "DB went away" type issues (Exceptions are already fine there), but 
"requested product not found."  Right now, the options we have are:

...

The first is probably most common, but null (as I go into in the article) 
doesn't tell you anything and leads to mismatch errors.

Exceptions, I'd argue, are just plain wrong in this situation.  (Which means, 
yes, all the frameworks that throw exceptions on route-not-found are doing it 
wrong.)

And the union-enum approach is a bit clunky as it has no native language support, and no 
solid conventions behind it.  This is my preferred approach personally today, but I think 
we can do better.  Even just having this available at all means that "well everyone 
just uses unchecked exceptions" isn't entirely true.  (All three of the above can be 
found in the wild.)


I can add a fourth option for the "get record by ID" that may not be found, that I've seen in the wild:

public function find($id): ProductSet {}

Where the ProductSet is a collection of (in this case) no more than one Product. It's up to the caller to verify there actually is a Product inside and extract it, and it's also the caller's decision whether not finding one is a problem or not.

Of course, in this domain it folded in with using ProductSets to represent more general collections of Products and the resulting set algebra.

Weedpacket

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to