php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #77037 New Breakable Construct
Submitted: 2018-10-19 06:00 UTC Modified: 2018-10-19 16:13 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: erickwoods at gmail dot com Assigned:
Status: Suspended Package: *Programming Data Structures
PHP Version: Next Major Version OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: erickwoods at gmail dot com
New email:
PHP Version: OS:

 

 [2018-10-19 06:00 UTC] 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.



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-10-19 06:41 UTC] requinix@php.net
-Status: Open +Status: Suspended
 [2018-10-19 06:41 UTC] 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 14:58 UTC] 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 16:13 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 17:01:58 2024 UTC