Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122352 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 13257 invoked from network); 9 Feb 2024 11:52:11 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Feb 2024 11:52:11 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1707479585; bh=ffcCGBDeVmg/9+JuhUkVvt+Uo1462AnjRwn8yJ8aJ6E=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=mtCcs1qY+pc0gCOlWyjIUSL0cfFT+reNn40eNgEvM8P7sw0wKi2TxYtiyRxwAriPX fuZpPGDNrOtEyC6pHr4+QI/Klugt/ZkbcJT5aLzIyVMALz8tjxzrDZd5kf4xcx0fHJ OB4vusW1TGGneK1xQoiOBQ6PXzpum+dJGU1gfksZ5/hIESp8Eulbgwc7HgCwr92sFM Kfo5reyaTIyiywOh8ga/F8raByi30nylySi8mcexXgqPs3bB5GygwEcY6N8TjbPQNp DkfUD+BuqGWytjF1My74aqdjMFbbh4pHOw9TusHJHd1Jlgz9OZhqtN7sqbBWYXB0ZI LEiIufVkFke5A== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CFDB7180059 for ; Fri, 9 Feb 2024 03:53:04 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-oi1-f182.google.com (mail-oi1-f182.google.com [209.85.167.182]) (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 ; Fri, 9 Feb 2024 03:53:04 -0800 (PST) Received: by mail-oi1-f182.google.com with SMTP id 5614622812f47-3bbb4806f67so524636b6e.3 for ; Fri, 09 Feb 2024 03:52:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707479529; x=1708084329; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=y5wvAboGZjq51MY4lVi4qNcbHHFY18/t1r8yYzJm19U=; b=GhKdjr+ZLgXpi7qhGFZ1RSzTo0fknKonsTvAcc3tF1S06trS2EbLCptCQw+UsfI7gZ 0EdoLQD5CuWY2/N5ishUMfankbBU9efpd1jHTOhQD0WW9hDBmynwsPEQR+18kQYye/p9 McvtA1XocoG+mdVlUddr5A0I8jxVLKZeMK0TUtRdptQP9VEf4b6u+F2rCZbgm0GzbEyW e9MeqSB/Z/QJbBh9klOKKTJyz0tlZ+7E4IQV35slealCJx7eieqjfcuVEXf92q8eb3BT k+JhMnFDPjnSnWF7MtLTgLYcjtS+/1thRRnEx3TeRzUeINpAxuLnMazhX9cluf5y6AT6 NPQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707479529; x=1708084329; h=content-transfer-encoding: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=y5wvAboGZjq51MY4lVi4qNcbHHFY18/t1r8yYzJm19U=; b=VoLLOWtqpy2pArXnE+gEyjro2IU7VzYWJgElH0cklI02sisCDWHRuHAIBORVmGBj/u 3Vst3008TfbfzWYYITxazLQUDPRItnd4Qol+Z2Z4TAhTCz4f9CHoIvM0M5olvYZwgBpK ajyG2jwJQR2tw099BxrJFqe2mqKa6+ZpD/rYwxp4nIYZNTyvaDQD5xYUSRHEiYR+KNRf S2QoVEJTy8dmb2fEr0qp7cg8U69sCM6LLH2CKTNOpmKSBRbVCYlorXZ2Xqj479QK4R/O AfKRTJAFS1pq1rJ6ilT93TYdU2BgscYdWkX570mwSfVSHmo+upcC3Q5ErLuzt0/2kv58 Gx+w== X-Forwarded-Encrypted: i=1; AJvYcCULG1k4Qq0MrZ5NANwtihwTLjkO27edo3ywXPDL5Wpr4Km8Exu9FRwaveNghT03+WMS0PqFZ3qDkWPKRuclWoRXqieSmMsfew== X-Gm-Message-State: AOJu0YxIj6wIzH90zm6kasm1cvzZO0v0EneuPSNOsACt+ajsx6irAipf 99wAdiYnLn95wyOeRQ5o6itK66qy1yW1s10AjRmvw/s46gTMykGipyHLn6nQfnjhEf6ZYdlfa4t On8fyeu8qJHaha2KGY7AiVHGTVXk= X-Google-Smtp-Source: AGHT+IFPuaN3t1DwQGMs9wWmt02nFQvqxKLMJAe9mcM24eT8JtrCfUENdC3CTMbyG7Yk4/rTN79PRQ1KEJOovdN0ZaY= X-Received: by 2002:a05:6871:6a7:b0:219:570f:31db with SMTP id l39-20020a05687106a700b00219570f31dbmr1397374oao.10.1707479529511; Fri, 09 Feb 2024 03:52:09 -0800 (PST) MIME-Version: 1.0 References: <742f202d-7990-4f51-b903-7a15e3fd33c2@app.fastmail.com> <1cd8fa7b-ab62-4563-bf13-bb7801f1ce8d@app.fastmail.com> <90cc47f7-ba80-4798-8447-2f3bb5734eab@app.fastmail.com> In-Reply-To: Date: Fri, 9 Feb 2024 12:51:57 +0100 Message-ID: To: =?UTF-8?B?0JPRgNC40LPQvtGA0LjQuSBTZW5pb3IgUEhQIC8g0KDQsNC30YDQsNCx0L7RgtGH0LjQuiBXZWI=?= <6562680@gmail.com> Cc: Weedpacket , internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Feature request: https://github.com/php/php-src/issues/13301 From: landers.robert@gmail.com (Robert Landers) On Thu, Feb 8, 2024 at 8:57=E2=80=AFPM =D0=93=D1=80=D0=B8=D0=B3=D0=BE=D1=80= =D0=B8=D0=B9 Senior PHP / =D0=A0=D0=B0=D0=B7=D1=80=D0=B0=D0=B1=D0=BE=D1=82= =D1=87=D0=B8=D0=BA Web <6562680@gmail.com> wrote: > > 1. Do some benchmark with > > ``` > > function a($a, $b) > { > return $a + $b; > } > > function aa($a, $b) > { > throw new \Exception(); > > return $a + $b; > } > > $bag =3D []; > > $mt =3D microtime(true); > > for ($i =3D 0; $i < 100000; $i++) { > try { > a(1,2); // float(0.0023398399353027) > // aa(1, 2); // float(0.053406953811646), what 25 times slower? > } catch (\Throwable $e) { > $bag[] =3D $e; > } > } > > var_dump(microtime(true) - $mt); > ``` > > 2. Your function is a closed case then. Any error inside that case disabl= es > the whole function. > So the function should be as small as possible, also possible > repair/fallback/saga functions should be written. > > The OOP idea is "the object prepares himself (from inside), or with > services (from outside)". > Getters/Setters is bad because only the object should know what's happeni= ng > inside. > Using exceptions you have to write super-micro functions and split your > logic to hundreds of methods, because any of them could fall with > exceptions. > And if you write a Facade that calls one function, each from that calls > 10-15 internal functions - exactly that facade should decide what to do > with any occurred exception. > There are cases you CANNOT decide without third-party libraries. So you > can't write facades and you should break the encapsulation by doing small > methods instead of useful ones. > That's why exceptions are not a solution. Exceptions are a validation cas= e > and a safe-shield (reporting to email) for cases you didn't cover because > of a lot of work. It's not actually 25x slower, it's only 15x slower to create an exception. The rest of your overhead is in appending to the array (~1ms aggregate), throwing (~10ms aggregate). This isn't even something to worry about though, in most applications. > Using exceptions you have to write super-micro functions and split your > logic to hundreds of methods, because any of them could fall with > exceptions. I've worked on PHP (and non-PHP) applications with (tens of) millions of lines of code, and I've never run into this problem. You write small functions so they can be reusable, not to handle exceptions. Generally, you don't need to do anything if you rely on types: function add(int|float $left, int|float $right): int|float { return $left + $right; } will throw a TypeError if you call it with strings (at least in strict mode, in non-strict mode, it will be coerced to a number if a numeric string), as it should. You shouldn't even have to catch this because it should be caught in development, and in production, it very much should crash. For larger functions/methods, error conditions should be guarded against through interfaces and types, which will be caught long before production, either in a code review or in a developer environment. Robert Landers Software Engineer Utrecht NL