I feel really strongly about this one. Coder currently triggers on code like this:
$a = $b + 1; // My brief comment well placed
Coder reports:
ERROR | Comments may not appear after statements.
I really find this too "bossy" of Coder (and/or Drupal coding standards) and unnecessary. As long as an inline comment is not too long (does not cause the line to be longer than 80 characters) I can see no good reason for preventing such comments.
One very good reason for permitting such comments is that one can use "tokens" or keywords like TODO, TEST, CHECK etc. after a comment on the same line and filter on it easily (with say the IDEs search functionality) to find all lines in a project "tagged" by a given token/keyword.
$pi = 4.14159; // CHECK!
This is much harder if inline comments must come after the code.
Please relax this rule.
Comments
Comment #1
webel commentedAnother situation where inline comments after statements are very useful is when there is a list of parameters like this:
Coder wants it at least like this:
But to make it clear whether the comment applies to the line above or before one has to do this (leave a line)
The 1st case, with inline comments after the parameters, is much clearer and easier.
Also, one can filter in and IDE on the '// TODO' to find and see the relevant line.
If the comment is below or above the line it refers to the search result is not clear,
one has to go to the comment line in the file then read the line above or below.
Comment #2
webel commentedSeriously, this rule is just plain annoying, it has to go.
Here is another example. I am calling an operation of a class MenuTabs:
In the case where fixed values are passed as parameters it is handy to use trailing inline comments:
It's concise and clear. The alternative is to define temporary local parameter value holders,
or to have the comments on separate lines after each value so Coder does not complain.
Comment #3
webel commentedAnd another useful example of a token/keyword one can search on. DEBUG statements with inline comment marker:
Comment #4
webel commentedAnd another case: asserting that a line is right (a good idea ok) with an exclamation mark:
For what it's worth, I don't like having to leave the space after //, I prefer:
Comment #5
klausiFrom https://www.drupal.org/node/1354#inline : "Comments should be on a separate line immediately before the code line or block they reference."
So the alternative for you is to just comment directly on the line before, as we do everywhere else in Drupal. I think this sniff is fine as is.
Comment #6
webel commented@klausi
I appreciate your contribution but please do not simply close my issues this way, you haven't addressed the matter at all:
Absolutely not. You haven't addressed for example the matter of filtering on keywords on inline comments after a piece of code like // DEBUG, // TODO, // CHECK. In fact you haven't addressed any of the examples I've given (and nor does putting the comment before or after the code line).
My reports are carefully written and well argued with many examples and are possibly of interest to others, I don't mind if you delay attending to them, or offer a different opinion, but simply closing them because you don't agree is not acceptable.
Comment #7
webel commentedComment #8
davidwbarratt commentedComment #9
davidwbarratt commentedComment #10
davidwbarratt commentedwebel,
I've moved the issue because as pointed out in #5, this isn't a problem with the Coder module, it's a problem with the Drupal coding standard(s).
Comment #11
webel commented@davidwbaratt Just a quick reply to say that I am following all coding standards and Coder related tickets, but I am not offering any further input at the moment. Thanks for your time in considering this and other coding standards related issue reports.
Comment #12
jhodgdonCoding standards discussions happen elsewhere.
Comment #13
dawehnerHaha, this made me laugh :)
Comment #14
tizzo commentedMoving this issue to the Coding Standards queue per the new workflow defined in #2428153: Create and document a process for updating coding standards.
Comment #15
tizzo commented@webel I think you misunderstand the recommendation. The current standard pushes you to put the comment *before* the related code so that as you're reading you have that context before encountering the offending line rather than after it.
I'm not speaking for the TWG right now but personally I strongly prefer the current standard.
Comment #16
tizzo commented@webel I think you misunderstand the recommendation. The current standard pushes you to put the comment *before* the related code so that as you're reading you have that context before encountering the offending line rather than after it.
I'm not speaking for the TWG right now but personally I strongly prefer the current standard.
Comment #17
webel commented@tizzo suggested 'I think you misunderstand the recommendation.'
Implied telepathy aside, I don't misunderstand it, I understand it fully. I just don't like it as the only option.
There are appropriate and different acceptable usages of comments:
1. Before a line of code.
2. At the end of a line of on the same line (especially for briefly "tagging" IDE-searchable matters).
3. After a line of code.
After over 35 year of coding I think I've seen every acceptable variant by now.
The Drupal coding standard is unnecessarily limiting (and w.r.t. 2. above not particularly IDE friendly).
Comment #18
rudolfbyker@webel is absolutely right. The current standard is not IDE-friendly. Having inline TODO tags is a must with most IDEs.
Comment #19
JvE commentedThe coding standards are for code going public. Feel free to ignore them in code you don't share.
Comment #20
slootjes commented+1 on relaxing this.
Comment #21
euphoric_mv commented+1 on this
Comment #22
pfrenssen-1 This is only useful for short temporary comments like
// TODOwhich should not be committed to main branches. The main code should be free of these types of comments.Comment #23
quietone commented@webel, thanks for the suggestion.
There hasn't been discussion here in 8 years and the consensus at the time was to keep the existing coding standard. Therefor I am going to close this as outdated. If anyone disagrees, just re-open and comment. Thanks.