What is a line length “soft limit” and how do I interpret this in the PSR-2 convention?

前端 未结 5 550
死守一世寂寞
死守一世寂寞 2021-02-03 18:29

I don\'t understand this part of php\'s PSR-2 convention:

The soft limit on line length MUST be 120 characters; automated style checkers MUST

相关标签:
5条回答
  • 2021-02-03 18:57

    The key to understand all FIG standards is to properly interprate key words used in RFCs.

    There MUST NOT be a hard limit on line length.

    Although no one will put a gun to your head…

    The soft limit on line length MUST be 120 characters.

    … it would be better not exceed the length of 120 characters…

    Lines SHOULD NOT be longer than 80 characters; lines longer than that SHOULD be split into multiple subsequent lines of no more than 80 characters each.

    … but, as a recommendation, try to split lines longer than 80 characters.


    It all comes down to readability. Legibility research specific to digital text has shown that, like with printed text, line length can affect reading speed (saccade). Also, it turns out that the subconscious mind is energized when jumping to the next line (as long as it doesn't happen too frequently). At the beginning of every new line the reader is focused, but this focus gradually wears off over the duration of the line ("Typographie", E. Ruder).

    0 讨论(0)
  • 2021-02-03 19:07

    I'm pretty sure that I've seen another explanation somewhere… I don't know if it's true, but I feel like I should share it =)

    Ok, here it goes: no line should be longer than 80, but that does not account for the indentation. With the indentation, the limit is 120.

    So, if your current indentation is 80 (consider "guards", btw…) — you have only 40 characters remaining. But it your current indention is 40 or less — you can use up to 80 chars for the actual code.

    0 讨论(0)
  • 2021-02-03 19:11

    It's pretty easy:

    • 0-80: Always ok
    • 80-120: Acceptable (if there's a good reason for it, e.g. a string where wrapping it would be ugly)
    • 120+: Lint tools, IDEs, etc. MUST show a warning (but never an error!) since the line might be ugly/unreadable/...
    0 讨论(0)
  • 2021-02-03 19:12

    Let me add something to the above answers

    Editor setup

    I was reading the fig standards as well, and I've stumbled against the same question.

    After giving a deeper look to my IDE settings (PhpStorm), I've found a little note underneath the general "Code style" page, you wouldn't see that note in the php code section.

    The note reports:

    "Specify one guide (80) or several (80, 120)"

    That allowed a better interpretation of the PSR rules, which I'm now reading like this:

    "You shouldn't write lines longer than 80 characters, but you can if you need to. If you exceed the 80 characters, in any case avoid lines longer than 120 characters".

    That is totally possibile if you have two vertical rulers: set them to 80, 120 and you will know when the first limit is exceeded (warning), and also when you have passed the second one (halt!).

    If you use automatic code formatting (I do, and people hates me), then you can set into the php code style page the Hard wrap at: 120. This setting will ensure that the PSR rule if followed.

    Personal toughts

    To be honest, nowadays we have monitors that are a lot WIDER than the old ones, because they use the 16:9 aspect ratio instead of 4:3. So I don't see that urgency to follow the 120 characters rule, because a 16:9 monitor can easily handle more than 200 characters on a line (I've printed 224 on my screen).

    Anyway, I don't like the idea of breaking the rules, if I can, I try to follow them when speaking about coding standards. Maybe I don't see the point of a rule, but if there's a whole team who discussed about that topic, and decided to set the limit at 120 characters, then maybe there's a reason behind that decision that I cannot see, so I just adhere to it. That's the point of a Standard for me.

    Why 120 is a good compromise

    In fact, in the last weeks I've started to split vertically my editor's window, keeping some file types on the left, while some other types (usally JavaScript) on the right. That can be a bit weird at the beginning, I'm the kind of person very disturbed from misaligned stuff. But now I've got used to it, because in practice, it works better like that!

    I've read somewhere that our sight doesn't keep up very well with lines longer than a certain amount, and you can easily understand why. Try to read a document with lines longer than 200, you will loose the focus a lot more easily. Probably you will loose the point a lot of times and read again the whole line...if you can find where it starts!

    Instead a document long between 80 and 120 maxiumum, will have more lines, but will be much more readable, and you can also split the screen horizontally, without having to scroll, most of the time.

    I just wanted to say what I discovered on PhpStorm, and as usual I ended up writing a papyrus. I apologize :)

    0 讨论(0)
  • 2021-02-03 19:18

    There is no hard limit in PSR-2.

    The MUST (NOT) refers to automated style checkers, not to you as a programmer.

    Also note the section that says that no error should be issued when exceeding the soft limit.

    0 讨论(0)
提交回复
热议问题