5

One thing which I am trying to understand why PSR2 has curly brackets for class and method on new line and for the rest not. I would say it's much easier to read code if curly brackets are always on a new line.

class Foo extends Bar implements FooInterface
{
    public function sampleFunction($a, $b = null)
    {
        if ($a === $b) {
        }

Can somebody explain what is the logic behind:

Opening braces for control structures MUST go on the same line, and closing braces MUST go on the next line after the body.

deceze
  • 510,633
  • 85
  • 743
  • 889
Zdenek Machek
  • 1,758
  • 1
  • 20
  • 30
  • 1
    PSR-2 is an opinion. It isn't right or wrong; it's just one way of doing things. It was decided by concensus opinion and based on the various existing coding standards across the PHP world, but plenty of people were doing things differently, some even the way you suggest. They've mostly adapted to the standard now. But if you don't like it, you don't have to follow it. You will, however, find most other people are following it these days, so you'll be going out on a limb. – Simba Nov 19 '15 at 15:18

2 Answers2

4

My understanding of the reasoning here is basically to prevent blocks of contiguous code from becoming excessively long.

For example, say I have an 50 line function written in PSR-2 style. If that function contains 10 if() conditions and 5 loops, then my function under your rules would be 65 lines long.

Since it's a single function, I need to be able to work with it as a single entity, so it is really helpful to be able to see it all at once. With a 50-line function, that's feasible. It gets a lot harder if the function is 65 lines long.

In fact, looking at my editor window right now, I can see 30 lines at a time, so even a 50-line function is a bit too long to manage easily without resizing or removing the docked panels in my IDE.

Now I know that clean code rules would say that a 50-line function is far too long anyway and needs to be refactored. I'd agree with that. But guess what: functions with 50 lines or more are pretty common; I have to deal with them all time time, and the less unnecessary white space they have in them, the easier they are to work with.

Ultimately, however, this is just one interpretation of the root reasons behind it.

The real reason PSR-2 is this way is because that's how the consensus of opinion fell. They surveyed all the frameworks and others who had established PHP coding standards. All those different standards existed because people had sat down and thought about how best to format their code. They didn't agree because it is an opinionated subject, but there were broad areas of agreement and that was what went in to forming the PSR standards.

Simba
  • 4,952
  • 3
  • 19
  • 29
2

Several reasons.

  1. Democracy/Popular Opinion. A poll was taken from amongst the PHP-FIG and this brace style is what the majority of those projects used. You can read more, by Paul M Jones here (search for "braces").
  2. Historical usage. This style is known as K&R, for Kernighan and Ritchie, which is widely prevalent in C (for various reasons, see the Wikipedia article). PHP, like many languages, is heavily influenced by C, both in syntax and style. (Also, PHP is written primarily in C!)
  3. Visually distinction. When scanning large sections of code, it's easier to spot where functions and classes start, and distinguish them from control blocks.
haz
  • 1,549
  • 15
  • 20