# Make try/catch brackets optinal

- Thread: https://externals.io/message/61463
- Original archive: https://news-web.php.net/php.internals/61463
- Started by Ivan Enderlin @ Hoa on 2012-07-19T09:44:39+00:00
- Messages: 46
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/61463.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #61463 — Ivan Enderlin @ Hoa — 2012-07-19T09:44:39+00:00

URL: https://externals.io/message/61463#61463

Hi internals,

As you certainly know, brackets defining blocks in PHP are optional if 
blocks contain a single instruction. Thus:

if($condition) {
echo 'foobar';
}

is strictly equivalent to:

if($condition)
echo 'foobar';

But this syntactic sugar is not applied uniformly to all PHP language 
constructions. I have the try/catch couple in mind.
First, I would like to know why it is not possible to write:

try
throw new Exception('foobar');
catch(Exception $e)
var_dump($e->getMessage());

as a strict equivalence of:

try {
throw new Exception('foobar');
}
catch(Exception $e) {
var_dump($e->getMessage());
}

Second, if it is possible, could we plan to have this “feature” 
(uniformity actually) in PHP6 (or maybe before)?

Best regards.

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
http://disc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/

---

## #61464 — Paul Dragoonis — 2012-07-19T09:49:13+00:00

URL: https://externals.io/message/61463#61464 · In reply to: #61463

Why is your try block only going to contain 1 line, and that's
throwing an exception??

try
throw new Exception('foobar');
catch(Exception $e)


Braces are a good thing, they give structure and stop people from
mis-reading things and writing bugs, the same can be said for the if()
situation.

1) Braces are good.
2) Try with only one line in it to throw an exception doesn't seem
like a realistic situation.

-1 from me, sorry Hoa.

On Thu, Jul 19, 2012 at 10:44 AM, Ivan Enderlin @ Hoa
<ivan.enderlin@hoa-project.net> wrote:
> Hi internals,
>
> As you certainly know, brackets defining blocks in PHP are optional if
> blocks contain a single instruction. Thus:
>
> if($condition) {
> echo 'foobar';
> }
>
> is strictly equivalent to:
>
> if($condition)
> echo 'foobar';
>
> But this syntactic sugar is not applied uniformly to all PHP language
> constructions. I have the try/catch couple in mind.
> First, I would like to know why it is not possible to write:
>
> try
> throw new Exception('foobar');
> catch(Exception $e)
> var_dump($e->getMessage());
>
> as a strict equivalence of:
>
> try {
> throw new Exception('foobar');
> }
> catch(Exception $e) {
> var_dump($e->getMessage());
> }
>
> Second, if it is possible, could we plan to have this “feature” (uniformity
> actually) in PHP6 (or maybe before)?
>
> Best regards.
>
> --
> Ivan Enderlin
> Developer of Hoa
> http://hoa.42/ or http://hoa-project.net/
>
> PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
> http://disc.univ-fcomte.fr/ and http://www.inria.fr/
>
> Member of HTML and WebApps Working Group of W3C
> http://w3.org/

---

## #61465 — Alexey Zakhlestin — 2012-07-19T09:58:05+00:00

URL: https://externals.io/message/61463#61465 · In reply to: #61464

> 2) Try with only one line in it to throw an exception doesn't seem
> like a realistic situation.

It could be something like this:

try
$object->method();
catch (\Exception $e)
return false;


and ->method(), obviously, can have quite a long and complex structure

---

## #61466 — Charlie Somerville — 2012-07-19T09:58:14+00:00

URL: https://externals.io/message/61463#61466 · In reply to: #61464

> Why is your try block only going to contain 1 line, and that's
> throwing an exception??
>  
> try
> throw new Exception('foobar');
> catch(Exception $e)
>  
>  

Because it's a contrived example. He's not trying to write real code, he's trying to demonstrate his point - and you totally missed that point.  

> Braces are a good thing, they give structure and stop people from
> mis-reading things and writing bugs, the same can be said for the if()
> situation.
>  
> 1) Braces are good.
This is subjective. There are some cases where it might improve code readability to drop the braces for a single-statement try/catch.


There's certainly no technical barrier to doing this. I'm not familiar with PHP's parser, but I'd imagine there would be some kind of 'statement' non-terminal that would handle single statements as well as a braced group of statements.

