# Best way to group functions?

**URL:** <https://discourse.julialang.org/t/best-way-to-group-functions/67011>\
**Category:** New to Julia\
**Tags:** functions\
**Created:** [August 26, 2021, 12:54am UTC](https://discourse.julialang.org/t/best-way-to-group-functions/67011 "2021-08-26T00:54:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DiracFermion1](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@DiracFermion1](https://discourse.julialang.org/u/DiracFermion1)\
**Post date:** [August 26, 2021, 12:54am UTC](https://discourse.julialang.org/t/best-way-to-group-functions/67011/1 "2021-08-26T00:54:15Z")

</div>

Since there are no classes in Julia, I wonder what might be a good way to group functions that work for the same type of objects?

One way I can think of is to use `modules`, just want to hear if there are other better approaches?

Thanks!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 26, 2021, 1:00am UTC](https://discourse.julialang.org/t/best-way-to-group-functions/67011/2 "2021-08-26T01:00:02Z")

</div>

> [@DiracFermion1](#):
>
> group functions

what is the meaning of “group” here, list them side by side? to discover them?

---

<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:** [August 26, 2021, 1:06am UTC](https://discourse.julialang.org/t/best-way-to-group-functions/67011/3 "2021-08-26T01:06:06Z")

</div>

> [@DiracFermion1](#):
>
> that work for the same type of objects?

That is not a concept very natural in Julia. Functions are, whenever possible, written in a generic way, to be able to operate on types that any other user may define and where not even envisaged by the function original developer.

This talk gives an overview of the concept: [https://youtu.be/kc9HwsxE1OY](https://youtu.be/kc9HwsxE1OY)

---

<div class="post-metadata">

**Author:** ![DiracFermion1](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@DiracFermion1](https://discourse.julialang.org/u/DiracFermion1)\
**Post date:** [August 26, 2021, 2:09am UTC](https://discourse.julialang.org/t/best-way-to-group-functions/67011/4 "2021-08-26T02:09:36Z")

</div>

Basically it means organizing functions such that it is easy to maintain and debug. Also, we can avoid passing the same argument through multiple functions if it is possible to define some “shared variables” (similar to OOP private variables of an object), which could make refactoring much easier.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 26, 2021, 2:13am UTC](https://discourse.julialang.org/t/best-way-to-group-functions/67011/5 "2021-08-26T02:13:50Z")

</div>

> [@DiracFermion1](#):
>
> shared variables

structs already do that, the only difference is that in OOP:

```julia
OBJ.func(X)

```

in Julia:

```julia
func(OBJ, X)

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [August 26, 2021, 5:11am UTC](https://discourse.julialang.org/t/best-way-to-group-functions/67011/6 "2021-08-26T05:11:40Z")

</div>

You can put related functions close to each other in source. You don’t have to, of course. But you can.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [August 26, 2021, 7:50am UTC](https://discourse.julialang.org/t/best-way-to-group-functions/67011/7 "2021-08-26T07:50:19Z")

</div>

Yes, that’s a good advice. I think the only place where I have seen a list of functions which relate to a type are the docs (or by looking into the source code).  
So, if your package will have documentation, that might be the best place to communicate the groups of functions.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 26, 2021, 8:33am UTC](https://discourse.julialang.org/t/best-way-to-group-functions/67011/8 "2021-08-26T08:33:16Z")

</div>

Even though generic functions are preferred, there are always a number of methods that have to be defined specifically for any new type, like accessors, or implementations of an interface you want to support.

Those methods ‘belong’ with the type, and it is probably meaningful to put them next to your struct definition. But you only ‘group’ them in the sense of putting them near each other in the source.
