-3

How does the below piece of code relate to the Drupal coding standards? Notice that this function is not returning anything.

$a = foo();

function foo() {
  // …
  drupal_func();
}

Somebody claims that:

  1. the assignment to the variable is for the future because that way it is easier later to refactor foo(). However, the code does not use $a, now.
  2. Drupal people often do this kind of practice, which is even in Drupal core.

Is this valid?
Should this be kept in the code?

We agreed to keep Drupal coding standards in our project. I was doing a code review of my friend's code and he insisted on keeping that code (using arguments that it is used inside Drupal core, but I couldn't find an example myself) while I was trying to convince him to remove $a. We couldn't reach an agreement, so I decided to reach out for help.

2
  • I can't see the reason that $a is necessary. It is not usual in Drupal, but ctools plugins is using this way to define the plugins meta.
    – Jimmy Ko
    Commented Jun 28, 2016 at 10:49
  • Welcome to Drupal Answers :) It would help if you could clarify what you're asking here - do you need to know if that code violates Drupal coding standards, for example? Are you asking why there are examples of this in Drupal core code? If so, can you provide those examples? If not, why not? Why would what your friend claims, if true, even matter? Getting some information on those points would be really useful, otherwise it's hard to understand what an answer could look like
    – Clive
    Commented Jun 28, 2016 at 10:55

2 Answers 2

2

A lot of this is my opinion, I should say that up front...

Drupal's coding standards don't have any provisions for the type of pattern you've described.

You may be falling foul of "Naming Conventions", because you haven't prefixed the global function with the name of the module that "owns" it, but apart from that, and other small points that are only valid because the code is only an example, no, you don't seem to be breaking any specific Drupal standards with that code.

But just because it doesn't specifically break one of the generalised standards doesn't make it "good" code. Polluting the global scope is almost always bad, especially just for the sake of it with an unused var, and assigning a NULL returning function to a variable is an obvious code smell to me.

In short, it's a weird pattern, not one that I'm familiar with in any language/framework (though I'm sure it's been used, most things have). There are sort-of-similar patterns used elsewhere, e.g. the CTools plugin system, but that works because the calling code includes the file and actually makes use of the $plugin variable in scope. So the vars is used; and NULL functions aren't assigned to vars in that context either.

I highly doubt you'll find an example of this pattern in core, but if your friend can provide one, I'd be interested to see it.

2
  • Thanks for the answer. I think with all the down votes and your answer I've got the answer I needed: "do not do that" would be to put it short.
    – user62935
    Commented Jun 28, 2016 at 12:35
  • 1
    I'm a bit surprised by the downvotes actually, I didn't see much wrong with the question myself. Seemed like a reasonable thing to ask. Ah well, each to their own :)
    – Clive
    Commented Jun 28, 2016 at 12:36
0

There is one module Coder for check the drupal standard for every module and its file.

Configure it properly and after run the test it will show you the errors and suggestions.

You can run the test for your custom module directly from the module listing page, there is an option besides your module to run the test.

Install this Coder Module from the below link.

https://www.drupal.org/project/coder

Just try this module, it is really helpful for developers.

Hopefully this may help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.