> 2) Try with only one line in it to throw an exception doesn't seem
> like a realistic situation.
>  
>  

There could be some utility to this. For example, as well as having post-fix if, unless, etc., Ruby also has a post-fix 'rescue'. Here's a silly example of its use:


    some_var = foo.bar rescue "oops"

If 'foo.bar' threw an exception, some_var would contain "oops" instead.

I think PHP could benefit from having a single statement try form. I often turn to PHP for quick and dirty scripts when I need to do something with little fuss. I think having try/catch support brace-less single statements would help increase consistency in PHP's syntax, as well as be useful in certain situations.  



>  
> -1 from me, sorry Hoa.
>  
> On Thu, Jul 19, 2012 at 10:44 AM, Ivan Enderlin @ Hoa
> <ivan.enderlin@hoa-project.net (mailto:ivan.enderlin@hoa-project.net)> wrote:
> > […]

---

## #61467 — Rafael Dohms — 2012-07-19T10:01:44+00:00

URL: https://externals.io/message/61463#61467 · In reply to: #61463

On Thu, Jul 19, 2012 at 11:44 AM, Ivan Enderlin @ Hoa <
ivan.enderlin@hoa-project.net> wrote:

> Hi internals,
>
> As you certainly know, brackets defining blocks in PHP are optional if
> blocks contain a single instruction. Thus:
>
> if($condition) {
> echo 'foobar';
> }
>
> is strictly equivalent to:
>
> if($condition)
> echo 'foobar';
>
> But this syntactic sugar is not applied uniformly to all PHP language
> constructions. I have the try/catch couple in mind.
>

Writing if blocks without brakets is considered a bad practice. IMHO anyway.


> First, I would like to know why it is not possible to write:
>
> try
> throw new Exception('foobar');
> catch(Exception $e)
> var_dump($e->getMessage());
>
>
This has code readability problem written all over it. When maintaining it
also has problems, like with the bracket-less if's.
You would need to add brackets if you add an extra line here, as well as
you might get unexpected behaviour of you forget to
add brackets in that case.


> as a strict equivalence of:
>
> try {
> throw new Exception('foobar');
> }
> catch(Exception $e) {
> var_dump($e->getMessage());
> }
>
> Second, if it is possible, could we plan to have this “feature”
> (uniformity actually) in PHP6 (or maybe before)?
>
>
Sorry, -1 from me.

---

## #61468 — Paul Dragoonis — 2012-07-19T10:03:07+00:00

URL: https://externals.io/message/61463#61468 · In reply to: #61466

On Thu, Jul 19, 2012 at 10:58 AM, Charlie Somerville
<charlie@charliesomerville.com> wrote:
> 
>
> Why is your try block only going to contain 1 line, and that's
> throwing an exception??
>
> try
> throw new Exception('foobar');
> catch(Exception $e)
>
> Because it's a contrived example. He's not trying to write real code, he's
> trying to demonstrate his point - and you totally missed that

You're right, I totally missed that point.

>
> Braces are a good thing, they give structure and stop people from
> mis-reading things and writing bugs, the same can be said for the if()
> situation.
>
> 1) Braces are good.
>
> This is subjective. There are some cases where it might improve code
> readability to drop the braces for a single-statement try/catch.
>
> There's certainly no technical barrier to doing this. I'm not familiar with
> PHP's parser, but I'd imagine there would be some kind of 'statement'
> non-terminal that would handle single statements as well as a braced group
> of statements.

Same sentiments as from Rafael, bracket-less is bug-prone.

