# When to define a \`module\`

**URL:** https://discourse.julialang.org/t/when-to-define-a-module/120935
**Category:** General Usage
**Tags:** question, package, modules
**Created:** [October 4, 2024, 5:51pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935 "2024-10-04T17:51:34Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Santiago\_Lopez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/santiago_lopez/32/46298_2.png) [@Santiago\_Lopez](https://discourse.julialang.org/u/Santiago_Lopez)
#### Post date: [October 4, 2024, 5:51pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/1 "2024-10-04T17:51:35Z")

</div>

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 `include`d 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 `module`s 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?

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 4, 2024, 6:27pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/2 "2024-10-04T18:27:53Z")

</div>

I don´t think there are clear rules for that, and taste and good sense are important.

Nevertheless, recently a developer (not me) of ChunkSplitters.jl reorganized the code separating all the internals of the package into a module: [ChunkSplitters.jl/src at main · JuliaFolds2/ChunkSplitters.jl · GitHub](https://github.com/JuliaFolds2/ChunkSplitters.jl/tree/main/src)

That way, it is hard to access the internals inadvertently, and what is API and what’s not is very transparent. I found that organization neat. One can extend that idea in having the API defined at the top level module of the package, and split all the remaining code into submodules that import the API from the main module and then define the internals providing the functionalities.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 4, 2024, 6:32pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/3 "2024-10-04T18:32:08Z")

</div>

VSCode does not support module based development well. So I am hardly using modules any more, only packages that are split up with includes. Not perfect, but kind of OK.

---

<div class="post-metadata">

### Author: ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)
#### Post date: [October 5, 2024, 11:14am UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/4 "2024-10-05T11:14:49Z")

</div>

> [@ufechner7](#):
>
> VSCode does not support module based development well.

What do you mean specifically?

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 5, 2024, 11:57am UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/5 "2024-10-05T11:57:54Z")

</div>

