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.

8
  • I've never heard of auxiliary functions what are they? Doing a google search, I couldn't find anything concrete.
    – No Sssweat
    Commented Sep 2, 2016 at 17:20
  • @NoSssweat for my a function that is not a hook implementation Commented Sep 2, 2016 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
    Commented Sep 2, 2016 at 17:34
  • @NoSssweat the name auxiliary it's fine? Commented Sep 2, 2016 at 17:36
  • stackoverflow.com/a/663368/295112
    – Kevin
    Commented Sep 2, 2016 at 17:46

2 Answers 2

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.
This is a practice followed from PHP which is also followed from Drupal. For example, theme() had been renamed _theme(), before being removed, to make clear it was a private functions modules should not call. It's described in theme() renamed to _theme() and should not be called directly.

The direct callability of theme() was removed in favor of building render arrays consistently. It has been renamed to _theme() and is for internal use only. Build render arrays instead of using _theme() in your code for the following reasons.

Calling the _theme() function directly raises several issues:

  • It circumvents caching.
  • It circumvents defaults of types defined in hook_element_info(), including attached assets
  • It circumvents the pre_render and post_render stages.
  • It circumvents Javascript states information.
4
  • So, the best way is use an underscore? Commented Sep 2, 2016 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
    Commented Sep 2, 2016 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.
    – avpaderno
    Commented Sep 3, 2016 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.
    – avpaderno
    Commented Sep 3, 2016 at 7:41
2

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.

0

Your Answer

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

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