# Should a function be named with \`!\` if it only modifies a container type's inner values?

**URL:** <https://discourse.julialang.org/t/should-a-function-be-named-with-if-it-only-modifies-a-container-types-inner-values/101504>\
**Category:** General Usage\
**Tags:** function\
**Created:** [July 12, 2023, 2:09am UTC](https://discourse.julialang.org/t/should-a-function-be-named-with-if-it-only-modifies-a-container-types-inner-values/101504 "2023-07-12T02:09:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [July 12, 2023, 2:09am UTC](https://discourse.julialang.org/t/should-a-function-be-named-with-if-it-only-modifies-a-container-types-inner-values/101504/1 "2023-07-12T02:09:39Z")

</div>

If `A` is a container type like:

```julia
struct A
    values::Vector
end
# Or
struct A
    values::Tuple
end

```

where `A` should hold a number of custom types `B`:

```julia
mutable struct B
    x
end

```

If a function called `modify!` can change `B.x` to a different value, and I want to apply `modify!` to `A{B}`. Should it be called `modify` or `modify!`?

```julia
modify!(a::A) = map(modify!, a.values)

```

In Julia, `!` should be added to [names of functions that modify their arguments](https://docs.julialang.org/en/v1/manual/style-guide/#bang-convention).

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [July 12, 2023, 2:39am UTC](https://discourse.julialang.org/t/should-a-function-be-named-with-if-it-only-modifies-a-container-types-inner-values/101504/2 "2023-07-12T02:39:21Z")

</div>

I’d say anything that does an in-place mutation should have an `!` appended, no matter where that mutation is happening. To me, if I see a function without an `!`, I assume that the author of the function is promising me that it will only create new values.

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [July 12, 2023, 2:48am UTC](https://discourse.julialang.org/t/should-a-function-be-named-with-if-it-only-modifies-a-container-types-inner-values/101504/3 "2023-07-12T02:48:58Z")

</div>

What about functions may or may not change the values (depends on what the values are), should they be appended a bang `!`?

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [July 12, 2023, 2:50am UTC](https://discourse.julialang.org/t/should-a-function-be-named-with-if-it-only-modifies-a-container-types-inner-values/101504/4 "2023-07-12T02:50:12Z")

</div>

Could you give a more concrete example? If there are two different behaviors, it feels like it should probably be two different functions, but I might be missing something!

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [July 12, 2023, 2:55am UTC](https://discourse.julialang.org/t/should-a-function-be-named-with-if-it-only-modifies-a-container-types-inner-values/101504/5 "2023-07-12T02:55:22Z")

</div>

It’s like this in my [code](https://github.com/MineralsCloud/EasyJobsBase.jl/blob/18321e6f7d81c3a4079f3843228ad9f41a83e036/src/run.jl#L72-L86). A simpler version is this:

```julia
function execute!(job::AbstractJob)
    if issucceeded(job)
        return job # Just return the job if it has succeeded
    else
        return dispatch!(job)
    end
end

```

For `AbstractJob`s that are already succeeded, I just return it back (it is a cached value) since I do not want to run it again, but if it is not run, I will use a function that will modify its values.

I suppose if there’s a chance where the `job` could be modified, then I should append a `!`.

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [July 12, 2023, 3:20am UTC](https://discourse.julialang.org/t/should-a-function-be-named-with-if-it-only-modifies-a-container-types-inner-values/101504/6 "2023-07-12T03:20:39Z")

</div>

Ah, I see what you’re saying! Manager methods that can kick off a bunch of different actions depending on state. Some mutating, some not.

I’m inclined to say yes to `!`, but this is also the point where clear docs are way more important than a naming convention. As long as you’re clear that mutation is a possibility, then it doesn’t really matter how you get it across to a user. Or at least, that is my two cents.

---

<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:** [July 12, 2023, 7:13am UTC](https://discourse.julialang.org/t/should-a-function-be-named-with-if-it-only-modifies-a-container-types-inner-values/101504/7 "2023-07-12T07:13:38Z")

</div>

> [@singularitti](#):
>
> I suppose if there’s a chance where the `job` could be modified, then I should append a `!`.

I would say absolutely yes to using `!`. Having a trailing `!` is no guarantee that the input will change, only a warning that it might. For example, `empty!` applied to an already empty collection will not change it. And any updating function that overwrites a value with the same value, is still mutating.

Yes, imo, without a doubt, use `!`
