Hi PHP Mailing list,
My username is vvolynets.
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?
Regards
Volodymyr Volynets
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
On Tue, Nov 26, 2024 at 6:18 PM Christoph M. Becker cmbecker69@gmx.de
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.
On Tue, Nov 26, 2024 at 2:18 PM Christoph M. Becker cmbecker69@gmx.de
wrote:
What's wrong with
if (!$result['success']) return $result;
Christoph
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.
--
Marco Deleu
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
$result['success'] ?: return false;
and
match ($something) {
123 => do_stuff(),
default => return false,
}
but they both can open an endless can of worms I'd say so I agree with
Christoph that the current syntax is good enough
Anton
Here’s the previous RFC: https://wiki.php.net/rfc/conditional_break_continue_return
And the previous discussion: https://externals.io/message/110107
Great, thanks
I went through RFC and discussions and there's new findings with throw:
- throw when condition, new Exception('Massage!!!');
- throw when $success, new Exception('I am here');
-
return when condition, 'return value';
- return when $success, $result;
- return when ($result = Somefunc() !== false), $result;
How can we reopen a discussion on these two topics?
Regards
Volodymyr Volynets
Here’s the previous RFC:
https://wiki.php.net/rfc/conditional_break_continue_returnAnd the previous discussion: https://externals.io/message/110107
Hey Volodymyr,
You can already write:
$success or throw new Exception('I am here');
Honestly, I don't see a good reason to introduce another syntax to do the
same thing.
Kind regards,
Jorg Sowa
To me it seams too similar to:
return !$result['success']?? $result;
Which returns at that point, regardless of the status of $result[’success’];
I think this
if (!$result['success'])
return $result;
Is short enough and is clear when scanning the code that it will not return anything if $result['success’] == true.
Daniel Baldwin
541-401-7797
Hi PHP Mailing list,
My username is vvolynets.
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?
Regards
Volodymyr Volynets