>
> 2) Try with only one line in it to throw an exception doesn't seem
> like a realistic situation.
>
> There could be some utility to this. For example, as well as having post-fix
> if, unless, etc., Ruby also has a post-fix 'rescue'. Here's a silly example
> of its use:
>
>     some_var = foo.bar rescue "oops"
>
> If 'foo.bar' threw an exception, some_var would contain "oops" instead.
>
> I think PHP could benefit from having a single statement try form. I often
> turn to PHP for quick and dirty scripts when I need to do something with
> little fuss. I think having try/catch support brace-less single statements
> would help increase consistency in PHP's syntax, as well as be useful in
> certain situations.
>
> 
>
>
> -1 from me, sorry Hoa.
>
> On Thu, Jul 19, 2012 at 10:44 AM, Ivan Enderlin @ Hoa
> <ivan.enderlin@hoa-project.net> wrote:
>
> Hi internals,
>
> As you certainly know, brackets defining blocks in PHP are optional if
> blocks contain a single instruction. Thus:
>
> if($condition) {
> echo 'foobar';
> }
>
> is strictly equivalent to:
>
> if($condition)
> echo 'foobar';
>
> But this syntactic sugar is not applied uniformly to all PHP language
> constructions. I have the try/catch couple in mind.
> First, I would like to know why it is not possible to write:
>
> try
> throw new Exception('foobar');
> catch(Exception $e)
> var_dump($e->getMessage());
>
> as a strict equivalence of:
>
> try {
> throw new Exception('foobar');
> }
> catch(Exception $e) {
> var_dump($e->getMessage());
> }
>
> Second, if it is possible, could we plan to have this “feature” (uniformity
> actually) in PHP6 (or maybe before)?
>
> Best regards.
>
> --
> Ivan Enderlin
> Developer of Hoa
> http://hoa.42/ or http://hoa-project.net/
>
> PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
> http://disc.univ-fcomte.fr/ and http://www.inria.fr/
>
> Member of HTML and WebApps Working Group of W3C
> http://w3.org/

---

## #61469 — Sebastian Krebs — 2012-07-19T10:03:14+00:00

URL: https://externals.io/message/61463#61469 · In reply to: #61464

Hi,

2012/7/19 Paul Dragoonis <dragoonis@gmail.com>

> Why is your try block only going to contain 1 line, and that's
> throwing an exception??
>
> try
> throw new Exception('foobar');
> catch(Exception $e)
>
>
> Braces are a good thing, they give structure and stop people from
> mis-reading things and writing bugs, the same can be said for the if()
> situation.
>
> 1) Braces are good.
> 2) Try with only one line in it to throw an exception doesn't seem
> like a realistic situation.
>

try $this->foobar(); catch ($e) {
    // Much amount of error handling
}

// or maybe

try $this->foobar(); catch ($e) $this->handleException($e);

OK, the first example is better, but both reads like quite nice :)

try someVeryDifficultStuff(); catch ($e) { /* handle */ }


Regards,
Sebastian


>
> -1 from me, sorry Hoa.
>
> On Thu, Jul 19, 2012 at 10:44 AM, Ivan Enderlin @ Hoa
> <ivan.enderlin@hoa-project.net> wrote:
> > […]
> (uniformity
> > […]

---

## #61470 — Charlie Somerville — 2012-07-19T10:03:51+00:00

URL: https://externals.io/message/61463#61470 · In reply to: #61467

> This has code readability problem written all over it. When maintaining it
> also has problems, like with the bracket-less if's.
> You would need to add brackets if you add an extra line here, as well as
> you might get unexpected behaviour of you forget to
> add brackets in that case.
>  
>  


I've often heard people make this argument, but I've never found it to be a real concern in practise.

Is this really such a common problem?  



> On Thu, Jul 19, 2012 at 11:44 AM, Ivan Enderlin @ Hoa <
> ivan.enderlin@hoa-project.net (mailto:ivan.enderlin@hoa-project.net)> wrote:
>  
> > […]
>  
>  
> Writing if blocks without brakets is considered a bad practice. IMHO anyway.
>  
>  
> > […]
>  
> This has code readability problem written all over it. When maintaining it
> also has problems, like with the bracket-less if's.
> You would need to add brackets if you add an extra line here, as well as
> you might get unexpected behaviour of you forget to
> add brackets in that case.
>  
>  
> > […]
>  
> Sorry, -1 from me.

---

## #61471 — Rafael Dohms — 2012-07-19T10:04:58+00:00

URL: https://externals.io/message/61463#61471 · In reply to: #61466

On Thu, Jul 19, 2012 at 11:58 AM, Charlie Somerville <
charlie@charliesomerville.com> wrote:

>
> 
>
> > […]
>
> Because it's a contrived example. He's not trying to write real code, he's
> trying to demonstrate his point - and you totally missed that point.
>
>
In this case the removal of brackets would surely limit this to one line,
so any examples or use cases would look the same.


> > […]
> This is subjective. There are some cases where it might improve code
> readability to drop the braces for a single-statement try/catch.
>
>
It would cause code maintainability problems and unexpected outputs in
error cases, just like if's do.


>
> There's certainly no technical barrier to doing this. I'm not familiar
> with PHP's parser, but I'd imagine there would be some kind of 'statement'
> non-terminal that would handle single statements as well as a braced group
> of statements.
>
> > […]
>
> There could be some utility to this. For example, as well as having
> post-fix if, unless, etc., Ruby also has a post-fix 'rescue'. Here's a
> silly example of its use:
>
>
>     some_var = foo.bar rescue "oops"
>
> If 'foo.bar' threw an exception, some_var would contain "oops" instead.
>

a rescue method is a complete other thing, sounds interesting, but has no
reference to bracket-less try blocks.


>
> I think PHP could benefit from having a single statement try form. I often
> turn to PHP for quick and dirty scripts when I need to do something with
> little fuss. I think having try/catch support brace-less single statements
> would help increase consistency in PHP's syntax, as well as be useful in
> certain situations.
>

I think bracket-less is a bad practice that was left for BC, i would rather
we move away from it then move more things into it.

-- 
Rafael Dohms
PHP Evangelist and Community Leader
http://www.rafaeldohms.com.br
http://www.phpsp.org.br

---

## #61472 — Rafael Dohms — 2012-07-19T10:06:34+00:00

URL: https://externals.io/message/61463#61472 · In reply to: #61470

On Thu, Jul 19, 2012 at 12:03 PM, Charlie Somerville <
charlie@charliesomerville.com> wrote:

> This has code readability problem written all over it. When maintaining it
> also has problems, like with the bracket-less if's.
> You would need to add brackets if you add an extra line here, as well as
> you might get unexpected behaviour of you forget to
> add brackets in that case.
>
> I've often heard people make this argument, but I've never found it to be
> a real concern in practise.
>
> Is this really such a common problem?
>

I have seen this problem happen, people losing time trying to figure out
what is wrong only to find
its a missing bracket.
As Paul said, this is bug-prone.
-- 
Rafael Dohms
PHP Evangelist and Community Leader
http://www.rafaeldohms.com.br
http://www.phpsp.org.br

---

## #61473 — Peter Beverloo — 2012-07-19T10:11:58+00:00

URL: https://externals.io/message/61463#61473 · In reply to: #61472

On Thu, Jul 19, 2012 at 11:06 AM, Rafael Dohms <listas@rafaeldohms.com.br>wrote:

> On Thu, Jul 19, 2012 at 12:03 PM, Charlie Somerville <
> charlie@charliesomerville.com> wrote:
>
> > […]
> it
> > […]
>
> I have seen this problem happen, people losing time trying to figure out
> what is wrong only to find
> its a missing bracket.
> As Paul said, this is bug-prone.
>

Other bracket-less blocks allow authors to shoot themselves in the foot
equally so, yet PHP supports these as well. The actual problem here is an
inconsistency in the parser, which I'd consider to be a bug.

Peter


> --
> Rafael Dohms
> PHP Evangelist and Community Leader
> http://www.rafaeldohms.com.br
> http://www.phpsp.org.br

---

## #61474 — Rune Kaagaard — 2012-07-19T10:45:53+00:00

URL: https://externals.io/message/61463#61474 · In reply to: #61473

+1 for the consistency of it. It's surprising that:

    if ($foo)
        return $bar;
    else
        return 42;

works and:

    try
        maybe_dangerous();
    catch(Dynamite $e)
        handle_error();

does not.


> On Thu, Jul 19, 2012 at 11:06 AM, Rafael Dohms <listas@rafaeldohms.com.br>wrote:
>
> > […]
>
> Other bracket-less blocks allow authors to shoot themselves in the foot
> equally so, yet PHP supports these as well. The actual problem here is an
> inconsistency in the parser, which I'd consider to be a bug.
>
> Peter
>
>
> > […]

---

## #61475 — Reeze — 2012-07-19T10:54:56+00:00

URL: https://externals.io/message/61463#61475 · In reply to: #61474

Hi，


在 2012年7月19日星期四，下午6:45，Rune Kaagaard 写道：

> +1 for the consistency of it. It's surprising that:
>  
> if ($foo)
> return $bar;
> else
> return 42;
>  
> works and:
>  
> try
> maybe_dangerous();
> catch(Dynamite $e)
> handle_error();
>  
>  

There is no condition after `try`, it's really hard to read without bracket.  
it becomes even worse if it didn't format pretty as above.
> does not.
>  
> 
> > […]

---

## #61479 — Lester Caine — 2012-07-19T12:30:09+00:00

URL: https://externals.io/message/61463#61479 · In reply to: #61473

Peter Beverloo wrote:
> Other bracket-less blocks allow authors to shoot themselves in the foot
> equally so, yet PHP supports these as well. The actual problem here is an
> inconsistency in the parser, which I'd consider to be a bug.

Having been caught out too many times now when adding an extra part to code 
there 99% of the blocks are correctly wrapped ... when I see code without the 
brackets they get added! The bug as far as I am concerned is NOT reporting them 
missing but just getting them displayed in eclipse would do me for now ;)

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

