On Tue, Nov 26, 2024 at 6:18 PM Christoph M. Becker <cmbecke...@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.