rfc:conditional_break_continue_return

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:conditional_break_continue_return [2020/05/16 19:16] – minor edits ralphschindlerrfc:conditional_break_continue_return [2022/04/17 18:38] (current) – Move to inactive ilutov
Line 1: Line 1:
-====== PHP RFC: Your Title Here ====== +====== PHP RFC: Conditional Return, Break, and Continue Statements ====== 
-  * Version: 0.9 +  * Version: 1.0 
-  * Date: 2013-02-24 (use today's date here)+  * Date: 2020-05-16
   * Author: Ralph Schindler, ralphschindler@php.net   * Author: Ralph Schindler, ralphschindler@php.net
-  * Status: Draft+  * Status: Inactive
   * First Published at: http://wiki.php.net/rfc/conditional_break_continue_return   * First Published at: http://wiki.php.net/rfc/conditional_break_continue_return
 ===== Introduction ===== ===== Introduction =====
 +
 +Most generally, this is a syntactical change that (IMO, of course) allows for a terser and more expressive way to achieve conditional returns (along with breaks and continues).
  
 This proposal is a syntatical addition that allows `break`, `continue` and `return` to be conditionally qualified. Two variations are possible (depending on which, if either are desirable to have.) This proposal is a syntatical addition that allows `break`, `continue` and `return` to be conditionally qualified. Two variations are possible (depending on which, if either are desirable to have.)
Line 19: Line 21:
         return $dividend / $divisor;         return $dividend / $divisor;
     }      } 
-</code php>+</code>
  
 Option #2 with optional return after return keyword: Option #2 with optional return after return keyword:
Line 31: Line 33:
         return $dividend / $divisor;         return $dividend / $divisor;
     }      } 
-</code php>+</code>
  
 Considering Zeev's quote: Considering Zeev's quote:
Line 45: Line 47:
 In short [I perceive], a benefit of using guard clauses is to avoid deeply nested conditionals and to avoid increasing cognitive complexity in a function or method. Utilizing this technique in code results in code that is more easily code reviewable and easier to determine possible code paths that exists from the top to the bottom of a function or method. In short [I perceive], a benefit of using guard clauses is to avoid deeply nested conditionals and to avoid increasing cognitive complexity in a function or method. Utilizing this technique in code results in code that is more easily code reviewable and easier to determine possible code paths that exists from the top to the bottom of a function or method.
  
-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.+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|in 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 (called a modifier), and I believe they are heavily utilized.. [[https://github.com/rubocop-hq/ruby-style-guide#no-nested-conditionals|see here]].
  
-It is also worth mentioning that Ruby has similar features (called a modifier), and I believe they are heavily utilized: +(While the text of this applies most arguments to `return`, at current the same arguments apply for break and continue, and are included in this proposal.)
- +
-  see: https://github.com/rubocop-hq/ruby-style-guide#no-nested-conditionals+
  
 ==== Variation #1 ==== ==== Variation #1 ====
Line 59: Line 59:
 <code php> <code php>
 return if ($condition): $returnValue; return if ($condition): $returnValue;
-</code php>+</code>
  
 Pros: Pros:
-1. `return if` acts as a compound keyword, put another way since `if` must follow `return`, their proximity acts just like a new singular keyword would. It would effectively be a singular visual cue (much like, for example `return_if` if it were proposed). +  - `return if` acts as a compound keyword, put another way since `if` must follow `return`, their proximity acts just like a new singular keyword would. It would effectively be a singular visual cue (much like, for example `return_if` if it were proposed). 
-2. In the most common coding standards, `return if` will be aligned to the left most side of a line of code, making it easier for (humans) to scan for and quickly identify. +  In the most common coding standards, `return if` will be aligned to the left most side of a line of code, making it easier for (humans) to scan for and quickly identify. 
-3. (Building on the #2 Pro...) It keeps the precedence of information about the statement in a prioritized order. Put another way, since the first 2 lexical tokens are constant, and a meaning for the full statement can be derived from those, the following information (the actual condition and the actual optional return value - which I argue have less precedence) can be found later in the statement. +  (Building on the #2 Pro...) It keeps the precedence of information about the statement in a prioritized order. Put another way, since the first 2 lexical tokens are constant, and a meaning for the full statement can be derived from those, the following information (the actual condition and the actual optional return value - which I argue have less precedence) can be found later in the statement. 
-4. the optional return value syntax mimics that of the function/method return type hint syntax, as in at the end of a method signature in the `: <return>` format.+  - The optional return value syntax mimics that of the function/method return type hint syntax, as in at the end of a method signature in the `: <return>` format.
  
 Cons: Cons:
-1. Does not read like an "English-like" statement: "Return X if Y"+  - Does not read like an "English-like" statement: "Return X if Y"
  
 ==== Variation #2 ==== ==== Variation #2 ====
Line 76: Line 76:
 <code php> <code php>
 return $returnValue if ($condition); return $returnValue if ($condition);
-</code php>+</code>
  
 Pros: Pros:
-1. Reads well "Return subject if condition" +  - Reads well "Return subject if condition" 
-2. Follows a similar pattern as found in other languages, like Ruby+  Follows a similar pattern as found in other languages, like Ruby
  
 Con: Con:
-1. Large optional return values will create more space between the `return` and `if` keywords, potentially making it harder for (humans) to scan for the conditional qualifier.+  - Large optional return values will create more space between the `return` and `if` keywords, potentially making it harder for (humans) to scan for the conditional qualifier. 
 + 
 +==== A visual argument between the two variations ==== 
 + 
 +(Click to view full) 
 + 
 +{{:rfc:return-if-visual-reasoning.png}}
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
  
-No BC issues.+No BC issues: there are no new keywords, nor does not impact any existing code. 
 + 
 +This syntactical change is reusing existing keywords.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 113: Line 121:
  
 The recently contributed [[rfc:guard_statement|Guard RFC]] The recently contributed [[rfc:guard_statement|Guard RFC]]
 +
 +https://stackoverflow.com/questions/5436034/is-there-a-ruby-one-line-return-if-x
 +
 +https://engineering.helpscout.com/reducing-complexity-with-guard-clauses-in-php-and-javascript-74600fd865c7
 +
 +https://guidelines.spatie.be/code-style/laravel-php#avoid-else
 +
  
rfc/conditional_break_continue_return.1589656581.txt.gz · Last modified: 2020/05/16 19:16 by ralphschindler