Where to put export statements (Sub-Module Pattern) in distributed code?

I am working through Tom Kwong’s book "Hands-On Design Patterns and Best Practices with Julia." In Chapter 7 on Maintainability Patterns, Tom talks about organizing code (sub-module pattern) and gives an example where a module simply includes functions from a file, in which the code is not organized within a sub-module. E.g.,

# funcs.jl

export func1, func2

function func1()
   # body of func1
end

function func2()
    # body of func2
end

That code is then included in the main package, like the following minimal example.

# MyPackage.jl
module MyPackage
    include("funcs.jl") # code for func1 and func2
end # module

It may not be a big issue, but I would not have “hidden” export statements within some file. Instead I would have chosen to make the exports remain explicit within the module that exports them? E.g.,

# MyPackage.jl
module MyPackage
    export func1, func2 # contained in funcs.jl file
    include("funcs.jl")
end # module

What is best practice in this regard?

I don’t know what the best practice is, but I prefer to adopt the second approach. I found it clearer.

2 Likes

Second approach seems typical because exports are fundamentally a characteristic of the module, not the objects assigned to the exported variables. That said, there could be justifications for doing it the first way, and as long as all the right lines get evaluated into the module’s scope, it’ll work the same.

1 Like