php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #76841 simple breaking structure for better code.
Submitted: 2018-09-04 23:23 UTC Modified: 2018-09-13 20:24 UTC
From: leon at valkenb dot org Assigned:
Status: Wont fix Package: *General Issues
PHP Version: Next Minor Version OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2018-09-04 23:23 UTC] leon at valkenb dot org
Description:
------------
I'd like to see something  like this in PHP (and all languages)

procedure{

	// Check simple statement
	if ($a = $b){
		break;
	}

	// Check complex statement
	if ( ($a != $b) && ($c > $b) ) {
		break;
	}

	// All checks succeed. Do something.
	something();
}

I write a good amount of code, and one of the annoyng things is not being able to simply create a process that can be broken out of without a loop.
Often i will use a switch / case, but this is not a tidy solution since there is oten no requirement for a case.

Test script:
---------------
procedure{ // or could be just proc. I couldn't think of a better word

	// Check simple statement
	if ($a = $b){
		break;
	}

	// Check complex statement
	if ( ($a != $b) && ($c > $b) ) {
		break;
	}

	// All checks succeed. Do something.
	print "Hello"
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-09-04 23:28 UTC] chx@php.net
-Status: Open +Status: Wont fix
 [2018-09-04 23:28 UTC] chx@php.net
This is called a function. http://php.net/manual/en/functions.user-defined.php 

function foobar() {
  if (!check1()) {
    return;
  }
  if (!check2()) {
    return;
  }
  baz();
}

You could also throw exceptions  etc
 [2018-09-04 23:40 UTC] requinix@php.net
-Package: PHP Language Specification +Package: *General Issues
 [2018-09-04 23:40 UTC] requinix@php.net
do..while(false) is a common idiom across all programming languages.
 [2018-09-13 20:24 UTC] leon at valkenb dot org
Not a function.
A function has to be placed somewhere else in the code. And a nested function needs to be called after the declaration.
Both of these require developers to have to leave the simple procedure that are reading, then find this function, then read it, then go back to the code.
This is easy enough to do, but i can attribute hours per week of my time doing this. For some really really basic shit too.

I don't hate my life, but i do hate the goto statement.
So this is the combination of both.

All i want a super simple bit of procedural code to be just as simple to read.

I could also do the same logic with nested if statements or other logic. But all of this, as basic as it is, is still overkill.
 [2018-09-13 20:34 UTC] leon at valkenb dot org
do..while works yes, but the statements do not reflect the purpose of the code.

In my opinion the language fails as soon as you execute up in basic procedural code.
Or the language statements don't reflect the purpose in which they were used.

do..while and for imply that there is something that needs to be done more than once.

switch implies there is a single condition that has multiple options.

No matter what others i'm missing, you know that it's not going to reflect the intent of the developer in this situation.

And ill iterate, i hate goto, not my life.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 21:01:27 2024 UTC