tags:

views:

47

answers:

2

I have a few modules with some functionality overlap. In accordance with DRY, I'd like to move this out to another location, so I will have less code to maintain. Where is the best place to do this? If I just make a module (and make it a dependency of the ones that need it), will I be guaranteed that the constants, functions and variables defined in it will always be available?

A: 

This is how I've done it in the past. It works nicely, the only issue I've had is that sometimes I need to tweak my utility module's weight (with the Utility module) so it's included before the modules that depend on it.

ceejayoz
A: 

Yes that is a fine method. I usually create a module called "common" with a file structure like:

common
   common.module //include all the inc files here
   common.date.inc //all functions regarding date/time/etc.
   common.text.inc //all functions regarding text
   common.foo.inc
   ...
   common.js //common javascript stuff, namespacing, etc.

I name the functions as ie.

    date_unixtime_to_age()

and not

    common_date_unixtime_to_age()

out of convience, but only for this module. All other module I format as

    modulename_moduleincfile_functionname()

Constants are fine, but try to avoid cluttering the global namespace with variables.

runish
ok. isn't there some way to only include .inc files if they are necessary?
Rosarch
Well generally you want your common functions always to be available and the overhead of doing this is insignificant. Inside other modules you can use the "file" setting from hook_menu() to include a file on a per url basis. See http://api.drupal.org/api/function/hook_menu.
runish