Req #77037 [NEW]: New Breakable Construct
From: erickwoods at gmail dot com
Operating system: All
PHP version: Next Major Version
Package: *Programming Data Structures
Bug Type: Feature/Change Request
Bug description:New Breakable Construct
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 bug report at https://bugs.php.net/bug.php?id=77037&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=77037&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=77037&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=77037&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=77037&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=77037&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=77037&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=77037&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=77037&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=77037&r=support
Expected behavior: https://bugs.php.net/fix.php?id=77037&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=77037&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=77037&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=77037&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77037&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=77037&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=77037&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=77037&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=77037&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=77037&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=77037&r=mysqlcfg
Thread (4 messages)
- erickwoods at gmail dot com