Re: [PHP-DEV] Proposal For Return-If / Early Return / Guard Clause Syntax

This is only part of a thread. view whole thread
  110114
May 10, 2020 19:19 benas.molis.iml@gmail.com (Benas IML)
Hello,

I think that we SHOULD not introduce a new keyword (e. g.  guard) since that
would be a "major major" backwards incompatibility. "Guard" is a really
generic
word and a great example of that is Laravel and their authentication guards.

In general, I don't think that early returns require a seperate syntax
and/or
block statement since a simple `if (...) { return; }` is already sufficient
enough.

Best regards,
Benas Seliuginas
  110117
May 10, 2020 20:00 jbafford@zort.net (John Bafford)
Benas,

> On May 10, 2020, at 15:19, Benas IML iml@gmail.com> wrote: > > Hello, > > I think that we SHOULD not introduce a new keyword (e. g. guard) since that > would be a "major major" backwards incompatibility. "Guard" is a really generic > word and a great example of that is Laravel and their authentication guards. > > In general, I don't think that early returns require a seperate syntax and/or > block statement since a simple `if (...) { return; }` is already sufficient > enough. > > Best regards, > Benas Seliuginas
I think there's three main reasons for guard, as opposed to if: 1) It further and clearly establishes intent: if you see guard, you have more information about the programmer's intent and what the code will actually do. 2) It prevents having to negate the condition: guard (is valid) else, instead of if (not valid) then; negations impose additional cognitive load when attempting to understand code, especially if your condition already has a negation. 3) The language provides a guarantee that if the guard condition is not met, execution can not proceed past the else clause. This helps prevent accidental fall-through, such as if you wrote: if(!$valid) { terminate(); } expecting it to end execution (e.g. via a throw or exit), but for some reason it failed to do so. To take an idea from Ralph's original proposal, perhaps some syntax like "if guard (condition) else ...". In this context, guard could not possibly be confused with a function from the parser's point of view, instead serving as an intent modifier to if. -John
  110161
May 15, 2020 08:33 benas.molis.iml@gmail.com (Benas IML)
Hey,

`guard` would be a keyword this means that all of the classes, interfaces
and traits named Guard would be illegal. Therefore Laravel's `Guard`
interface would be incompatible with PHP 8 which in turn means thousands of
web applications would be too.

Best regards,
Benas Seliuginas
  110167
May 15, 2020 13:36 jbafford@zort.net (John Bafford)
Benas,

> On May 15, 2020, at 04:33, Benas IML iml@gmail.com> wrote: > > Hey, > > `guard` would be a keyword this means that all of the classes, interfaces and traits named Guard would be illegal. Therefore Laravel's `Guard` interface would be incompatible with PHP 8 which in turn means thousands of web applications would be too. > > Best regards, > Benas Seliuginas
If the parser were sufficiently smart, I don't think 'guard' would actually conflict with a class or function named 'guard'. If the syntax were: guard (EXPRESSION) else STATEMENT_OR_BLOCK then, 'guard' could not conflict with a class name, because there's no existing syntax that would fit the pattern of a type name followed by a parenthesis — you can't call a type name as if it were a function. Even if you could, it would still not conflict with a function call, because the 'else' keyword after the close parenthesis would not be valid syntax immediately following a function call. So the main question there is, is the parser sufficiently smart to be able to see 'guard' and look ahead for the 'else' to disambiguate a function call from a guard statement? -John
  110168
May 15, 2020 13:57 benas.molis.iml@gmail.com (Benas IML)
Hello,

Not it's not and will likely never be so using `guard` is a really bad idea.

Best regaeds,
Benas Seliuginas