Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110196 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 19619 invoked from network); 17 May 2020 05:56:17 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 May 2020 05:56:17 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9D1881804C3 for ; Sat, 16 May 2020 21:34:07 -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=0.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, MALFORMED_FREEMAIL,MISSING_HEADERS,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-io1-f66.google.com (mail-io1-f66.google.com [209.85.166.66]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 16 May 2020 21:34:04 -0700 (PDT) Received: by mail-io1-f66.google.com with SMTP id f3so7078354ioj.1 for ; Sat, 16 May 2020 21:34:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:cc :content-transfer-encoding; bh=6Ne2tADq14qMn+q2PXmFDMGtkuUAUigGynMd1/uCsLA=; b=M0ie3A9jYK3Y54p5D0A9bdqiCcfe3QXcro3A5WpE4Z+8ycOZSddb9dok7zPz3xHitM en0pAaYbFzxoGGUzVx/f2X0y+/rjPXLBQnM/X7weWbQhfz2ROAib1K7s1uaJLSiNmkZR a3rU7QGPAG9FZdX8OGDxUrMxxspmNsp7x9FCyTmHt4nJ599k45sAC+b3YJPMyWCqKbQt Ofz4Cl7TxoLpg6A4qMZSrkGzr6M7Gpj2KZGE60j2JiYVUYkXhR7e+AgG+WKF7xEc5pJo 5yGNEpM5Jn1KAOp2qC9GVk056PiAg25ddCkwSJ3Zd3Y9eWZx9hJd5v137YEfsxoSAmHb NS9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:cc:content-transfer-encoding; bh=6Ne2tADq14qMn+q2PXmFDMGtkuUAUigGynMd1/uCsLA=; b=Dd72SJ8pGSNBjomuxwlmL3ChCDcElu7vzGweEI/gcFHsxsB11TiK9tHTO2L5ufE4Cd gkvDQN/bZKx2veuGYlBHoRgTDddj4ZTKc1XmMwIQOpDvFCKzuRbw7iJ+3hrc1IFkZPXj x9PC50wd0hF0bYGkUxZi1rs+xidTJV+5FSOUrax7N8O9JF/el7SCafYH8Q60F1HPHv8W trZbjvZvPv79m7RJKFjRYGUMNAw/H9RDsimEYNeiYfhJ/Or4bF/9gvHomSlnx4GMtofm CD/PlPSNIssT1fkOryuYhFaMOeYArOmCHf7TVRzCIH8lHPv70qMEj5kShMHtjylRUVWE mKAg== X-Gm-Message-State: AOAM533WnBBtoyzsg1W2fDh9RxC9L2qPk1Y5QZ140lgxzHltrT2J0DEV dVu5itQ4haBq/1fPfJeVOWTFZhexq21AYqcZ25izFTwho74= X-Google-Smtp-Source: ABdhPJyaVxcROFtGLXeZLJf/LdOHBMRzxe0Fr2avWhEYAbn15avAy4o/AyIDqe7xL1VV9vuhnHYhKHjAcqKk5U0juOs= X-Received: by 2002:a5e:c603:: with SMTP id f3mr9112819iok.56.1589690042389; Sat, 16 May 2020 21:34:02 -0700 (PDT) MIME-Version: 1.0 References: <85C8412B-B04A-4853-8C4C-E270FCB35EEE@getmailspring.com> In-Reply-To: <85C8412B-B04A-4853-8C4C-E270FCB35EEE@getmailspring.com> Date: Sat, 16 May 2020 21:33:51 -0700 Message-ID: Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Proposal For Return-If / Early Return / Guard Clause Syntax From: sarkedev@gmail.com (Peter Stalman) A few thoughts: 1. I agree with the sentiment that this syntactic sugar doesn't actually save any verbosity, so it's not really syntactic sugar at all. 2. There appears to now be another RFC by Pavel Patapau, specifically focused on a Guard statement as a new keyword (https://wiki.php.net/rfc/guard_statement), which now has its separate discussion. 3. As Thomas Lamy mentioned, I too would prefer a single keyword. Consider the following two examples: function foo($bar) { if ($bar =3D=3D=3D 1) return; if ($bar =3D=3D=3D 666) delete_everything(); } function foo($bar) { if ($bar =3D=3D=3D 1) return if ($bar =3D=3D=3D 666); delete_everything(); } Both would now be valid syntax, and IDEs would have a harder time warning about the misplaced semicolon in the second example. Wouldn't be very common, but still. 4. However, this RFC is interesting to me because there be a way to modify it to allow for less verbose refactoring, and kinda allowing returns to bubble up like exceptions do. I think it would only make sense if it's a "if not null then return" type of thing. Consider the following (a bit contrived, but I hope you get the point): function main_function() { $result =3D calculate($var); if ($result !=3D=3D null) return $result; /* do other important stuff */ } function main_function() { ifnotnullreturn calculate($var); /* do other important stuff */ } Obviously not an ideal keyword, but this is the only thing I can think of where this type of syntactic sugar makes sense and decreases verbosity. Something similar can also be accomplished with exception though. 5. Finally, I think if we start putting several returns at the same indentation then the cognitive load increases because we can no longer tell if a return is actually a return at a glance. Thanks, Peter On Fri, May 15, 2020 at 11:58 AM Victor Bolshov wro= te: > > Hi internals, > > I think it's just as good to write: > if ($condition) return $retval; > Yes, there are subtle semantic differences the new syntax would emphasize= , but it doesn't feel like it justifies it. New syntax also means the need = to support it, for IDEs and other tools, static analysis tools, code-style = tools - and all that for a very tiny benefit, if any. > Cheers, > Victor > > Sent from Mailspring (https://link.getmailspring.com/link/85C8412B-B04A-4= 853-8C4C-E270FCB35EEE@getmailspring.com/0?redirect=3Dhttps%3A%2F%2Fgetmails= pring.com%2F&recipient=3DaW50ZXJuYWxzQGxpc3RzLnBocC5uZXQ%3D), the best free= email app for work > On May 10 2020, at 5:49 pm, Ralph Schindler wr= ote: > > Hi! # Intro I am proposing what is a near completely syntactical additi= on (only change is to language.y) to the language. The best terminology for= this syntax is are: `return if`, "return early", or "guard clauses". see: = https://en.wikipedia.org/wiki/Guard_(computer_science) Over the past few ye= ars, I've seen a growing number of blog posts, conference talks, and even t= ooling (for example code complexity scoring), that suggest writing guard cl= auses is a good practice to utilize. I've also seen it more prevalent in co= de, and even attempts at achieving this with Exceptions (in an HTTP context= ) in a framework like Laravel. see abort_if/throw_if: https://laravel.com/d= ocs/7.x/helpers#method-abort-if It is also worth mentioning that Ruby has s= imilar features, and I believe they are heavily utilized: see: https://gith= ub.com/rubocop-hq/ruby-style-guide#no-nested-conditionals # Proposal In an = effort to make it a first class feature of the language, and to make the co= ntrol flow / guard clause > s more visible when scanning code, I am proposing this in the syntax of a= dding `return if`. The chosen syntax is: return if ( if_expr ) [: optional_= return_expression] ; As a contrived example: function divide($dividend, $di= visor =3D null) { return if ($divisor =3D=3D=3D null || $divisor =3D=3D=3D = 0); return $dividend / $divisor; } There is already a little discussion aro= und the choice of order in the above statement, the main take-aways and (my= ) perceived benefits are: - it keeps the intent nearest the left rail of th= e code (in normal/common-ish coding standards) - it treats "return if" as a= meta-keyword; if must follow return for the statement to be a guard clause= . This also allows a person to more easily discern "returns" from "return i= fs" more easily since there is not an arbitrary amount of code between them= (for example if the return expression were after return but before if). - = it has the quality that optional parts are towards the end - is also has th= e quality that the : return_expression; > is very symmetrical to the way we demarcate the return type in method si= gnatures "): return type {" for example. - has the quality of promoting sin= gle-line conditional returns # Finally One might say this is unnecessary sy= ntactic sugar, which is definitely arguable. But we do have multiple ways o= f achieving this. Of course all of these things should be discussed, I thin= k sub-votes (should this PR make it that far) could be considered. The PR i= s located here: https://github.com/php/php-src/pull/5552 As mentioned, some= discussion is happening there as well. Thanks! Ralph Schindler PS: since i= mplementing the ::class feature 8 years ago, the addition of the AST abstra= ction made this kind of syntactical change proof-of-concept so much easier,= bravo! -- PHP Internals - PHP Runtime Development Mailing List To unsubscr= ibe, visit: http://www.php.net/unsub.php >