When to define a `module`

Santiago_Lopez
Fri, 04 Oct 2024 17:51:34 +0000
General Usage

When developing a package (call it MyPackage) one usually has a “main” function src/MyPackage.jl. This file is a module with a bunch of using, export and include statements.

The export statements establish the API of the package, i.e. the set of functions, variables, types, etc. which will be made available through the call using MyPackage. The include statements typically import .jl files which contain the actual code of our package.

For instance, if we are developing a package for time series analysis, the included files might be time_series.jl (defining a struct and basic functionalities), fourier.jl (containing FFT and power spectrum estimation functions), wavelets.jl implementing wavelet analysis, etc.

Programming principles encourage us to make the files of our package as independent as possible from one another. For instance, fourier.jl might operate entirely on native vectors, without needing to import anything from time_series.jl.

Should such independent portions of code (fourier.jl in our example) be modules as well? And what about less isolated, or less independent, files? In short, when should a set of functionalities be wrapped around a module, and when not?

23 posts - 9 participants