|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2018-10-19 06:41 UTC] requinix@php.net
-Status: Open
+Status: Suspended
[2018-10-19 06:41 UTC] requinix@php.net
[2018-10-19 14:58 UTC] erickwoods at gmail dot com
[2018-10-19 16:13 UTC] requinix@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 09:00:02 2025 UTC |
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.