Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110117 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 58354 invoked from network); 10 May 2020 21:24:02 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 10 May 2020 21:24:02 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 532C31804CB for ; Sun, 10 May 2020 13:00:18 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RDNS_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS701 96.241.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from nebula.zort.net (unknown [96.241.205.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 10 May 2020 13:00:17 -0700 (PDT) Received: from [10.0.1.2] (pulsar.zort.net [96.241.205.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by nebula.zort.net (Postfix) with ESMTPSA id 5ED36200918D0; Sun, 10 May 2020 16:00:16 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.11.0 nebula.zort.net 5ED36200918D0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zort.net; s=zort; t=1589140816; bh=w2whiaagqbJ0X7rHdX4h2D/MMjsBfBgaDt6+7b29wdc=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=tdnWsqoK2C+CQSYTLaG52Qs3+qjz4QdBigE7v6tYzqZkNyKnYcYoJ6Atyat1qtoJ+ kigtLeDjcMznNb1oPDYEw2H1/qNqa4cMXg/j7GOx8PKUcxkKkC/rabkFUJ8Q7wAUfB kMcW++/5X18uj0j9RoODAuEtNUKF53MV0aHlE0gw= Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.80.23.2.2\)) In-Reply-To: Date: Sun, 10 May 2020 16:00:16 -0400 Cc: Ralph Schindler , PHP internals Content-Transfer-Encoding: quoted-printable Message-ID: <5780912D-2926-4381-8B40-FF4353140014@zort.net> References: To: Benas IML X-Mailer: Apple Mail (2.3608.80.23.2.2) Subject: Re: [PHP-DEV] Proposal For Return-If / Early Return / Guard Clause Syntax From: jbafford@zort.net (John Bafford) Benas, > On May 10, 2020, at 15:19, Benas IML = wrote: >=20 > Hello, >=20 > I think that we SHOULD not introduce a new keyword (e. g. guard) = since that > would be a "major major" backwards incompatibility. "Guard" is a = really generic > word and a great example of that is Laravel and their authentication = guards. >=20 > In general, I don't think that early returns require a seperate syntax = and/or > block statement since a simple `if (...) { return; }` is already = sufficient > enough. >=20 > Best regards, > Benas Seliuginas I think there's three main reasons for guard, as opposed to if: 1) It further and clearly establishes intent: if you see guard, you have = more information about the programmer's intent and what the code will = actually do. 2) It prevents having to negate the condition: guard (is valid) else, = instead of if (not valid) then; negations impose additional cognitive = load when attempting to understand code, especially if your condition = already has a negation. 3) The language provides a guarantee that if the guard condition is not = met, execution can not proceed past the else clause. This helps prevent = accidental fall-through, such as if you wrote: if(!$valid) { = terminate(); } expecting it to end execution (e.g. via a throw or exit), = but for some reason it failed to do so. To take an idea from Ralph's original proposal, perhaps some syntax like = "if guard (condition) else ...". In this context, guard could not = possibly be confused with a function from the parser's point of view, = instead serving as an intent modifier to if. -John