Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 warn but MUST NOT error at the soft limit.

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.

I thought "soft limit" meant "try to stick to this limit, but a few characters extra are not a problem". But then I read the second line and now I don't get it.

like image 226
ChocoDeveloper Avatar asked Feb 19 '13 16:02

ChocoDeveloper


People also ask

What is Max line length?

Traditional line length research, limited to print-based text, gave a variety of results, but generally for printed text it is widely accepted that line lengths fall between 45 and 75 characters per line (cpl), though the ideal is 66 cpl (including letters and spaces).

Is PSR 2 deprecated?

Deprecated - As of 2019-08-10 PSR-2 has been marked as deprecated. PSR-12 is now recommended as an alternative. This guide extends and expands on PSR-1, the basic coding standard. The intent of this guide is to reduce cognitive friction when scanning code from different authors.

What is PSR code?

The PHP Standard Recommendation (PSR) is a PHP specification published by the PHP Framework Interoperability Group (PHP-FIG). It serves the standardization of programming concepts in PHP. The aim is to enable interoperability of components. The PHP-FIG is formed by several PHP frameworks founders.

How many characters are in the standard line length?

Ruder concluded that the optimal line length for body text is 50–60 characters per line, including spaces (“Typographie”, E. Ruder). Other sources suggest that up to 75 characters is acceptable.

What is the coding style guide PSR 1?

Overview 1 Code MUST follow a “coding style guide” PSR [ PSR-1 ]. 2 Code MUST use 4 spaces for indenting, not tabs. 3 There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines SHOULD be 80 characters or less. More items...

Is there a line length limit for a namespace declaration?

There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines SHOULD be 80 characters or less. There MUST be one blank line after the namespace declaration, and there MUST be one blank line after the block of use declarations.

Is there a limit on the length of a line?

There MUST NOT be a hard limit on line length. The soft limit on line length MUST be 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. There MUST NOT be trailing whitespace at the end of lines.

What is PSR-1?

This guide extends and expands on PSR-1 , the basic coding standard. The intent of this guide is to reduce cognitive friction when scanning code from different authors. It does so by enumerating a shared set of rules and expectations about how to format PHP code.


2 Answers

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/...
like image 91
ThiefMaster Avatar answered Sep 19 '22 10:09

ThiefMaster


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.

like image 39
Johan Avatar answered Sep 20 '22 10:09

Johan