Problem/Motivation
Hard and soft limits on line length is making the code harder to read and read (not the docblocks, they benefit from a 80 char max lenght soft limit).
Benefits
If we adopted this change, the Drupal Project would benefit by having code that was easier to read and understand.
Three supporters required
- https://www.drupal.org/u/pasqualle (29 Sep 2020 at 09:15)
- https://www.drupal.org/u/mherchel (10 January 2021 at 15:19)
- https://www.drupal.org/u/moshe-weitzman (12 January 2022 at 19:40)
Proposed changes
Provide all proposed changes to the Drupal Coding standards. Give a link to each section that will be changed, and show the current text and proposed text as in the following layout:
1. https://www.drupal.org/docs/develop/standards/php/php-coding-standards#l...
Current text
In general, all lines of code should not be longer than 80 characters.
Proposed text
In general, all lines of code should not be longer than 80 characters. But there are no hard or soft limits. Just make sure your code is simple to read and understand.
Remaining tasks
An MR with changes to all the languages regarding the 80 character limit.
Create this issue in the Coding Standards queue, using the defined templateList three supporters- Create a Change Record
- Review by the Coding Standards Committee
- Coding Standards Committee takes action as required
- Tagged with 'Needs documentation edits' if Core is not affected
- Discussed by the Core Committer Committee, if it impacts Drupal Core
- Documentation updates
- Edit all pages
- Publish change record
- Remove 'Needs documentation edits' tag
- If applicable, create follow-up issues for PHPCS rules/sniffs changes
For a fuller explanation of these steps see the Coding Standards project page
==== Original issue still here because it's dang well written, offers practical advice and is slightly different (it calls for a 120 char hard limit) ===
https://www.drupal.org/docs/develop/standards/coding-standards#linelength
Please increase the line length limit in coding standards.
I know, if you do google search, many blog posts still in favor to keep the 80 characters limit.
The problem I have with the limit:
- The code gets ugly with many unnecessary line breaks.
- The code is rewritten to use a "stupid" style to comply with the standard. Like the ternary operator is not used just because of the code line would be over 80 chars. So it is rewritten to if statement.
- The code comments are shortened just to comply with the standard. I am sure everyone does it if the comment is between 81-85 chars.
So I am limited at least on these 3 things, and I spend too much time on these instead of solving real problems.
The D9 core patch in #2572709: [meta] Fix 'Drupal.Files.LineLength' coding standard does not improve anything on the code readability.
Drupal can be the force to change the 80 chars limit for every developer. There should be a limit, as nobody reads code outside of the screen size, but it should not be 80 today.
proposal
Change the standard to "maximum line length of 120 characters"
Customised length
For those projects that want to start using a longer allowed length before this standard is changed, you can add these to your phpcs.xml or phpcs.xml.dist file
<!-- Increase the allowed line length for comments. -->
<rule ref='Drupal.Files.LineLength'>
<properties>
<property name="lineLimit" value="120"/>
</properties>
</rule>
<!-- Increase the allowed line length for inline array declarations. -->
<rule ref="Drupal.Arrays.Array">
<properties>
<property name="lineLimit" value="120"/>
</properties>
</rule>
Comments
Comment #2
pasqualleComment #3
alexpottLooking for inspiration elsewhere. There's the PSR12 coding standard. Which says:
Comment #4
alexpottAlso worth reading are https://nickjanetakis.com/blog/80-characters-per-line-is-a-standard-wort... and https://hackaday.com/2020/06/18/ask-hackaday-are-80-characters-per-line-...
Comment #5
pasqualleThanks for the links. The response from Linus is very entertaining:
https://lkml.org/lkml/2020/5/29/1038
Comment #6
pasqualleAnother problem: even if a code line is 80 characters, it still can take up more space in an IDE like PhpStorm with parameter hints enabled (which is by default).
So even if a hard wrap is displayed in the IDE at 80 chars, it is not visible where the 80 chars limit is. Therefore every line with a parameter hint needs to be checked one by one (or the hints need to be disabled).
Comment #7
jonathan1055 commentedWhilst working on the inline array Array.LongLineDeclaration coding standard I did some analysis on how the numbers would be affected when the limit is increased - see #3116859-26: [meta] Fix Drupal.Array.Array.LongLineDeclaration coding standard from #26 to #32
I support the case for increasing the limit to 120 for D9 and keeping it at 120 for D10. We also have to ask why we check this just for inline arrays and for comments, but not for other code lines.
Comment #8
pasqualleI like the "around 100-130 characters" rule better than a hard limit.
For me it is not ok to write a 120 characters long if statement, even the rule would allow it. The conditions should be refactored to be more readable.
"around 100-130 characters" means, if the line is longer than 100 then you should check if the code is readable, if the line is longer than 130 then you need a good excuse to have that long line.
Comment #9
mherchelHuge plus one on this:
+1
I'm creating some JS for Olivero, and to pass linting I have to re-write
as
The first example is far more readable than the second example, IMO.
Comment #10
crasx commentedIn #9 the second piece of code is more readable on drupal.org, while the first piece of code is more readable in an IDE. Might be something we want to address (design-wise) if we make this change.
+1 from me on this as well, code is more art than science anyway
Comment #11
gapple+1
I created #3039007: Relax the rule that arrays may not span more than 80 characters some time ago because the Content Security Policy module's admin form has a pretty deep form array, and altering the parents hierarchy of elements and retrieving values from the form state both end up crossing the 80 character limit.
Comment #12
pasqualleMoving to the right queue. Not sure if it helps..
Comment #13
moshe weitzman commented+1 here too
Comment #14
borisson_This is linked from #1539712: [policy, no patch] Coding standards for breaking function calls and language constructs across lines, and it is very related. This change however is about not just for function calls and language constructs but for our entire code.
I think the reasons given in the IS here are mostly preference
I don't think this is an objective measurement, but I don't disagree.
I disagree with this, if statements are very often a lot clearer and are a lot easier when scanning code. I think a lot of people overuse ternary operators to make their code shorter and often think that readability is more important.
This is true, it sometimes make it worse to read, so this is a good argument I think.
I disagree with this suggestion, especially because it differs between versions and because it uses around, I'd prefer to have hard requirements here, because that makes checking with phpcs possible.
When looking at #3, there is also a mention of there not being a hard limit, but in practice the phpcs prs12 implementation has a hard limit at 120 chars, and I think that's a good idea personally, this is also mentioned in #7.
I also agree with #10, the code blocks on d.o are limited to 80 chars wide now, so these will have to change as well to allow for the 40 extra chars.
I think we have to change the language on this issue to allow all code to be maximum of 120 chars.
While we can copy the wording of psr12 here, I think the soft limit will in practice be a hard limit because of phpcs.
Comment #15
hudriI also want to join the +1 force here, totally in support for PSR-12. The 80 char limit is harmful nowadays.
When I write my code, I always try to write it readable in my IDE. It is an instinctive, natural behavior to set line breaks to support your own thinking process. And then I run phpcs and 80% of the reports I'm getting are line length issues, and a lot of those are in between 80 and 120 chars. This is frustrating and feels like mutilating your own code, making it much harder to read again, just to follow ancient coding standards.
Comment #16
chrissnyder+1 here too
Comment #17
steven snedker commentedI see absolutely no reason for a 40, 80 or 120 characters hard limit.
If a visually impaired developer chooses to develop on a 14" laptop using a huge font-size, this fine human will just have to make his editor wrap the code.
Paqualle pointed to this response from Linus:
So I'm +1 for no hard limits and +1 for no soft limits. PHPCS unable to do soft limits? Not our problem.
Just ditch the ancient character limits.
I'm sure this will lead to happier developers and more legible code for +97% of the developers.
Comment #18
ressa+1 for increasing it to 120 lines, or even no limits. Core contributors, contrib module maintainers, and README.md editors spend too much time on this tedious task, which is essentially a relic of the past.
Case in point is #3354774: Fix the issues reported by PHPCS from today:
The 5 offending lines, which would not have been an issue with max. 120 lines limit, or none:
Comment #19
chrissnyderThere does not seem to be much opposition to increasing the line length. Who needs to be involved in this issue thread to make a change to the coding standards?
Comment #20
ian.ssu commented+1 for increasing to 120 lines. The core source does not appear to be hindered by this excessively strict rule.
Echo above from @ChrisSnyder question: What needs to happen to get movement on this issue?
Comment #21
jonathan1055 commentedIf you are on Drupal Slack you could raise it in the #coding-standards channel
https://app.slack.com/client/T06GX3JTS/C02LJCF78E8
Comment #22
bernardm28 commented+1 for this issue.
Laravel follows PSR-2 and that states https://www.php-fig.org/psr/psr-2/
https://laravel-news.com/php-codesniffer-with-laravel
Joomla states
https://developer.joomla.org/coding-standards/basic-guidelines.html#:~:t....
Even WordPress has more room there.
https://developer.wordpress.org/coding-standards/wordpress-coding-standa...
Let's update this 10 year old issue.
https://www.drupal.org/project/drupal/issues/935284
Comment #23
jonathan1055 commentedUpdated issue summary with the current consensus of the new line length.
Also added info on how to change the allowed length in a project's phpcs.xml(.dist) file if you want to start using this anyway.
Comment #24
jonathan1055 commentedAdded second example for array-wrapping line length.
Comment #25
pfrenssen+1
Comment #26
joachim commentedI'm leaning towards a -1 on this.
> - The code comments are shortened just to comply with the standard. I am sure everyone does it if the comment is between 81-85 chars.
Wide text is harder to read. This is the reason newspapers are printed in columns and well-designed web pages don't let their text go to the full width of the window.
Coding standards are adding more line breaks generally -- in function declarations, function calls, and so on. These make code more readable, and produce cleaner diffs too.
Comment #27
pasqualleI would like to see a code comment example where this is true.
Comment #28
mherchelAgree that for reading stories and normal text, 80 should be the max (I actually prefer less). However reading normal text is very different than reading code.
Comment #29
hudriI'm with @mherchel here. Stories usually don't have a line break after each sentence. But code usually has a linebreak after each statement anyway, making it very different to read than editorial text.
Comment #30
joachim commentedI'm thinking of the large paragraphs of docblocks we have in our codebase. E.g. -- and this is only an average sized one -- https://git.drupalcode.org/project/drupal/-/blob/11.x/core/lib/Drupal/Co...
Comment #31
alexpottThis is why we should adopt something similar to #3. Where we recommend 80 characters or less but if there good reasons you can be longer. The whole hard limit at 80 characters sometimes forces the one liner for methods to become really bad documentation. In those instances we have our priorities wrong. Adhering to the line limit instead of correctness is the wrong way around.
Comment #32
quietone commentedThe Coding Standards Committee has developed a custom issue template to assist in managing issues efficiently. We have already found that it's use is helping. For issues created before the template was in use I am adding the new template to the Issue Summary and asking everyone here to help convert to it..
Thank you for your help!
Comment #33
steven snedker commentedJoachim is absolutely correct regarding docblocks (#29). They are easier to read when only 80 chars wide. "Make your docblocks easier to read by aiming for a width of 80 chars" is clearly a sensible recommendation.
But, using the exact same example, everyone is way happier with the 96 char line at
https://git.drupalcode.org/project/drupal/-/blob/11.x/core/lib/Drupal/Co...
than with some chopped up, harder to read, 80 char monstrosity. And also already allowed per "Control structure conditions may exceed 80 characters, if they are simple to read and understand".
The same goes for mherchels 130 char line in #9. Way better than any chopped up 80 or 120 char monstrosity.
Also already allowed per "Control structure conditions may exceed 80 characters, if they are simple to read and understand".
Let's be pragmatic thinkers and doers and extend "may exceed 80 characters, if it is simple to read and understand" to everything.
No hard or soft limits. Just (modern) common sense recommendations on legibility.
Comment #34
jonathan1055 commentedReinstating the full 9 step "remaining tasks", as this serves as the checklist for the process.
Comment #35
borisson_I created a draft change record, as this is the next in the steps of remaining tasks.
I think consensus in this issue is a soft limit of 120 characters without a hard limit at all.
Comment #36
quietone commentedThere is core issue about the line length in js files, #2924755: Set max line length for JavaScript code comments to 80 (rather than 100). From that I found that for js files the maximum line length checks are not run,
"max-len": "off",.I checked that with the code sample from #9. That did not result in errors about line length. Presumably, there are other config options about the formatting of if statements at work for that.
In summary,
Comment #37
quietone commentedOr, in other words, this change is documenting the current practice. And if that is correct, do we need a change record? I think not.
Comment #38
borisson_I think it's still good to create a change record, because even though the rule was not enforced, I would frequently push back on code > 80 wide, which now no longer should happen at all.
Comment #39
quietone commentedAh, good point. Thank you.
Comment #40
quietone commentedOK, more investigation and there are 18 other uses of 80 in the coding standard. Those need to be checked and probably adjusted. Plus, the text in the issue summary is in the PHP section. So, this does need work to get the documentation updated for all languages.