Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Stitcher.io Blog:
Where a curly bracket belongs
Jan 19, 2018 @ 16:38:45

On the Stitcher.io blog they've shared a post that makes some suggestions about where a curly brace belongs and how that might differ from situation to situation.

Dedicating a whole blogpost to curly brackets might seem like overkill but I believe it's worth thinking about them. Not just because of one curly bracket, but because there's a bigger message in all this. Thinking about how we read and write code not only improves the quality of that code, it also increases our own and others ease of mind when working with it. It can improve the fluency of your work and free your mind to think about real important stuff.

[...] I wrote about visual code improvements a while back in a previous blogpost about cognitive load. Today I want to focus on that one little, yet very important character in our codebase: the curly bracket. More specifically, we're only going to look at the opening curly bracket, because there's little to no discussion about the closing one.

The post goes on to show several different example situations and where they think the "most correct" placement for the curly brace is. They alos talk about the difference between their use on constructors versus control structures. The main recommendation, however, is to keep things consistent across the codebase.

tagged: curly bracket constructor opinion location consistency

Link: https://www.stitcher.io/blog/where-a-curly-bracket-belongs

Sameer Borate's Blog:
The benefits of colon syntax for control structures
Jan 15, 2009 @ 14:48:33

In this recent post to his CodeDiesel.com blog, Sameer is promoting the use of colons in control structures over curly braces.

PHP offers a alternative syntax for some of its control structures- if, while, for, foreach, and switch, where you change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively. [...] With the colon syntax you just have to match a ‘if’ with a ‘endif’ or a ‘for’ with a ‘endfor’. When you are mixing HTML with PHP in web pages the code can become quite dense and confusing if it uses a lot of braces.

An example is included showing its use in "if" and "white" statements. With most editors and IDEs supporting brace matching and code folding, this is less of an issue, but it can help make for cleaner code.

tagged: control structure colon curly brace alternative syntax advantage

Link:

Mike's Blog:
Time to say Good Bye
Nov 17, 2005 @ 11:38:33

According to this post on Mike's blog today, it's "time to say goodbye" to curly braces.

Yeah, it may come suddenly, but it's time to say good bye to curly braces used as string indexing operator.

PHP-5.1 will issue an E_STRICT error, and PHP-6 will probably don't know this syntax any more.

I don't know why, but I've got some strange sentimental feelings, maybe because I've never used those other brackets for string indexing...

The method he's talking about here is the $string{0} format, and it's making way for a unified string/array operator. It won't break things badly, though - in PHP 5.1 it'll just toss out a non-fatal error to let you know. But watch out, by PHP6, this functionality will be gone...

tagged: curly braces string 5.1 curly braces string 5.1

Link:

Mike's Blog:
Time to say Good Bye
Nov 17, 2005 @ 11:38:33

According to this post on Mike's blog today, it's "time to say goodbye" to curly braces.

Yeah, it may come suddenly, but it's time to say good bye to curly braces used as string indexing operator.

PHP-5.1 will issue an E_STRICT error, and PHP-6 will probably don't know this syntax any more.

I don't know why, but I've got some strange sentimental feelings, maybe because I've never used those other brackets for string indexing...

The method he's talking about here is the $string{0} format, and it's making way for a unified string/array operator. It won't break things badly, though - in PHP 5.1 it'll just toss out a non-fatal error to let you know. But watch out, by PHP6, this functionality will be gone...

tagged: curly braces string 5.1 curly braces string 5.1

Link:


Trending Topics: