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

webel’s picture

Another situation where inline comments after statements are very useful is when there is a list of parameters like this:

public function __construct(array &$form, $title, $description = NULL) {
    parent::__construct(
        NULL, // TODO type?
        NULL // DO NOT pass $description here, display via description item.
    );

Coder wants it at least like this:

public function __construct(array &$form, $title, $description = NULL) {
    parent::__construct(
        NULL, 
        // TODO type?
        NULL 
        // DO NOT pass $description here, display via description item.
    );

But to make it clear whether the comment applies to the line above or before one has to do this (leave a line)

public function __construct(array &$form, $title, $description = NULL) {
    parent::__construct(
        NULL, 
        // TODO type?
.. leave line here ..
        NULL 
        // DO NOT pass $description here, display via description item.
    );

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.

webel’s picture

Seriously, this rule is just plain annoying, it has to go.

Here is another example. I am calling an operation of a class MenuTabs:

  /**
   * Makes a new tab menuitem with the given page controller and title.
   * 
   * @param \Drupal\ooe\Page\IPageController $pageController
   *   The page controller for the tab menu item.
   * @param string $title
   *   The title of the menu item
   * @param bool $isDefault
   *   Whether this is a default.
   * 
   * @return \Drupal\ooe\Page\IPageMenuItem
   *   The new tab menu item.
   * @throws \Exception
   *   If $isDefault is not a boolean.
   */
  protected function makeTabMenuItem(IPageController $pageController, $title, $isDefault = false) {
..

In the case where fixed values are passed as parameters it is handy to use trailing inline comments:

$this->menuItemDefault = $this->makeTabMenuItem(
        $this->pageControllerBase,
        NULL, // $title: Rely on default tab title assignment by item count
        TRUE // $isDefault
);

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.

webel’s picture

And another useful example of a token/keyword one can search on. DEBUG statements with inline comment marker:

dpm($var,'var'); // DEBUG
webel’s picture

And another case: asserting that a line is right (a good idea ok) with an exclamation mark:

    $n = count($this->menuItems); // !

For what it's worth, I don't like having to leave the space after //, I prefer:

    $n = count($this->menuItems); //!
klausi’s picture

Status: Active » Closed (works as designed)

From 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.

webel’s picture

@klausi

I appreciate your contribution but please do not simply close my issues this way, you haven't addressed the matter at all:

I think this sniff is fine as is.

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.

webel’s picture

Status: Closed (works as designed) » Active
davidwbarratt’s picture

Project: Coder » Drupal core
Version: 7.x-2.2 » 8.0.x-dev
Component: Review/Rules » documentation
Category: Feature request » Bug report
davidwbarratt’s picture

Issue tags: +Coding standards
davidwbarratt’s picture

webel,

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).

webel’s picture

@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.

jhodgdon’s picture

Title: Please relax rule for inline Comments: 'Comments may not appear after statements' » [policy] Relax rule for inline Comments: 'Comments may not appear after statements'
Project: Drupal core » Drupal Technical Working Group
Version: 8.0.x-dev »
Component: documentation » Miscellaneous
Category: Bug report » Plan

Coding standards discussions happen elsewhere.

dawehner’s picture

Haha, this made me laugh :)

tizzo’s picture

Project: Drupal Technical Working Group » Coding Standards
tizzo’s picture

@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.

tizzo’s picture

@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.

webel’s picture

@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).

rudolfbyker’s picture

@webel is absolutely right. The current standard is not IDE-friendly. Having inline TODO tags is a must with most IDEs.

JvE’s picture

The coding standards are for code going public. Feel free to ignore them in code you don't share.

slootjes’s picture

+1 on relaxing this.

euphoric_mv’s picture

+1 on this

pfrenssen’s picture

-1 This is only useful for short temporary comments like // TODO which should not be committed to main branches. The main code should be free of these types of comments.

quietone’s picture

Component: Miscellaneous » Coding Standards
Status: Active » Closed (won't fix)

@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.