# Breaking notation convention - a potential anti-pattern?

**URL:** https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699
**Category:** General Usage
**Tags:** question, convention, notation
**Created:** [July 17, 2023, 5:34pm UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699 "2023-07-17T17:34:33Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 17, 2023, 5:34pm UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/1 "2023-07-17T17:34:33Z")

</div>

I have a scenario where I think it might be warranted to depart from the _end with an exclamation mark_ convention for function names that modify at least one of their arguments.

However, I wouldn’t want to get into an anti-pattern here - so I am really curious about your take on this.

I think the best approach is to show a MWE here:

```julia
struct MyStruct
    arr::Vector{Int} # updatable thing
    updatable::Bool
end

function myfun(x::MyStruct)
    # some sophisticated logic using x
    y = 1 # resulted from the logic
    success = true # can also be false - depending on the logic
    success && x.updatable && push!(x.arr, y)
    return x
end

x = MyStruct([1, 2, 3], true)
myfun(x)
@info x

```

In this scenario, `myfun` has the potential to modify its argument - and will always fail to modify it when the `updatable` is set to `false`. And will almost always modify it when the `updatable` is set to `true` (with a few exceptions - depending on the internal logic of the function).

I am aware that I could isolate my logic and have `myfun` and `myfun!` versions that can call `myfun_logic` that returns `(result=an Int, success=true/false)` and perform the update only inside `myfun!` - but in this case I could very well just drop the `updatable` field and decide which function to call each time. So let’s stick with the MWE - where the user does not need to select between the two functions at each call location.

So, should I switch to `myfun!` in this scenario? Or, is `myfun` a legitimate option?

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [July 17, 2023, 6:17pm UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/2 "2023-07-17T18:17:28Z")

</div>

> [@algunion](#):
>
> Or, is `myfun` a legitimate option?

IMO it isn’t. There are many functions with a bang that might fail to modify their argument for some reason. The bang is here to tell you that they might.

```julia
julia> push!(Int[], 0.5)
ERROR: InexactError: Int64(0.5)
Stacktrace:
 [1] Int64
   @ ./float.jl:900 [inlined]
 [2] convert
   @ ./number.jl:7 [inlined]
 [3] push!(a::Vector{Int64}, item::Float64)
   @ Base ./array.jl:1060
 [4] top-level scope
   @ REPL[1]:1

```

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 17, 2023, 6:22pm UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/3 "2023-07-17T18:22:18Z")

</div>

Just to be sure about your intended message: do you mean I should switch to `myfun!` and drop the `myfun` (given the function might modify the argument)?

I am writing this because you _quoted should I switch to `myfun!` in this scenario?_ but you replied _you shouldn’t_. However - the rest of your message seems to support the bang usage in the context.

---

<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 17, 2023, 6:38pm UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/4 "2023-07-17T18:38:07Z")

</div>

This is a mutating function, and should end with a `!`

There are some exceptions, like `print`, which don’t end with a `!`, but I don’t see that this is a relevant comparison. Keep the `!`

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [July 17, 2023, 6:40pm UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/5 "2023-07-17T18:40:01Z")

</div>

I was trying to confuse you. And me. Mission accomplished 🕶  
Also, just edited my message

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [July 17, 2023, 6:52pm UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/6 "2023-07-17T18:52:30Z")

</div>

Another convention that I think is nice is the `!!` convention from [BangBang.jl](https://github.com/JuliaFolds2/BangBang.jl), where

```julia
push!!(v, x)

```

is something like

```julia
if x fits_in v
   push!(v, x)
else
   vcat(v, [x])
end

```

i.e. the function is allowed to choose if it does mutation or an out-of-place operation. The mutation is basically an optimization.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [July 17, 2023, 6:53pm UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/7 "2023-07-17T18:53:10Z")

</div>

The `!` is just a convention, designed to be a helpful signal to end users about what the function may or may not do. IMO it all depends on the use-case and how you might imagine callers reasoning about the function call.

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 17, 2023, 7:30pm UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/8 "2023-07-17T19:30:19Z")

