If you’re reading this blog post, you probably already know what PSR-2 is, but if you don’t, you might check out the PSR-2 coding standard as accepted by the PHP Framework Interoperability Group (FIG). PSR stands for “Proposed Standards Recommendation”, and PSR-2 is FIG’s accepted standards for PHP coding style. PSR-2 creates coding style standards for teams of PHP developers that work with frameworks, and most of what they propose for standards is acceptable. So far, I just have one major complaint.
I’ve now been working with PHP for seven years, and I’ve changed coding styles over time. In fact, a few years back I made a major change, switching from an indent style similar to the one PSR-2 standardized, to the Allman indent style. I have specific reasons for using the Allman style, and changing back to what I believe is an inferior indent style seems counterproductive. Let me explain why I have formed this opinion, then perhaps you can comment and help me understand why the PSR-2 style is beneficial to you.
There was a time when I liked using an indent style like the one PSR-2 has accepted as a standard. It made my code more compact. I could look up and down the screen at 40 or 50 lines of code. Sometimes seeing more code may be beneficial. Especially with widescreen monitors, I can see that losing a few lines of code to a few opening braces on their own lines may not be very nice. I don’t use widescreen monitors, so this isn’t so much of a problem for me. So why is the Allman indent style important to me?
Using the Allman style is important, at least for me, because it aligns the opening and closing braces with each other. Since these braces are squarely lined up with each other, and since most text editors highlight paired braces, it’s easier to see where one block of code starts and ends. Because blocks of code are more clearly defined by the braces, code comments that are at the top of the block have a more natural place to exist. This indentation style lends itself to more organized code. Allman style code could be considered “framed” by it’s braces.
PSR-2’s style leaves opening braces in random positions. Depending on what’s in front of the opening brace, paired braces may not even be visible at the same time. I’m frequently highlighting braces as I code, to find out where the other one is, and if I can’t see it I may not easily be able to determine a desired point in my code. The Allman style doesn’t have this problem.
In conclusion let me say that I’m all for many of FIG’s proposed and accepted PSRs. I know of, and have respect for most of FIG’s voting members. I just don’t understand their logic when accepting the PSR-2 indent style. In what way is this indent style better than the Allman style? I question whether the voting members of FIG just voted based on their own personal preferences, or if they actually discussed the benefits of the indent style they standardized.