## #61482 — jevon — 2012-07-19T12:55:10+00:00

URL: https://externals.io/message/61463#61482 · In reply to: #61479

This reminds me of
   $var = something or `die()`;

So if you do follow with braceless-try, I would have
   try something() catch($e) do_something_with($e);

Or (a bit simpler, but assumes we have a new pseudovariable $e),
   try something() or do_something_with($e)

I don't like the form with a semicolon, because what if there are two
semi-colons after the statement? What does the try statement wrap?
   try something();; catch ($e) { ... }

The whole concept breaks away from the tradition of wrapping massive
blocks with try { } statements, and might make applications use
exceptions a LOT more freely. Something to keep in mind.

Jevon


> Peter Beverloo wrote:
> > […]
>
>
> Having been caught out too many times now when adding an extra part to code
> there 99% of the blocks are correctly wrapped ... when I see code without
> the brackets they get added! The bug as far as I am concerned is NOT
> reporting them missing but just getting them displayed in eclipse would do
> me for now ;)
>
> --
> Lester Caine - G8HFL
> -----------------------------
> Contact - http://lsces.co.uk/wiki/?page=contact
> L.S.Caine Electronic Services - http://lsces.co.uk
> EnquirySolve - http://enquirysolve.com/
> Model Engineers Digital Workshop - http://medw.co.uk
> Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

## #61483 — Sebastian Krebs — 2012-07-19T13:16:01+00:00

URL: https://externals.io/message/61463#61483 · In reply to: #61472

2012/7/19 Rafael Dohms <listas@rafaeldohms.com.br>

> On Thu, Jul 19, 2012 at 12:03 PM, Charlie Somerville <
> charlie@charliesomerville.com> wrote:
>
> > […]
> it
> > […]
>
> I have seen this problem happen, people losing time trying to figure out
> what is wrong only to find
> its a missing bracket.
> As Paul said, this is bug-prone.
>

Just curious (once more): How can anyone overlook, that the brackets are
intentionally omitted here?

if ($x) {
    // Do something
} else return null;


Of course it's a matter of code style [1], but one can write the ugliest
code with every syntax. This argument about "bug-prone" often confuses me,
because I for myself never had such a problem. I also never knew someone,
who had a problem with bracket-less blocks (but must say, that most write
blocks _with_ brackets, thus they _may_ have problems). I've often just
heard this "its bug-prone" (that sounds to me like "there are inattentive
devs out there" to me :X) as as killer argument...
At the end of the day I don't really care wether I should avoid
bracket-less blocks, or not, but ... it's this argument that annoys me :X
And I just think this just looks cute :)

try $calculator->calculateSomeVeryDifficultStuff() catch ($e) {
  // on failure
}

2cent and so on

Regards,
Sebastian


[1] I would wrap "return null;" in brackets too, if the line gets too long.

if ($x) {
    // Do something
} else {
    return someVeryLongStatement() && andSuch();
}






> --
> Rafael Dohms
> PHP Evangelist and Community Leader
> http://www.rafaeldohms.com.br
> http://www.phpsp.org.br

---

## #61484 — Lester Caine — 2012-07-19T13:51:06+00:00

URL: https://externals.io/message/61463#61484 · In reply to: #61483

