help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Best practices for loading packages


From: Daniel Kraft
Subject: Best practices for loading packages
Date: Thu, 27 Mar 2014 12:38:12 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0

Hi!

I've recently started to appreciate what packages can do to keep code
structured in Octave.  Now I'm wondering what the "best practices" for
loading packages are.

In particular, if I write a function "foo" that uses some package "bar",
and then use the function itself from my main program.  What I'm
currently doing in the main script is something like this:

  clear ("all");
  pkg load bar;
  ... other things ...
  foo ();

I. e., I load the package together with a lot of other "initialisation"
things right at the beginning of each main program that uses foo.
However, in some sense this seems not optimal -- a user of foo need not
necessarily know that foo uses package "bar", and also the main program
may not need or even want the package to be loaded (except indirectly
for using foo).

Hence, I can also load all necessary packages in foo directly.  This
ensures that users of the function need not worry about loading all
required packages (which should be "hidden implementation details" of
the function).  However, this still leaves the package loaded for the
main program -- and without even explicitly stating that the package is
loaded there, it may lead to even more confusion / problems if the
package clutters the namespace in an unexpected way.

Unloading "bar" when the function returns is also not possible, since it
is not guaranteed that the package is indeed not used anywhere else....

What is the recommended way of handling this situation?  Just load all
packages in the main program?  Or is there a way to "ensure" that a
package is loaded just for the scope of a function executing?  (Similar
to #include of headers in some .cpp file or import directives in certain
languages that only concern a "local" scope (one compilation unit).)

Thanks!  Yours,
Daniel

-- 
http://www.domob.eu/
OpenPGP: 901C 5216 0537 1D2A F071  5A0E 4D94 6EED 04F7 CF52
Namecoin: id/domob -> https://nameid.org/?name=domob
--
Done:  Arc-Bar-Cav-Hea-Kni-Ran-Rog-Sam-Tou-Val-Wiz
To go: Mon-Pri

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]