I have an idea which will save a lot of code. I am proposing to add":
return when [condition], [return value];
This construct will remove a lot of ifs statements after method calls. For
example:
$result = Class->method();
if (!$result['success']) {
return $result;
}
This becomes:
return when !$result['success'], $result;
Any thoughts?
What's wrong with
if (!$result['success']) return $result;
That's not longer than what you are proposing, and in my opinion, quite
readable.
The fact that contemporary coding standards require you to write the
if-body on a separate line and to always use braces, is perhaps a
problem of these coding standards.
On 26/11/2024 18:10, Volodymyr Volynets wrote:
> I have an idea which will save a lot of code. I am proposing to add":
>
> return when [condition], [return value];
>
> This construct will remove a lot of ifs statements after method calls.
> For example:
>
> $result = Class->method();
> if (!$result['success']) {
> return $result;
> }
>
> This becomes:
> return when !$result['success'], $result;
>
> Any thoughts?
Is there a language that uses a similar syntax? I don't find it readable. It also uses a new keyword (when) when you can reuse if I'd say, also comma does not look like a good separator here.
Some alternatives could be:
Perl&Ruby-like postfix if:
return $result if (!$result['success']);
or making return an expression that will allow us to do like this
On Tue, Nov 26, 2024 at 6:18 PM Christoph M. Becker <cmbecker69@gmx.de> wrote:
On 26.11.2024 at 17:10, Volodymyr Volynets wrote:
I have an idea which will save a lot of code. I am proposing to add":
return when [condition], [return value];
This construct will remove a lot of ifs statements after method calls. For
example:
$result = Class->method();
if (!$result[‘success’]) {
return $result;
}
This becomes:
return when !$result[‘success’], $result;
Any thoughts?
What’s wrong with
if (!$result[‘success’]) return $result;
That’s not longer than what you are proposing, and in my opinion, quite
readable.
The fact that contemporary coding standards require you to write the
if-body on a separate line and to always use braces, is perhaps a
problem of these coding standards.
Christoph
I don’t know if PHP needs this feature, though for me this notation is harder to quickly scan and see what it does. It’s really relatively easy to find out what a line of code does when it starts with if, for, foreach, while, do, switch, return etc, The example you’ve provided reads to me as an if and then the part below is the body of the if. To find out it’s a return I first have to look to the right of what was written. I used to write if something then return end in Lua, but the more I had to read back my code I started disliking the readability of this formatting.
I don’t think I’m in favour of the proposal as I think it’s high effort for little gain, but to me this “alternative” requires you to mentally parse a conditional statement and then pay attention to its consequence, which is an afterthought and can be a return, but can also be anything else. The proposed syntax seems superior because the first thing the developer is presented with is the fact that this is a return statement / end of the block, but under a specific condition, which is a secondary artifact.
Working with early returns and fail-early approaches, I have to agree that the proposed syntax is superior than this alternative and not just because of current coding standards, but more on the semantics of how to go about the statement itself.
But as I stated, it doesn’t feel like the effort is worth the benefit because even if this could be implemented in a 1-line-of-code on php-src, it still means syntax change which will affect tooling and cause a ton of open-source overhead with token_get_all() changes, etc.