Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128333 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id CBCFE1A00BC for ; Thu, 31 Jul 2025 08:32:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1753950641; bh=D4BsBfDUSHGgTuXMT8KO8DIzQQjtq6v09bzpY+pvzxM=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=YNVxaA40PfbwgUP7dSKNV8UXlrtoGi/kQZdRoImpHMMJvrd+nGc1/sbcVGDx0siaW OZrg4p4CvIKL2ln0CS3XHcB8o/hqQg0j29vy2uc0mOkP9HimpWjRAZwlV9VJ78EClj rDJqVucgL+1SNCoemJ2sx085t8FOD6/9i8ZM+PIRa52FETAjjzk3jf5UGpDt1Alzdd ICn+ukDFa5MegkEw87SG6WNvsY5+j92+c60OWBM0OtBLTbmpx4bfWN59VjlQMfMoCq /KLuYWDU+bH8LiJ2A/69M1b5764Ld7fpBNdrSfLIJYrVIkHTp6rJzU6VbL8i8jEhwL REox/iCN0ECmg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8AD9B1801DB for ; Thu, 31 Jul 2025 08:30:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-lj1-f170.google.com (mail-lj1-f170.google.com [209.85.208.170]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 31 Jul 2025 08:30:40 +0000 (UTC) Received: by mail-lj1-f170.google.com with SMTP id 38308e7fff4ca-3322d10e29dso1018731fa.0 for ; Thu, 31 Jul 2025 01:32:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753950741; x=1754555541; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=ZW0gRxpylfC+vPg2ecxYFJJRp0COemHxRlSPAd5slZo=; b=ihl7/rHFOv4wTy+bTRxpSpB0bVqnmNxGEIu9v8WGhTdiEQ/NtqygOOE2fSfdBdEod+ ol+H8ORTeFuCcXeK9HMYPZKH//S3jVVGgUgHb6WnNMSoekXGyjOTwa/Uv5A4DcFVptOk oyo9/1jYYEhMXaIPAg9Il+BVH6qNzSvy7ldzHGMtWhA2wD0pDRS3joUs4kjsg/N5usJG QQtzw0+ideE/tz+DxFOHfEF4uFEOz46bYEsGldUf2mrRtpcofXQruJNcVouLxc535jSl g28FRQuEqL0//yYS8uAx+cfwMALd2DwOxm8LYmpynZWfvNugKMTNEwA7D54IaLUoelEH 6kSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753950741; x=1754555541; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZW0gRxpylfC+vPg2ecxYFJJRp0COemHxRlSPAd5slZo=; b=EdLdb1IXsrqG4mycAbK/QbxMM2d4XGW5Nsjt/lnctJILtK+QxHvqOO+N6fiGw0rtWE xUeIzW/iiBwXH9nSH678M+J0u7xOuvjK1VNeyZBIua9K1j9wl13mrx8YYTGHHNvjaPmn 0qsR6i5NUA14uVVoZ7VPkkPYDrC+5xjizSRoiU/KCSTIrKyAZ+EKiUrzFkbzcYCFpUu4 yLiolyLyXIPhK1UMfZKdDYmnNI9BZHwjXoiZQNW7QwUnnLqjbqow3LvZSVSUVKxxpPkY 2xc4+6mSKTcqbzuZQuHVyFQc4gQk8kq2SfPVKdeNpWg6SGJyZ9hQKtEc8nyF3+UYlJRV 4dsg== X-Gm-Message-State: AOJu0YzCpglMHr/SUAjRkD1QlaaLSjHPlE0H7ck/TyuUWHU9mEO8nhpV hMzLZxdNm8CA03/vVXkfahbYcnJJUm1zlthKTB47HHv37qdxzYvQUm0jUC3H/FT5BJl3AVV/YKF c2RlT3eNP6hmaFO+AZRmC8214K/CaYQ4m2uoV X-Gm-Gg: ASbGncuoF3LfbGzj5pLtlOKY+taznPWY2ceiW7auzjV6xifY2625jO3peYDiXt21XiY cAKSyfpF7KkOfVhvDIRIIgDVKCsCj71ENvjj5AHtLm3A5NYAuba9cFevGN/U2xTYDfpJvx+8nfG ZaA1g2O5do2f+rdcczl5AMzdExeh9A/9rtsv76StS5Uq2aRtfqFJZGLMMUgNZRdy9UEyDycYXvK SkDiQ== X-Google-Smtp-Source: AGHT+IE5SA/lJ7fQynWffTF+fQna2jokGyBvwwe0ZcQxyoNvKbyjbJcCRGuS4EzsG8clwpYfQLU9kgvYDA3MmCZoU7E= X-Received: by 2002:a05:651c:711:b0:30b:ba06:b6f9 with SMTP id 38308e7fff4ca-33224bdb9ffmr14979131fa.26.1753950740387; Thu, 31 Jul 2025 01:32:20 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <095f25f5-695b-46d4-9f31-5d7e7a04ce05@rwec.co.uk> In-Reply-To: <095f25f5-695b-46d4-9f31-5d7e7a04ce05@rwec.co.uk> Date: Thu, 31 Jul 2025 10:31:54 +0200 X-Gm-Features: Ac12FXz6fH_7ImuY-GHsb50iA68IifO97FKkuRnFqivbOfwGCSMY9H_Egv02x4w Message-ID: Subject: Re: [PHP-DEV] [RFC] Optional Catch Block Body To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000d59d23063b357a88" From: kjarli@gmail.com (Lynn) --000000000000d59d23063b357a88 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Jul 31, 2025 at 10:11=E2=80=AFAM Rowan Tommins [IMSoP] 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 =3D try $this->doSomething() ignore (Foo, Bar); ignore(Foo, Bar) { $foo =3D $this->doSomething(); } catch (Throwable) { // .... } finally { // ... } try ignore(Foo, Bar) { $foo =3D $this->doSomething(); } catch (Throwable) { // ... } finally { // ... } ``` --000000000000d59d23063b357a88 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Thu, Jul 31,= 2025 at 10:11=E2=80=AFAM Rowan Tommins [IMSoP] <imsop.php@rwec.co.uk> wrote:
On 31/07/2025 05:53, Mihail Liahimov w= rote:
> 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 <= br> > important parts.


The "boilerplate" in question is a single saved byte - ";&qu= ot; instead of
"{}". Depending on keyboard layout one or the other might be slig= htly
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 re= ad the intention of it being on purpose, whereas `catch (Foo) {}` I can eas= ily interpret as "the developer forgot to handle the error" or &q= uot;I can't tell if this is a bug or not". This often requires som= ething like:
```php
try {
=C2=A0 =C2=A0 // ..= .
} catch (Foo) {
=C2=A0 =C2=A0 // do nothing
}
```
Now the boilerplate has increased from {} vs ; t= o quite some extra, just to ensure the intention. I don't know if omitt= ing 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 int= ended and/or correct. I do agree that empty catches are usually a sign of t= rouble, but realistically speaking we don't always have control over th= e situation due to legacy or vendor code.

Just tos= sing some ideas out here:
```php
$foo =3D try $this->= ;doSomething() ignore (Foo, Bar);

ignore(Foo, Bar)= {
=C2=A0 =C2=A0 $foo =3D $this->doSomething();
} ca= tch (Throwable) {
=C2=A0 =C2=A0 // ....
} finally {
=C2=A0 =C2=A0 // ...
}
try ignore(Foo, Bar) {
=C2=A0 =C2=A0 $foo =3D $this-&= gt;doSomething();
} catch (Throwable) {
=C2=A0 =C2=A0 /= / ...
} finally {
=C2=A0 =C2=A0 // ...<= /div>
}
```
--000000000000d59d23063b357a88--