5

I'm writing a module and I need to create an auxiliary (helper) function, but I don't know which is the best name to my function.

Should I name my function like:

function _MODULE_my_function

or:

function MODULE_my_function()

I usually use _MODULE_my_function() but there is a standard for this?

There is not reference about this in the Drupal Coding Standards or in the Naming your modules, functions, and variables article.

  • I've never heard of auxiliary functions what are they? Doing a google search, I couldn't find anything concrete. – No Sssweat Sep 2 '16 at 17:20
  • @NoSssweat for my a function that is not a hook implementation – Adrian Cid Almaguer Sep 2 '16 at 17:21
  • Ah ok, hmmm, I've seen both versions, and looks like you're right, there is nothing explicit in the docs. – No Sssweat Sep 2 '16 at 17:34
  • @NoSssweat the name auxiliary it's fine? – Adrian Cid Almaguer Sep 2 '16 at 17:36
  • stackoverflow.com/a/663368/295112 – Kevin Sep 2 '16 at 17:46
4

How to name a function is described in Naming Conventions which says:

Functions should be named using lowercase, and words should be separated with an underscore. Functions should in addition have the grouping/module name as a prefix, to avoid name collisions between modules.

That is valid for hooks and other functions implemented from the module.

For functions that are not hooks, you could also prefix them with an underscore, right before the grouping/module name. If you do it, you are saying to the developers of other modules that the function is not supposed to be called from other modules, but it is private for the module defining it.

  • So, the best way is use an underscore? – Adrian Cid Almaguer Sep 2 '16 at 18:52
  • The keyword being could, so it seems the conclusion is that it's up to you and there is no standard. If you do it, you are saying to the developers of other modules that the function is not supposed to be called from other modules is this somewhere in the docs? or where did you learn this from? Is this some sort of an unwritten rule? or is this PHP rule? – No Sssweat Sep 2 '16 at 19:45
  • @AdrianCidAlmaguer There is no best way. If your functions are not supposed to be called from other modules, you add an underscore to their names. – kiamlaluno Sep 3 '16 at 7:33
  • 1
    @NoSssweat It is a PHP convention that Drupal follows. theme() was renamed _theme() to make clear it was only for internal use, before being removed. I said could because you don't do it for functions that are part of the public API of the module. – kiamlaluno Sep 3 '16 at 7:41
1

I believe I have seen it on a few places that helper functions should be prefixed with an underscore. This is how you distinguish between them and other callbacks / main functions, plus you make it impossible to accidentally name something after a hook.

I've been using this system for years and is in my custom module boilerplate as well.

Hope this helps.

  • thanks Aram I will wait for other answer, for the moment I take your advice – Adrian Cid Almaguer Sep 2 '16 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.