Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110107 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 90125 invoked from network); 10 May 2020 17:13:08 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 10 May 2020 17:13:08 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A141A1804CB for ; Sun, 10 May 2020 08:49: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=0.0 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE 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-ot1-f43.google.com (mail-ot1-f43.google.com [209.85.210.43]) (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 ; Sun, 10 May 2020 08:49:17 -0700 (PDT) Received: by mail-ot1-f43.google.com with SMTP id m13so5568998otf.6 for ; Sun, 10 May 2020 08:49:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ralphschindler-com.20150623.gappssmtp.com; s=20150623; h=to:from:subject:message-id:date:user-agent:mime-version :content-language:content-transfer-encoding; bh=vp5sgQkUIjge7oyHO42x31zkue8iFsw19VihQ4ehiSE=; b=e6n5Oc0oUKTibTTzjhrW9S3Oa9z2sawOsUOxHekNuy84NZyQea4LFk6R21EZeizbh0 Tpo0SqBrTVm2fkChCTOIQCv1DGD+X5ZiBnhzKfwmi6dYIS3qiUAArI0us/bBN0lRg0cq XrMxamuLzXr9Sgkday+6b/CWZQRHY8S/cmhydJjbQDHkPyLK9wpdXNRTVnM0e/atp5Ex kZP37lcM5royMStErlTlwnyUFHsv4ddlUxR880oYliB2fQddyTY6YiwbGTqXrXsV41xt WXLkPR0+6isCImLSaxnr3QfyCGDY2xUpfKdaPa/Qz4AZsY5i+eQ3e2/2OnVlJMoS4ioF Y4Mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language:content-transfer-encoding; bh=vp5sgQkUIjge7oyHO42x31zkue8iFsw19VihQ4ehiSE=; b=JGWDc+iPj62LCt8AxWP4B6crb1bJUzbiJAEkN2uiwWZ3NVmaz/pzOPqZC2IwCcbxBu 2PwYLibmFIqAL/RrIH8T9tJSGd2MDvraIrO7e+VezJDrwTOPuAwytfc9p/06wVhFAfiu pIjbGQl0GJaedQZuOVq/n/vHVaPSpZxkQs+yEdiP1CH0Filv5qy0zAMVCxyL+bcmxQQJ a9j/n08ecx0bL6QtTkOaZe+DEY88A4qGpT8W91EFEuPO/RLV4PzkvSxV6t8clSX7x/Z4 xLmNzyms2ZFE/d5B6YUyzCgoHRJyPoAek/jDmMMflErKf087gevUg0FrJE431NdiLJH7 F9Lg== X-Gm-Message-State: AGi0Pubq+/IPQfmFc/fxnnGD658hzwqn8fcy6gzDfA/jJXDkj6FC9Prq QLI7SL1CXNr8nkxynXvAunB7dkTS9Ss= X-Google-Smtp-Source: APiQypKHvYgGhUgwLoykA03uLDZJVJx7f4zMs0o7OoP+c5w3e/xAu5eMQ9xQLNDFRV41SK0NQQxUpA== X-Received: by 2002:a9d:4808:: with SMTP id c8mr9240303otf.112.1589125757180; Sun, 10 May 2020 08:49:17 -0700 (PDT) Received: from Ralphs-ZiffBook-Pro.local (ip72-204-153-233.no.no.cox.net. [72.204.153.233]) by smtp.gmail.com with ESMTPSA id j3sm1982050otq.22.2020.05.10.08.49.16 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 10 May 2020 08:49:16 -0700 (PDT) To: PHP internals Message-ID: Date: Sun, 10 May 2020 10:49:15 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Subject: Proposal For Return-If / Early Return / Guard Clause Syntax From: ralph@ralphschindler.com (Ralph Schindler) 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 clauses 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!