3

So I was going over the PSR coding standards the other day (which I duly follow), and Ive kind of always understood the reasons for everything, and mostly coded that way for everything Ive worked on over the last 10+ years as very similar standards were heavily forced upon me in my university days - it just stuck.

However, I do wonder about this note in the PSR-2 standards:

There MUST be one blank line after the namespace declaration, and there MUST be one blank line after the block of use declarations.

Why is this? I dont object to it, but Im sure there is some kind of background or history to explain, and it never really jumped out to me as why. I guess the obvious reason is legibility, but I have a feeling like there are deeper reasons for this standard.

While I'm at it - does anyone know of any good resources to explain the logic behind all of the PSR guidelines? Its useful to have an official reasoning and history which led to specific standards when trying to convince others to follow them, whom may not be convinced its in their best interests.

Todd
  • 2,824
  • 2
  • 29
  • 39
  • Legibility seems *the* obvious answer. What more reason do you need? Legibility is a great reason in and of itself. Basically the entire PSR-2 is about legibility. You may ask the same question about every other rule in PSR-2, and the answer will usually be *because that's how it's been decided because that's what most people [who drafted the standard] found the most readable.* – deceze Dec 04 '14 at 02:01
  • You should probably omit the last paragraph of your question lest it get closed for being too broad; that said, the rest of the question is kind of asking for opinions. – Ja͢ck Dec 04 '14 at 02:24
  • 1
    The mailing list has a lot of history. [Here is a particularly relevant thread.](https://groups.google.com/forum/m/#!msg/php-fig/v2JsJcdSJ50/6GV9N5f3N98J) – bishop Dec 04 '14 at 02:52
  • @deceze - disagree, PSR is about interoperability and also encourages easily modifiable, extensible, debuggable code. Legibility is just a win that comes along with that. Its far too subjective to be the only basis for a coding standard. – Todd Dec 04 '14 at 18:07
  • @Jack, I feel that asking for a good resource to go deeper into the reasons behind this standard isnt very broad. If I asked everyone to just explain it all to me here, that would be too broad. – Todd Dec 04 '14 at 18:12
  • 1
    The PSR overall is about interoperability, yes. The PSR-2 specifically is "just" a *Coding Style Guide*. It has no bearing on API-level interoperability, it is all about a unified way to style things so they're easily readable by different programmers. It's about "Human Level Interoperability". – deceze Dec 05 '14 at 01:23

2 Answers2

9

Well, what looks better? This:

namespace Foo;
use Bar\Baz;
use Bar\Qux;
function test()
{
}

Or this:

namespace Foo;

use Bar\Baz;
use Bar\Qux;

function test()
{
}

I think it speaks for itself.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • As i said in comments below my answer, this is very subjective (though I agree with it). What is it about the explicit line break declaration that encourages the greater system to be more modular, more easily modified, more easily extended, and easier to debug? Making things look pretty is more of a side affect than the primary goal. – Todd Dec 04 '14 at 18:10
  • @Todd It *is* subjective, but it's also part of a coding standard that was voted upon as a whole. You could argue that "prettier" code is easier to debug and more pleasant to edit, but that's about it; after all, it's a coding standard and not a design philosophy which is often backed up by more tangible reasons than aesthetics :) – Ja͢ck Dec 05 '14 at 01:59
1

I think the best resource you're going to get is the PHP-FIG mailing list, specifically the archive on Google Groups. Here are some interesting discussions related to your question:

My personal recollection is that the standard came about as a blending of PEAR2, PHPCS, Doctrine1 and Flow3 styles. But all this was 5 years ago, and I have forgotten all about it.

bishop
  • 37,830
  • 11
  • 104
  • 139