</div>

Thanks - after reading your messages, I will opt for bang usage in my scenario.

To not open a new topic - and somewhat derived from the present one - how about the callable objects?

Continuing the MWE from OP:

```julia
function (x::MyStruct)(someargs...)
    y = 1 # resulted from the logic
    success = true # can also be false - depending on the logic
    success && x.updatable && push!(x.arr, y)    
    return x
end

```

Calling the object might update the state of the object - I don’t see any convention opposing such behavior.

What are your thoughts about this?

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [July 17, 2023, 8:18pm UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/9 "2023-07-17T20:18:38Z")

</div>

A mutable callable object is kinda cursed 🤣 I guess you have no other choice than to document it thoroughly

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [July 17, 2023, 8:43pm UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/10 "2023-07-17T20:43:22Z")

</div>

I guess in this case one could just advise that people name the callable object with a `!`. i.e.

```julia
function (x!::MyStruct)(someargs...)
    y = 1 # resulted from the logic
    success = true # can also be false - depending on the logic
    success && x!.updatable && push!(x!.arr, y)    
    return x!
end

```

but I’m not sure that’s a great idea

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [July 17, 2023, 8:50pm UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/11 "2023-07-17T20:50:26Z")

</div>

What if your mutatable callable object computed factorials?

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [July 17, 2023, 9:05pm UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/12 "2023-07-17T21:05:04Z")

</div>

don’t

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 17, 2023, 9:54pm UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/13 "2023-07-17T21:54:31Z")

</div>

Thank you, everybody, for the input.

I guess that such behavior can be _permitted_ inside the internals of a package - primarily used in a limited scope.

```julia
function enclosing(someargs...)
    # define the updatable thing here
    x = MyStruct([1, 2, 3], true)
    # do crazy stuff in this scope
    x()()()()()()
    @info x
    # return and hide from API what you did
    return x.arr
end

```

I am not asking these things just for the sake of departing from the convention for no reason. I imagine scenarios where multiple lines of code can be saved and where the _weird_ thing is self-explanatory inside a limited scope. Obviously, if the weirdness spans across functions/modules/files, it becomes a clear no-go.

Another scenario I can think of is when you create a DSL that will end up with a specific set of documentation (overlapping or not with Julia’s) - and where Julia conventions are not expected (and provided there are good arguments for this approach in the context of the DSL design).

But I get that doing this stuff and exposing it via that public interface is like setting traps for the users of the package (or ending up with pretty complex documentation that might be hard to follow - especially if convention-breaking occurs).

Thank you all.

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 17, 2023, 10:17pm UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/14 "2023-07-17T22:17:04Z")

</div>

I appreciate all your inputs, and I don’t think there is _a solution_ - however, I find @gdalle’s _don’t_ a very good (and funny) thing to show together with the OP - that is, for potential new users who are reading the OP and don’t want to follow the thread.

At the same time, I acknowledge that things can be more nuanced (e.g., see @mbauman [contribution](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/7)).

I think is better to go with _don’t_ default - and once people are really comfortable and have good enough contextual arguments can opt for dropping the bang.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [July 18, 2023, 5:25am UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/15 "2023-07-18T05:25:11Z")

</div>

> [@frylock](#):
>
> What if your mutatable callable object computed factorials?

Actually, my “don’t” was a response to this, as in “don’t even go there it is beyond cursed”

> [@How about to simplify the factorial function name?](https://discourse.julialang.org/t/how-about-to-simplify-the-factorial-function-name/80715/6):
>
> And then again it is debatable whether !(5) is truly more readable than factorial(5), especially in a language where ! usually means boolean negation

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [July 18, 2023, 6:46am UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/16 "2023-07-18T06:46:44Z")

</div>

I have updated the accepted answer, as I think Matt’s reply is more helpful for users who might land on this page from Google

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 18, 2023, 9:21am UTC](https://discourse.julialang.org/t/breaking-notation-convention-a-potential-anti-pattern/101699/17 "2023-07-18T09:21:35Z")

</div>

You are right. Thank you.