Sebastian Krebs wrote:
> Of course it's a matter of code style [1], but one can write the ugliest
> code with every syntax. This argument about "bug-prone" often confuses me,
> because I for myself never had such a problem. I also never knew someone,
> who had a problem with bracket-less blocks (but must say, that most write
> blocks_with_  brackets, thus they_may_  have problems). I've often just
> heard this "its bug-prone" (that sounds to me like "there are inattentive
> devs out there" to me :X) as as killer argument...

10 years in to development work on projects combining library code from many 
sources, The indenting tends to get messed up over time, and it CAN be difficult 
to spot that the brackets are missing from a block. In addition to adding them 
as I spot missing ones, the indentation will also get tidied up to make things 
readable again. The latest injection problem I had reported turned out to be in 
the smarty library. The fix involved tidying the layout just to make sure I was 
in the right place when fixing it. ( 'highlight' would quite happily pass nasty 
code! ) It's all this code, the authors of which have now retired to an easier 
live which we have to maintain going forward :(

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

## #61487 — Cyberspice — 2012-07-19T14:26:09+00:00

URL: https://externals.io/message/61463#61487 · In reply to: #61470

Probably not so much in PHP but I've had some real doozers in my time due this.

For example in C...

if (somethingsFailed)
	DEBUG_MACRO("Something failed!");

var = 1;

And then someone un-defines DEBUG_MACRO.

Fun then ensues.

Melanie



> 
> 
> I've often heard people make this argument, but I've never found it to be a real concern in practise.
> 
> Is this really such a common problem?

---

## #61490 — Larry Garfield — 2012-07-19T15:43:52+00:00

URL: https://externals.io/message/61463#61490 · In reply to: #61473

> > […]
>
> Other bracket-less blocks allow authors to shoot themselves in the foot
> equally so, yet PHP supports these as well. The actual problem here is an
> inconsistency in the parser, which I'd consider to be a bug.
>
> Peter


PHP doesn't support optional brackets on functions, either; please no 
one suggest that.

Yes, it's inconsistent that some structures allow short-circuited 
brackets.  The solution isn't to let all structures have the 
bug-attracting syntax.  If it wouldn't break a few zillion lines of 
existing code I'd say we should resolve the inconsistency by making the 
braces required on if/foreach/etc.  PHP only has them optional due to a 
C/C++ legacy, which may have made sense when the byte size of source 
code actually mattered for storage efficiency.

Yes, I have run into bugs that were caused by people forgetting braces. 
  Yes, I have introduced bugs without realizing it because someone left 
off a brace and I didn't notice it.  Yes, I now always add braces when 
looking at someone's code; I can't even read it otherwise anymore.  Any 
respectable coding standard requires the otherwise-optional braces.

And yes, I always close my </p> tags as well, and so should you! :-)

--Larry Garfield

---

## #61494 — Andrew Faulds — 2012-07-19T15:52:19+00:00

URL: https://externals.io/message/61463#61494 · In reply to: #61490

Always close <p>, but never close <li> :)


> 
>
>  I have seen this problem happen, people losing time trying to figure out
> > […]
>
>
> PHP doesn't support optional brackets on functions, either; please no one
> suggest that.
>
> Yes, it's inconsistent that some structures allow short-circuited
> brackets.  The solution isn't to let all structures have the bug-attracting
> syntax.  If it wouldn't break a few zillion lines of existing code I'd say
> we should resolve the inconsistency by making the braces required on
> if/foreach/etc.  PHP only has them optional due to a C/C++ legacy, which
> may have made sense when the byte size of source code actually mattered for
> storage efficiency.
>
> Yes, I have run into bugs that were caused by people forgetting braces.
>  Yes, I have introduced bugs without realizing it because someone left off
> a brace and I didn't notice it.  Yes, I now always add braces when looking
> at someone's code; I can't even read it otherwise anymore.  Any respectable
> coding standard requires the otherwise-optional braces.
>
> And yes, I always close my </p> tags as well, and so should you! :-)
>
> --Larry Garfield

---

## #61495 — Larry Garfield — 2012-07-19T15:56:53+00:00

URL: https://externals.io/message/61463#61495 · In reply to: #61494

There is no such thing as an optional closing tag or brace.  There's 
only lazy and sloppy coders, and parsers that encourage them.

--Larry Garfield


> Always close <p>, but never close <li> :)
> 
>
> > […]

---

## #61498 — Andrew Faulds — 2012-07-19T16:07:34+00:00

URL: https://externals.io/message/61463#61498 · In reply to: #61495

HTML5 allow omitting head, body, html (they had no fundamental differences
in parsing IRL), and also quotes and some end tags. Makes for much, much
nicer HTML, e.g.:

