rfc:conditional_break_continue_return
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:conditional_break_continue_return [2020/05/16 19:16] – minor edits ralphschindler | rfc:conditional_break_continue_return [2020/05/16 19:20] – ralphschindler | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Your Title Here ====== | + | ====== PHP RFC: Conditional Break, Continue, and Return Statements |
- | * Version: | + | * Version: |
- | * Date: 2013-02-24 (use today' | + | * Date: 2020-05-16 |
* Author: Ralph Schindler, ralphschindler@php.net | * Author: Ralph Schindler, ralphschindler@php.net | ||
* Status: Draft | * Status: Draft | ||
Line 19: | 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 31: | Line 31: | ||
return $dividend / $divisor; | return $dividend / $divisor; | ||
} | } | ||
- | </ | + | </ |
Considering Zeev's quote: | Considering Zeev's quote: | ||
Line 45: | Line 45: | ||
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. | + | 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. |
- | see abort_if/ | + | It is also worth mentioning that Ruby has similar features (called a modifier), and I believe they are heavily utilized.. [[https:// |
- | + | ||
- | It is also worth mentioning that Ruby has similar features (called a modifier), and I believe they are heavily utilized: | + | |
- | + | ||
- | see: https:// | + | |
==== Variation #1 ==== | ==== Variation #1 ==== | ||
Line 59: | Line 55: | ||
<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 76: | Line 72: | ||
<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