rfc:conditional_break_continue_return
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:conditional_break_continue_return [2020/05/16 19:12] – created, first pass ralphschindler | rfc:conditional_break_continue_return [2020/05/16 19:18] – ralphschindler | ||
---|---|---|---|
Line 5: | Line 5: | ||
* Status: Draft | * Status: Draft | ||
* First Published at: http:// | * First Published at: http:// | ||
- | |||
- | This is a suggested template for PHP Request for Comments (RFCs). Change this template to suit your RFC. Not all RFCs need to be tightly specified. | ||
- | Read https:// | ||
- | |||
- | |||
- | Quoting [[http:// | ||
- | |||
- | > PHP is and should remain: | ||
- | > 1) a pragmatic web-focused language | ||
- | > 2) a loosely typed language | ||
- | > 3) a language which caters to the skill-levels and platforms of a wide range of users | ||
- | |||
- | Your RFC should move PHP forward following his vision. As [[http:// | ||
- | large chunk of our userbase, and not something that could be useful in some | ||
- | extremely specialized edge cases [...] Make sure you think about the full context, the huge audience out there, the consequences of making the learning curve steeper with | ||
- | every new feature, and the scope of the goodness that those new features bring." | ||
- | |||
===== Introduction ===== | ===== Introduction ===== | ||
Line 36: | Line 19: | ||
return $dividend / $divisor; | return $dividend / $divisor; | ||
} | } | ||
- | </ | + | </ |
Option #2 with optional return after return keyword: | Option #2 with optional return after return keyword: | ||
Line 48: | Line 31: | ||
return $dividend / $divisor; | return $dividend / $divisor; | ||
} | } | ||
- | </ | + | </ |
+ | |||
+ | Considering Zeev's quote: | ||
+ | |||
+ | > Consider only features which have significant traction to a large chunk of our userbase, and not something that could be useful in some | ||
+ | I believe a significant number of developers would start to write conditional return (breaks and continues) as a natural tendency, and without thinking twice, if it were available... And I also believe that there would be, for a large population of developers and immediate benefit in reading code and reviewing code when adopting these kinds of conditional statements for php code that is written in the most popular coding standards that exist today. | ||
===== Proposal ===== | ===== Proposal ===== | ||
Line 71: | Line 59: | ||
<code php> | <code php> | ||
return if ($condition): | return if ($condition): | ||
- | </ | + | </ |
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. | + | |
- | 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. | + | |
- | 4. the optional return value syntax mimics that of the function/ | + | - The optional return value syntax mimics that of the function/ |
Cons: | Cons: | ||
- | 1. Does not read like an " | + | - Does not read like an " |
==== Variation #2 ==== | ==== Variation #2 ==== | ||
Line 88: | Line 76: | ||
<code php> | <code php> | ||
return $returnValue if ($condition); | return $returnValue if ($condition); | ||
- | </ | + | </ |
Pros: | Pros: | ||
- | 1. Reads well " | + | - Reads well " |
- | 2. 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. |
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== |
rfc/conditional_break_continue_return.txt · Last modified: 2022/04/17 18:38 by ilutov