<!doctype html>
<meta charset=utf-8>
<title>hello</title>

<h1>hi</h1>
<ul>
    <li>item 1
    <li>item 2
</ul>

...that's off-topic though. I don't think we should have more tolerance of
omitted braces in PHP. If you desparately want them, write CoffeeScript
except for PHP (RasmusScript?)


> There is no such thing as an optional closing tag or brace.  There's only
> lazy and sloppy coders, and parsers that encourage them.
>
> --Larry Garfield
>
> 
>
> > […]

---

## #61509 — Sara Golemon — 2012-07-19T18:22:11+00:00

URL: https://externals.io/message/61463#61509 · In reply to: #61463

For all the reasons already stated, most explicitly because it make the
code ugly as sin, my vote is somewhere between "No", and "Hell No."

-1 on bracketless try/catch


On Thu, Jul 19, 2012 at 2:44 AM, Ivan Enderlin @ Hoa <
ivan.enderlin@hoa-project.net> wrote:

> Hi internals,
>
> As you certainly know, brackets defining blocks in PHP are optional if
> blocks contain a single instruction. Thus:
>
> if($condition) {
> echo 'foobar';
> }
>
> is strictly equivalent to:
>
> if($condition)
> echo 'foobar';
>
> But this syntactic sugar is not applied uniformly to all PHP language
> constructions. I have the try/catch couple in mind.
> First, I would like to know why it is not possible to write:
>
> try
> throw new Exception('foobar');
> catch(Exception $e)
> var_dump($e->getMessage());
>
> as a strict equivalence of:
>
> try {
> throw new Exception('foobar');
> }
> catch(Exception $e) {
> var_dump($e->getMessage());
> }
>
> Second, if it is possible, could we plan to have this “feature”
> (uniformity actually) in PHP6 (or maybe before)?
>
> Best regards.
>
> --
> Ivan Enderlin
> Developer of Hoa
> http://hoa.42/ or http://hoa-project.net/
>
> PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
> http://disc.univ-fcomte.fr/ and http://www.inria.fr/
>
> Member of HTML and WebApps Working Group of W3C
> http://w3.org/

---

## #61540 — Amaury Bouchard — 2012-07-20T07:35:26+00:00

URL: https://externals.io/message/61463#61540 · In reply to: #61475

2012/7/19 Reeze <reeze.xia@gmail.com>

> 在 2012年7月19日星期四，下午6:45，Rune Kaagaard 写道：
>
> > […]
>
> There is no condition after `try`, it's really hard to read without
> bracket.
> it becomes even worse if it didn't format pretty as above.
>

Bad argument. It's not about the presence of a condition after "try". It's
about language consistency.
If you were right, we shouldn't be able to write that:
  $i = 0;
  do
      echo($i++);
  while ($i < 3);

PHP allows it. Nobody cares about it, while I think it's a very ugly
writing. But everybody is accustomed to it, because it's inherited from C
syntax.
And bracketless try/catch doesn't exists in other languages, so it
shouldn't exists in PHP?

More, do not confuse coding conventions (depends on people, changes from
time to time) and language syntax (should be stable and consistent).

---

## #61543 — Andrew Faulds — 2012-07-20T07:47:51+00:00

URL: https://externals.io/message/61463#61543 · In reply to: #61540

I don't think we should add it purely for consistency, because then we'd
have to allow nonsense like:

switch x;
case 1;
endcase;
endswitch;

or...

try;
x;
catch e;
endtry;

Sure, consistency is good, but this would allow sloppy code.


> 2012/7/19 Reeze <reeze.xia@gmail.com>
>
> > […]
>
> Bad argument. It's not about the presence of a condition after "try". It's
> about language consistency.
> If you were right, we shouldn't be able to write that:
>   $i = 0;
>   do
>       echo($i++);
>   while ($i < 3);
>
> PHP allows it. Nobody cares about it, while I think it's a very ugly
> writing. But everybody is accustomed to it, because it's inherited from C
> syntax.
> And bracketless try/catch doesn't exists in other languages, so it
> shouldn't exists in PHP?
>
> More, do not confuse coding conventions (depends on people, changes from
> time to time) and language syntax (should be stable and consistent).

---

Next page: https://externals.io/message/61463.md?page=2
