# How to organise files and modules?

**URL:** <https://discourse.julialang.org/t/how-to-organise-files-and-modules/4842>\
**Category:** New to Julia\
**Tags:** code-organization\
**Created:** [July 13, 2017, 7:36pm UTC](https://discourse.julialang.org/t/how-to-organise-files-and-modules/4842 "2017-07-13T19:36:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)\
**Post date:** [July 13, 2017, 7:36pm UTC](https://discourse.julialang.org/t/how-to-organise-files-and-modules/4842/1 "2017-07-13T19:36:32Z")

</div>

Hello.

I’m currently using Julia for the first time in a project of more than one file and I’m struggling with `module`, `using` and `include`.

For this example, let say that I have a few files :

`big.jl`  
`small1.jl`  
`small2.jl`  
`small3.jl`

In `small3.jl` I have some functions that I would like to use in the 3 others files. In `big.jl` I would like use some functions from the 3 small files. Also, in `big.jl` I created a module :

```julia
module Big
include("small1.jl")
include("small2.jl")
include("small3.jl")
... other stuff
end

```

Since we are collaborating on that project, we found it important to be able to read and understand easily the codes. So, we would like to be able to know where each functions are defined.

For example, in `small1.jl` and `small2.jl` I would like to be able to call functions from `small3.jl` as `small3.foo(...)` or something similar.

We can do that by putting everything in `small3.jl` into a module. But, then if I included `small3.jl` in `small1.jl`, `small2.jl` and `big.jl` I got some warning telling me that the module in `small3.jl` have been included more than once when I included `big.jl`.

The other way that we found is to only include `small3.jl` in `big.jl` and not in the two others files. Since both `small1.jl` and `small2.jl` are included in `big.jl` we can call `big.small3.foo(...)` in those file. But, it’s not exactly what I wanted first. The other problem is that if I create a new module that used `big` than it looks like I need to call the function like this `other.big.small3.foo(...)` in `small1.jl` and `small2.jl`.

Finally, my question is : What is the Julia way to do that kind of thing?

Thanks!

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [July 13, 2017, 7:54pm UTC](https://discourse.julialang.org/t/how-to-organise-files-and-modules/4842/2 "2017-07-13T19:54:40Z")

</div>

It’s best to think of `include` as copy-pasting source code into the location from which `include` is called. Therefore, if `small2.jl` calls functions from `small1.jl` you can simply put `include` statements in `Big` like you have above, without putting `include` statements in any of the `small` files. I _think_ that should answer your question, but perhaps I misunderstood it.

---

<div class="post-metadata">

**Author:** ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)\
**Post date:** [July 13, 2017, 8:11pm UTC](https://discourse.julialang.org/t/how-to-organise-files-and-modules/4842/3 "2017-07-13T20:11:28Z")

</div>

It is a good solution except that I cannot call functions as `small3.foo(...)`in `small1.jl` which I want to be able to do. I find the code easier to read when I can easily track where functions have been defined.

Well, I think I want to be able to do that … unless, maybe, that’s not the Julia way to work.

---

<div class="post-metadata">

**Author:** ![jwu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jwu/32/3775_2.png) [@jwu](https://discourse.julialang.org/u/jwu)\
**Post date:** [July 13, 2017, 8:36pm UTC](https://discourse.julialang.org/t/how-to-organise-files-and-modules/4842/4 "2017-07-13T20:36:10Z")

</div>

I think ppl normally have a base file and put the include(“base.jl”) first.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [July 13, 2017, 8:47pm UTC](https://discourse.julialang.org/t/how-to-organise-files-and-modules/4842/5 "2017-07-13T20:47:03Z")

</div>

I would say that it is somehow “un-Julian” to excessively segregate functions into lots of different modules. From your desire to do `small3.foo(...)` I’m guessing that you are coming from Python which segregates functions in a rather extreme way. I suggest that in the long run you will be much happier if you give up on this notion. One of the big revelations I had when I started using Julia was how much the (arbitrary) association of functions with classes was damaging my ability to reason about code.

However, if you don’t want to do this, you are indeed free to define arbitrarily many nested or parallel modules.

---

<div class="post-metadata">

**Author:** ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)\
**Post date:** [July 13, 2017, 8:53pm UTC](https://discourse.julialang.org/t/how-to-organise-files-and-modules/4842/6 "2017-07-13T20:53:40Z")

</div>

So I guess it’s time to throw those Python habits of mine …

Thank for the answers.

---

<div class="post-metadata">

**Author:** ![fjarri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fjarri/32/630_2.png) [@fjarri](https://discourse.julialang.org/u/fjarri)\
**Post date:** [July 14, 2017, 1:53am UTC](https://discourse.julialang.org/t/how-to-organise-files-and-modules/4842/7 "2017-07-14T01:53:24Z")

</div>

> [@ExpandingMan](#):
>
> One of the big revelations I had when I started using Julia was how much the (arbitrary) association of functions with classes was damaging my ability to reason about code.

To be fair, I think @maxtremblay35’s example rather shows an association of functions with _namespaces_, which is (generally) not arbitrary and quite useful in large codebases.

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [July 14, 2017, 5:31am UTC](https://discourse.julialang.org/t/how-to-organise-files-and-modules/4842/8 "2017-07-14T05:31:33Z")

</div>

I’ve been monkeying around with a package that tries to standardize file/module setup.

Maybe you want to check it out?

[https://github.com/djsegal/Julz.jl](https://github.com/djsegal/Julz.jl)

// an example project buit with it can be found at: [git.io/tokamak](https://git.io/tokamak)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 14, 2017, 5:56am UTC](https://discourse.julialang.org/t/how-to-organise-files-and-modules/4842/9 "2017-07-14T05:56:38Z")

</div>

> [@maxtremblay35](#):
>
> we would like to be able to know where each functions are defined.

Julia has support for that, see `@edit`, and also the frame number + `Ctrl-Q` functionality after a stacktrace in the REPL. Other than that, your editor should be able to help you find things quickly (eg `helm` in Emacs).

Given a decent editor which you know how to use, I would put modules with \<300 LOC in a single file, and break up larger ones into smaller files when meaningful. However, establishing a 1:1 mapping between _small_ files and modules is usually not productive.
