Best way to structure Julia code

The structure that I use (and I think hasn’t yet been mentioned here) is having each module be a submodule of the global module associated to your project/package.

Submodules can use or import one another, provided that they provide either the correct relative path (with the right number of leading dots, which I find cumbersome) or an absolute path starting with the global module.

So something like this works (assuming you are in a project named “Top”):

module Top

# Potentially in a "A.jl" file
module A
export a
a() = "OK"
end


# Potentially in a "B.jl" file
module B
using Top.A
export b
b() = a()
end



using .B
export check
check() = b()


end # module

As already mentioned, this organization in top module + submodules is orthogonal to the organization of source files: using adequately placed includes, each submodule could be in its own file, or split in separate files, or all submodules could be grouped in one file.

As far as I know, everything works well with such a setup, including Revise, precompilation and so on.

7 Likes