Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110170 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 85226 invoked from network); 15 May 2020 20:20:47 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 May 2020 20:20:47 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A04E11804F8 for ; Fri, 15 May 2020 11:58:15 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, 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-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 ; Fri, 15 May 2020 11:58:12 -0700 (PDT) Received: by mail-wm1-f48.google.com with SMTP id f134so3335016wmf.1 for ; Fri, 15 May 2020 11:58:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:message-id:in-reply-to:references:subject:mime-version; bh=NHWy3lQ51hzQf7ol7JypuS0UXIv779f3wMXA/DQYyCg=; b=UNrwXH2uITWq7YsNOAHLj+hkxCP7CuoEk++yvshesbOSgGBTZTjamQUa+mrBwynRfr 9qXhfuceBlDRF0mEEtC1IbNyW7jJXmBC4hV8ur/w3/ifurFanghclIsrRCUlF5DoiMR8 RvFjCEpvyUjGDK8UVr4+EqFw8QnYDWQHZjlQpQfqELWnBbkl2xBkrYSMXHEwTSK234Tv +Z5YJNviOMMmeq05ucNaYb6BAaqsix1traNAljwc8Kt21zZfESyuJAYcHdftstQHGuV3 rc4hT4B7cWqAJZxm8SUcoBdzis/eBYPdjvdf9R6y1atW1cjUsTVvCelBRfzVbv0yiqmF Xe8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:message-id:in-reply-to:references :subject:mime-version; bh=NHWy3lQ51hzQf7ol7JypuS0UXIv779f3wMXA/DQYyCg=; b=pxlA0SbIUBZr7aJE7zvXqAVOQuaprftFmqTdiz7Mv9mkGi6FPRyd/VuOWXIyYbHPb7 QSo2GXmjVYdy2VxwY2YH3i9YdRJqe3ln4hqAETsGsV8YmH4qtU3mxG3PbjC0YWBfgbSw 7T/qD/wawwZKFS/MSgUWeS2SyxxBL2fVHbZ77XocT/j6EHxVsH84PRhSXrTvAhh0pIcF OwOJWKed3JalyHqtjPfzx//CYg8Ft4oY+vkCxFR6OnsGBkXbdD0fTi9nIBkiUtoGroV2 Jyu8x3HVc3cQu+mRfzrv0hzZ6Q1opONzZ1qoUB1eNy7B9WodG/Ck2Ob7QvBAi9vTGivN WvHw== X-Gm-Message-State: AOAM532K7pVJaGh8ssdL0YXnu+9aZsvbNuh7OBPBt5MZenA+lZp8xVYe lqdO3BxxqQK6Ya0tzaXSCH5dB/J2Mfc= X-Google-Smtp-Source: ABdhPJw9eEORLsLtJ2hI7h9CSu0XAEC8R6vdOLEBl+9hvEpPhx3tqOo8RaIKVFWW2eH//7SX0bQePg== X-Received: by 2002:a1c:c38b:: with SMTP id t133mr5311134wmf.31.1589569088203; Fri, 15 May 2020 11:58:08 -0700 (PDT) Received: from tux (s55958d9d.adsl.online.nl. [85.149.141.157]) by smtp.gmail.com with ESMTPSA id 94sm5225675wrf.74.2020.05.15.11.58.06 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 May 2020 11:58:06 -0700 (PDT) Date: Fri, 15 May 2020 20:58:05 +0200 To: PHP internals Message-ID: <85C8412B-B04A-4853-8C4C-E270FCB35EEE@getmailspring.com> In-Reply-To: References: X-Mailer: Mailspring MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="5ebee63d_6b18b9b8_238b0" Subject: Re: [PHP-DEV] Proposal For Return-If / Early Return / Guard Clause Syntax From: crocodile2u@gmail.com (Victor Bolshov) --5ebee63d_6b18b9b8_238b0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline 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-4853-8C4C-E270FCB35EEE@getmailspring.com/0?redirect=https%3A%2F%2Fgetmailspring.com%2F&recipient=aW50ZXJuYWxzQGxpc3RzLnBocC5uZXQ%3D), the best free email app for work On May 10 2020, at 5:49 pm, Ralph Schindler wrote: > Hi! # Intro I am proposing what is a near completely syntactical addition (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 years, I've seen a growing number of blog posts, conference talks, and even tooling (for example code complexity scoring), that suggest writing guard clauses is a good practice to utilize. I've also seen it more prevalent in code, 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/docs/7.x/helpers#method-abort-if It is also worth mentioning that Ruby has similar features, and I believe they are heavily utilized: see: https://github.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 control flow / guard clause s more visible when scanning code, I am proposing this in the syntax of adding `return if`. The chosen syntax is: return if ( if_expr ) [: optional_return_expression] ; As a contrived example: function divide($dividend, $divisor = null) { return if ($divisor === null || $divisor === 0); return $dividend / $divisor; } There is already a little discussion around 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 the 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 ifs" 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 the quality that the : return_expression; is very symmetrical to the way we demarcate the return type in method signatures "): return type {" for example. - has the quality of promoting single-line conditional returns # Finally One might say this is unnecessary syntactic sugar, which is definitely arguable. But we do have multiple ways of achieving this. Of course all of these things should be discussed, I think sub-votes (should this PR make it that far) could be considered. The PR is located here: https://github.com/php/php-src/pull/5552 As mentioned, some discussion is happening there as well. Thanks! Ralph Schindler PS: since implementing the ::class feature 8 years ago, the addition of the AST abstraction made this kind of syntactical change proof-of-concept so much easier, bravo! -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php --5ebee63d_6b18b9b8_238b0--