Standards for Namespaces (Sub-Modules and Functions)

Events happening in the community are now at Drupal community events on www.drupal.org.
zzolo's picture

The following two things are well known conventions but I was unable to find any documentation on d.o for them:

  1. Sub-Module names.
    These are modules within modules (which are really projects on d.o). The convention, as I understand it is: {core_module_name}_{sub_module}. This basically ensures that no sub-modules will use the same namespace as contributed modules or sub-modules.
  2. Function Names.
    We know that all hooks have to be prefixed with the module name, but it is only common practice to make any other public or private function prefixed with module name (as well as the leading underscore (_) for "private" functions): {module_name}_{action}. Again, this basically handles name collisions with other contributed modules. This standard could also include how to form function names, for example as suggested by webchick: {module_name}_{verb}_{thing_being_verbed}

So, what I need from this great group is:

  1. Are these agreed upon standards?
  2. Where is the best place for this documentation?
    There is: Module Development, but even under that, there is no general module development guidelines (instead of specific version)
    And: Standards and Security
    Or: some other.

Thanks.

Comments

What was your reference for

morbus iff's picture

What was your reference for "as suggested by webchick: {module_name}{verb}{thing_being_verbed}"? In my experience, it's actually the opposite of that - node_save and node_delete, not delete_node and save_node (so, {module_name}{thing_being_verbed}{verb}).

IRC

zzolo's picture

Hey Morbus,

That's just a quick IRC discussion we had last night. I do not think it is the de facto standard, just throwing things out there for discussion.

--
zzolo
zzolo.org

--
zzolo

Well, core does it as

morbus iff's picture

Well, core does it as {thing_being_verbed}{verb}, and I suspect that a number of modules have taken a similar stance too. I'm not thrilled with a change of {verb}{thing_being_verbed} (core or otherwise), so would vote to "Keep Things The Same" and "Accept That How Core Does It Is An Unspoken Standard" and "Unspoken Standards Should Be Spoken" ;)

Not a standard, it's a failsafe measure.

sun's picture
  1. Sub-Module names.
    These are modules within modules (which are really projects on d.o). The convention, as I understand it is: {core_module_name}_{sub_module}. This basically ensures that no sub-modules will use the same namespace as contributed modules or sub-modules.

Yes, prefixing a module with the dependent/required module groups them nicely together in the filesystem (think views*, og*, ec*, ...), but is only a recommendation, no standard, convention, or requirement.

  1. Function Names.
    We know that all hooks have to be prefixed with the module name, but it is only common practice to make any other public or private function prefixed with module name (as well as the leading underscore () for "private" functions): {module_name}{action}. Again, this basically handles name collisions with other contributed modules. This standard could also include how to form function names, for example as suggested by webchick: {module_name}{verb}{thing_being_verbed}

ALL functions in a module need to be prefixed with the module's name to prevent PHP fatal errors. Private functions (if required) use an additional _ prefix before the module name prefix in their name. On that note, your CVS application will be rejected if your code contains functions that are not prefixed with the module name. (btw: The same applies to global variables.)

The common standard for function names is: <module>_<object>[_<verb>]

  • mymodule_myform
  • mymodule_myform_validate
  • mymodule_myform_submit
  • mymodule_mynodetype_form
  • mymodule_mynodetype_view
  • mymodule_nodeapi('verb', ...) (in D6)
  • mymodule_node_verb (in D7)
  • ...

Daniel F. Kudwien
unleashed mind

Daniel F. Kudwien
netzstrategen

Not standards?

zzolo's picture

@sun, thanks for the comments.

So you are saying these should not be standards (whether enforced or not), even though they will cause PHP errors and/or weird Drupal behavoirs? I guess I am confused on what you are suggesting here.

Also, if you are saying that something is just a recommendation, then what differentiates that from a standard? Standards are enforced either through code or community action?

I think the sub-module thing should be a standard. The case I am looking at is where a module has a sub-module that is the same name as my module. My module is a project on Drupal and is obviously already taken (not a case where sub-modules happen to have the same name).

As far as function names, you say this is "needed" as if this is enforced. How is it enforced? Is it only enforced in .module files? As I have a case where someone has definitely used function names without the module prefix, but it may be outside of .module files.

--
zzolo
zzolo.org

--
zzolo

it's never too late...

rafamd's picture

Current coding standards state all function names should be prefixed with the module/grouping name (don't know what "grouping" could be, but nevermind that). (Reference: http://drupal.org/coding-standards#naming)

The standard could indicate the <object>[_<verb>] naming convention mentioned here though (which actually doesn't).

I couldn't find a standard for sub-module naming and also think there should be a standard about it. The proposed sounds good.

Is there a standard for contributed module names?

gisle's picture

I've not been able to find a standard for picking a name for a contributed module.

For instance, there is a module named Help in Drupal core. And then there is a contributed module named Help.

I think the core namespace should be protected, so that creating a contributed project with the same short name or human readable name as a core module should be blocked.

Coding standards

Group organizers

Group categories

Status

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week