ChrisP
ChrisP

Reputation: 5952

How to organize subroutines for use by multiple commands?

I am working on creating a package with two new commands, say foo and bar.

For example, if foo.ado contains:

program define foo
    ...
    rex
end

program define rex
    ...
end

But my other command, bar.ado, also needs to call rex. Where should I put rex?

I see the following few options:

  1. Create a rex.ado file as well.
  2. Create a rex.do file and include it from within both foo.ado and bar.ado using include "`c(sysdir_plus)'r/rex.do" at the bottom of each file.
  3. Copy the code into both foo.ado and bar.ado, which seems ugly because now the code must be maintained in two places.

What is best practice for organizing subroutines that are needed by both foo and bar?

Also, should the subroutine be called rex, _rex, or something else — maybe _foobar_rex — to indicate it is actually a sub-command that foo and bar depend on to work correctly rather than a separate command intended to stand on its own?

Upvotes: 0

Views: 101

Answers (1)

user8682794
user8682794

Reputation:

Create a rex.ado file as well

Your question is a bit too broad. Personally, I would go with the first option to be safe, although it really depends on the structure of your project. Sometimes including rex in a single ado file may be enough. This will be the case, for example, if foo is a wrapper command. However, for most other use cases, including two commands sharing a common program, i strongly believe that you will need to have a separate ado file.

The second option is obviously unnecessary, since the first does the same thing, plus it does not have to load the program every single time you call it. The third option is probably the worst in a programming context, as it may create conflicts and will be difficult to maintain down the road.

With regards to naming conventions, I would recommend using something like _rex only if you include the program as a subroutine in an ado file. Otherwise, rex will do just fine and will also indicate that the program has a wider scope within your project. It is also better, in my opinion, to provide a more elaborate explanation about the intended use of rex using a comment at the start of the ado file, rather than trying to incorporate this in the name.

Upvotes: 1

Related Questions