This [bug](https://github.com/julia-vscode/julia-vscode/issues/307), which is open since 7 years and I am not very optimistic that it will ever get fixed unless their is funding to hire another developer for the julia-vscode plugin.

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [October 6, 2024, 8:32am UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/6 "2024-10-06T08:32:06Z")

</div>

Hi,

I am not sure if this is entirely what you mean, but I seem to have VS Code working for me, while developing with modules.

If you like at my source repo here:

> **[SPHExample/src at main · AhmedSalih3d/SPHExample](https://github.com/AhmedSalih3d/SPHExample/tree/main/src)**
>
> Simple SPH dam-break simulation in Julia. Contribute to AhmedSalih3d/SPHExample development by creating an account on GitHub.

I have a module in `src/SPHCellList.jl`:

```julia
module SPHCellList

export ConstructStencil, ExtractCells!, UpdateNeighbors!, NeighborLoop!, ComputeInteractions!, RunSimulation

using Parameters, FastPow, StaticArrays, Base.Threads, ChunkSplitters
import LinearAlgebra: dot

using ..SimulationEquations
using ..SimulationGeometry
using ..AuxillaryFunctions
using ..SimulationMetaDataConfiguration
using ..SimulationConstantsConfiguration
using ..SimulationLoggerConfiguration
using ..PreProcess
using ..ProduceHDFVTK
using ..TimeStepping
using ..OpenExternalPrograms

```

Which refers to other modules (and not their filepaths directly). If I hover about it in VS Code:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/d/ed036ff6b572e330395768b3b0adf9d90c913d1a.png)

And I can to jump to using `crtl+left click`.

Of course I have a `main` file named after the package, `SPHExample.jl` where I must include all files etc.

It took me quite some time to get this work flow up and running from Julia, but I like it that it is module based and I can quite easily import/export modules between different files, without ever referring to direct file path (only once in main)

Hope it helps, if I understood you correctly

Kind regards

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [October 6, 2024, 1:45pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/7 "2024-10-06T13:45:07Z")

</div>

> [@ufechner7](#):
>
> This [bug](https://github.com/julia-vscode/julia-vscode/issues/307), which is open since 7 years and I am not very optimistic that it will ever get fixed

This only affects you if you rely on `LOAD_PATH`, right? I’m not sure what reason there is to rely on a custom load path. Private packages, but no time for a local registry?

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 6, 2024, 1:54pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/8 "2024-10-06T13:54:22Z")

</div>

Well, I am used to write code without using packages. If code shall not be reused by other projects, then why write a package?

If you define a file that contains a module in the src folder the easiest way to make use of this module is to add the src folder to the LOAD\_PATH.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 6, 2024, 1:55pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/9 "2024-10-06T13:55:44Z")

</div>

I do not understand the `using ..ModulName` syntax. What does it do?

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [October 6, 2024, 3:30pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/10 "2024-10-06T15:30:56Z")

</div>

> [@ufechner7](#):
>
> If code shall not be reused by other projects, then why write a package?

- Dependency management
- Tooling support (this topic)

> [@ufechner7](#):
>
> If you define a file that contains a module in the src folder the easiest way to make use of this module is to add the src folder to the LOAD\_PATH.

I don’t think that’s any easier than just `dev`-ing a package. It’s easy to create a package (interactively!) by:

```julia
using PkgTemplates: generate
generate()

```

> [@ufechner7](#):
>
> I do not understand the `using ..ModulName` syntax. What does it do?

- `using .N` makes the names of the module `N` from this namespace available in this namespace
- `using ..N` makes the names of the module `N` from the parent namespace available in this namespace
- and so on

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 6, 2024, 4:08pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/11 "2024-10-06T16:08:53Z")

</div>

> [@nsajko](#):
>
> I don’t think that’s any easier than just `dev`-ing a package.

I never dev a package. If I do that the source code ends up at a location which is below the `.julia` folder, and I delete that folder now and then.

---

<div class="post-metadata">

### Author: ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)
#### Post date: [October 6, 2024, 4:10pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/12 "2024-10-06T16:10:12Z")

</div>

You can change that by modifying `ENV["JULIA_PKG_DEVDIR"]` to a location which you prefer IIRC

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [October 6, 2024, 4:13pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/13 "2024-10-06T16:13:08Z")

</div>

> [@ufechner7](#):
>
> If I do that the source code ends up at a location which is below the `.julia` folder, and I delete that folder now and then.

You’re thinking about `dev RegisteredPackage`, I think, but we’re not talking about registered packages anyway. When I said `dev` I meant `dev /path/to/your/source`.

FTR I, too, don’t keep the development sources of any packages under `.julia`, but I `dev` many packages.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 6, 2024, 4:16pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/14 "2024-10-06T16:16:12Z")

</div>

> [@nsajko](#):
>
> When I said `dev` I meant `dev /path/to/your/source`.

So what is that good for?

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [October 6, 2024, 4:17pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/15 "2024-10-06T16:17:56Z")

</div>

It makes the package available to Julia and tooling, while tracking all changes to the source files. So you wouldn’t have to modify the load path.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 6, 2024, 4:24pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/16 "2024-10-06T16:24:32Z")

</div>

Nice. But this does not work for modules, only for packages. And modules are the main topic of this thread.

So I repeat my statement that modules are not well supported by the Julia tooling. Which means I cannot do modular programming in the way I would like to do it, to create code units that are equal to a file and understandable by reading just that one file.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [October 6, 2024, 4:30pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/17 "2024-10-06T16:30:12Z")

</div>

A package _is_ a module. Just a module with a `Project.toml`. And that, adding a `Project.toml`/making a package, is how you make modules “well supported”. I’m literally giving you the solution to your problems, while your dismissal seems quite arbitrary.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 6, 2024, 4:43pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/18 "2024-10-06T16:43:43Z")

</div>

> [@nsajko](#):
>
> I’m literally giving you the solution to your problems, while your dismissal seems quite arbitrary.

It is NOT a solution of my problem. A package can and should consist of multiple modules. It is a different level of granularity. And to say, because packages are well supported (well, even that is debatable, goto definition often does not work in examples or tests), to say that because of the tooling support for packages modules are well supported is just plain wrong.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [October 6, 2024, 4:51pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/19 "2024-10-06T16:51:13Z")

</div>

> [@ufechner7](#):
>
> A package can and should consist of multiple modules.

Yes, and this is well supported. As already noted above by @Ahmed_Salih.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 6, 2024, 4:52pm UTC](https://discourse.julialang.org/t/when-to-define-a-module/120935/20 "2024-10-06T16:52:30Z")

</div>

This contradicts my experience.

[Next page](https://discourse.julialang.org/t/when-to-define-a-module/120935.md?page=2)
