Req #77037 [Sus]: New Breakable Construct
| From: | requinix@php.net | Date: | Fri, 19 Oct 2018 16:13:43 +0000 |
| Subject: | Req #77037 [Sus]: New Breakable Construct | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-217636@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77037&edit=1
ID: 77037
Updated by: requinix@php.net
Reported by: erickwoods at gmail dot com
Summary: New Breakable Construct
Status: Suspended
Type: Feature/Change Request
Package: *Programming Data Structures
Operating System: All
PHP Version: Next Major Version
Block user comment: N
Private report: N
New Comment:
If you're asking what I would do, I don't remember a case where I considered a
do/while(false) and couldn't refactor into something better. But that's not my point. What
I'm saying is, while this sugar could be nice, it's not going to solve a problem of people
writing bad code. In case that was a consideration.
And I'm not dismissing this. If my short reply to your long request sounds dismissive then
that's because this bug tracker is not the place to talk about the idea: between suitability,
keyword choice, and other features, there's enough to need a discussion and RFC for it.
Previous Comments:
------------------------------------------------------------------------
[2018-10-19 14:58:10] erickwoods at gmail dot com
This is a useful construct that actually does something pretty cool in practice. Blowing it off
with flippant remarks about poor programmers doesn't offer anything useful. In the real world,
where people can't always hire programmers who are rock stars, we have to confine those
programmers to standards and methods that make it easier for others to follow. This is potentially
one of those. I'd suggest trying it. In real code. You'll find out how nice it actually
is. The skeletons in the original are just that and aren't fleshed out for brevity, but ...
Try it.
------------------------------------------------------------------------
[2018-10-19 06:41:35] requinix@php.net
Poor quality programmers write poor quality code. Adding another language feature won't change
that.
Check out the RFC process. https://wiki.php.net/rfc/howto
------------------------------------------------------------------------
[2018-10-19 06:00:00] erickwoods at gmail dot com
Description:
------------
I've been using php since the late 90's. Love it. I propose a new construct for the
language. Too many times, people nest if statements and make a mess of code. Sometimes, there just
isn't a good alternative. In some cases, there are other solutions, but are equally ugly. I
propose an elegant, easy to parse, easy to learn construct.
Consider this function and the general flow:
function x()
{
if (1)
{
if (2)
{
if (3)
{
do_something();
return true;
}
else
{
return false;
}
else
{
return false;
}
do_something_else();
}
return false;
}
What I've been doing for years:
function x()
{
$ret = false;
do
{
if (!1)
{
break;
}
do_something_else();
if (!2)
{
break;
}
if (!3)
{
break;
}
do_something();
$ret = true;
}
while (0);
return $ret;
}
See how that works?
- Single entry to and exit from function.
- Less nesting.
- Easier to read/debug.
The problem with that is the confusion is causes. The initial reaction most have is "why would
you have a loop that doesn't loop?" The answer is it's not used as a loop, but as a
breakable construct. It organizes the code so much better and is incredibly useful in a wide
variety of circumstances. Think of a function where you're pulling data from a database.
Every step from opening the connection to preparing the query to executing the query to fetching the
result can cause a complete stop. In a breakable construct, the coder issues a break and it acts a
little like a goto, but without the baggage and idiocy. It's organized in curly braces where a
goto isn't and it's very clear what it does. When those same people who questioned it
initially understand what's happening, they think it's brilliant. I've been teaching
this to friends and colleagues, and they all love it and use it regularly.
So, this is what I propose:
function x()
{
$ret = false;
bc <-- new statement, bc for breakable construct
{
if (!1)
{
break;
}
do_something_else();
if (!2)
{
break;
}
if (!3)
{
break;
}
do_something();
$ret = true;
}
return $ret;
}
That looks like a function without an argument list. That is not unprecedented in any language
(think namespaces). Make it bc{} or breakable{}, whatever, it's amazing to use and
bastardizing the do{}while() is not quite right.
I don't have the skills to work this into PHP and submit a patch, but if the devs would
consider this and, hopefully, implement, I'd be forever happy.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77037&edit=1