Req #77037 [Opn->Sus]: New Breakable Construct

From: Date: Fri, 19 Oct 2018 06:41:35 +0000
Subject: Req #77037 [Opn->Sus]: New Breakable Construct
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-217631@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: Open +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: 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 Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#217631) next »