# How do you structure your code?

**URL:** https://discourse.julialang.org/t/how-do-you-structure-your-code/26566
**Category:** General Usage
**Created:** [July 19, 2019, 10:20pm UTC](https://discourse.julialang.org/t/how-do-you-structure-your-code/26566 "2019-07-19T22:20:52Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [July 19, 2019, 10:20pm UTC](https://discourse.julialang.org/t/how-do-you-structure-your-code/26566/1 "2019-07-19T22:20:52Z")

</div>

I’m defining N types and let’s say there are 5-15 functions for each types. How would you structure your code?

1. Each file contains a single type and related definitions
2. One file contains all types, and a separate file for defining functions for each type
3. Something else…?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [July 19, 2019, 11:39pm UTC](https://discourse.julialang.org/t/how-do-you-structure-your-code/26566/2 "2019-07-19T23:39:33Z")

</div>

One file.

---

<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 20, 2019, 5:22am UTC](https://discourse.julialang.org/t/how-do-you-structure-your-code/26566/3 "2019-07-20T05:22:51Z")

</div>

This really depends on how large N is, and how long/complex the functions are.

I usually try to keep things in one file until that gets too large, then split.

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [July 20, 2019, 9:07am UTC](https://discourse.julialang.org/t/how-do-you-structure-your-code/26566/4 "2019-07-20T09:07:33Z")

</div>

What I usually do is have a file where all related types are defined and then I have a separate file where I define the operator algebras on those types. Both of those files are included from the main module. This separates the type definitions and their algebra. Utility methods are usually included with the type definitions. The algebra code can grow quite large due to the combinatorial possibilities of 10-15 types, so it needs to be kept separately to grow.

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [July 20, 2019, 9:20am UTC](https://discourse.julialang.org/t/how-do-you-structure-your-code/26566/5 "2019-07-20T09:20:59Z")

</div>

I try to keep concepts in their own files, so one abstract type and all its concrete types and all their methods in one file. The only real reason I do this is because it’s easier for me to navigate from one concept to the other in Vim. I just move between (Vim) buffers to get to where I want. If everything was in the same file I’d need to search the file to get to where I want. The biggest disadvantage I guess is that when you open a `src` directory it might “look” like this project is overly complex, but some of my files contain just 5 lines…

---

<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 20, 2019, 12:30pm UTC](https://discourse.julialang.org/t/how-do-you-structure-your-code/26566/6 "2019-07-20T12:30:07Z")

</div>

> [@yakir12](#):
>
> some of my files contain just 5 lines…

I would say it is definitely oversplitting.

Also, I think it is better to adapt your editor to the way you prefer to write Julia code than the other way round.

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [July 20, 2019, 12:56pm UTC](https://discourse.julialang.org/t/how-do-you-structure-your-code/26566/7 "2019-07-20T12:56:29Z")

</div>

> [@Tamas\_Papp](#):
>
> I would say it is definitely oversplitting.

Meh… I bet you’re right, but this seems to work. I mean, is there any real dis/advantage to “over/under splitting”?

> [@Tamas\_Papp](#):
>
> Also, I think it is better to adapt your editor to the way you prefer to write Julia code than the other way round.

I totally agree. I’m getting a new computer soon and I’ll be ricing that to “completion”…

---

<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 20, 2019, 1:14pm UTC](https://discourse.julialang.org/t/how-do-you-structure-your-code/26566/8 "2019-07-20T13:14:55Z")

</div>

> [@yakir12](#):
>
> I mean, is there any real dis/advantage to “over/under splitting”?

The compiler does not care, of course, so if that’s what you mean by “real”, there is no downside — you can put every single function in its own file (there is actually a pretty widely used scientific programming language that does this 😛).

But humans may find it easier to look at meaningful units of code. Think about the readers of the code, and try to make it easy for them to follow what you did. It is difficult to be more concrete than this; the problem is not unlike organizing chapters in a book, or planning the layout of a garden, or designing a building.

---

<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 20, 2019, 1:21pm UTC](https://discourse.julialang.org/t/how-do-you-structure-your-code/26566/9 "2019-07-20T13:21:51Z")

</div>

I normally mimic `object oriented` design, and each file is a module with a type and all the methods applies to it. As a result, I can use `Module.method(type, parameters)`. I can easily list all the methods applying to that type in editor. The `methodswith` function is nice, but could not be used in editor to list all the available methods corresponding to that type.

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [July 20, 2019, 1:46pm UTC](https://discourse.julialang.org/t/how-do-you-structure-your-code/26566/10 "2019-07-20T13:46:57Z")

</div>

I really need to understand better how to use modules separately from packages…

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [July 20, 2019, 2:49pm UTC](https://discourse.julialang.org/t/how-do-you-structure-your-code/26566/11 "2019-07-20T14:49:34Z")

</div>

Depends, but usually (2). The various types in one file and the implementation of the API in another.

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [July 20, 2019, 5:18pm UTC](https://discourse.julialang.org/t/how-do-you-structure-your-code/26566/12 "2019-07-20T17:18:22Z")

</div>

That seems natural for some people. What do you do when you have functions that take multiple types then? Do you create a separate file for these “interaction functions”?

---

<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 25, 2019, 5:03pm UTC](https://discourse.julialang.org/t/how-do-you-structure-your-code/26566/13 "2019-07-25T17:03:42Z")

</div>

the types are ordered, the later type can use dispatch to interact with previous type. This is not a good design since the methods with kind of scatters to other type files, but these kinds of functions are only a few in my application.
