On Thu, Jul 31, 2025 at 10:11 AM Rowan Tommins [IMSoP] <imsop....@rwec.co.uk> wrote:
> On 31/07/2025 05:53, Mihail Liahimov wrote: > > Reduced Boilerplate: Eliminates unnecessary empty blocks when > > exceptions only need to be caught and ignored. > > Improved Readability: Makes the code more concise and focuses on the > > important parts. > > > The "boilerplate" in question is a single saved byte - ";" instead of > "{}". Depending on keyboard layout one or the other might be slightly > easier to type. Readability is always subjective, but I think most PHP > programmers would quite easily read "{}" as "do nothing". > > I suspect the real motivation here is that *coding standards* require > more boilerplate than the language in this case, such as a newline > between the opening and closing brace. > > But PHP is not Python, where whitespace is enforced by the compiler; nor > is it Go, where a coding standard is part of the standard library; so > this is not the right place to change coding standards. Even if we add > the short-hand syntax, there is nothing to stop those coding standards > forbidding its use. > > As others have said, good uses of empty catch blocks are rare - > generally, you want to at least log that the exception happened, and > where it was caught. And even those which are functionally empty often > benefit from a comment explaining why they're not doing anything, which > sits naturally between the braces. > > If for some reason your project is commonly using completely empty catch > blocks, you can document where and why "catch(Foo){}" is allowed in your > local coding standard. > > > -- > Rowan Tommins > [IMSoP] > When I read `catch (Foo);` I read the intention of it being on purpose, whereas `catch (Foo) {}` I can easily interpret as "the developer forgot to handle the error" or "I can't tell if this is a bug or not". This often requires something like: ```php try { // ... } catch (Foo) { // do nothing } ``` Now the boilerplate has increased from {} vs ; to quite some extra, just to ensure the intention. I don't know if omitting the body for this is the solution, or if there's something else we can do like: `ignore(Foo, Bar)`. When I read the code I want the intention to be clear, and an empty body just makes me doubt whether or not it is intended and/or correct. I do agree that empty catches are usually a sign of trouble, but realistically speaking we don't always have control over the situation due to legacy or vendor code. Just tossing some ideas out here: ```php $foo = try $this->doSomething() ignore (Foo, Bar); ignore(Foo, Bar) { $foo = $this->doSomething(); } catch (Throwable) { // .... } finally { // ... } try ignore(Foo, Bar) { $foo = $this->doSomething(); } catch (Throwable) { // ... } finally { // ... } ```