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

前端 未结 5 549
死守一世寂寞
死守一世寂寞 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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 :)

提交回复
热议问题