# Naming positional arguments at call site

**URL:** <https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526>\
**Category:** Internals & Design\
**Created:** [September 4, 2023, 7:07pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526 "2023-09-04T19:07:20Z")\
**Posts on this page:** 20\
**Page:** 4

<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:** [September 5, 2023, 4:26pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/62 "2023-09-05T16:26:42Z")

</div>

Isn’t `mod` like the exact case of a function where this is just unhelpful line noise? And that’s at the crux here: the names that package/language developers use just aren’t _necessarily_ going to be informative to the callers. Just use `units_sold`! That’s what you care about.

Yes, _sometimes_ the callers and the devs use the same language, and it’s hugely beneficial! But not always. And that separation of concerns is sensible, in my view.

---

<div class="post-metadata">

**Author:** ![GregVernon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregvernon/32/5611_2.png) [@GregVernon](https://discourse.julialang.org/u/GregVernon)\
**Post date:** [September 5, 2023, 4:37pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/63 "2023-09-05T16:37:20Z")

</div>

At least for me it’s helpful, as I can’t ever remember what the order of operations are in `mod` functions:

> `mod( arg1, arg2 )` … is that dividing `arg1` into `arg2` or `arg2` into `arg1`?

But when I see `mod( x, y )` it triggers my memory of `x divided by y`. And so in every language, I always put comments around `mod` or `rem` functions to remind myself.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [September 5, 2023, 4:37pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/64 "2023-09-05T16:37:52Z")

</div>

nothing stops the user from naming their own positional arguments with… variable names

is

```julia
sample(rng=Xoshiro(0), sampler=NUTS(), model=my_loglikelihood)

```

really so much better than

```julia
rng, sampler, model = Xoshiro(0), NUTS(), my_loglikelihood
sampler(rng, sampler, model)

```

especially given that very often one wants to reuse arguments (and something something “no magic constants?”)

---

<div class="post-metadata">

**Author:** ![rdavis120](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@rdavis120](https://discourse.julialang.org/u/rdavis120)\
**Post date:** [September 5, 2023, 4:49pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/65 "2023-09-05T16:49:30Z")

</div>

I think there are people that would really like to know if the function `mod(x, y)` was changed to `mod(x, z)`. So ideally a prompt in the ide, or package diagnostic, or (less desirable) a runtime error when running the unit test suite.

Writing `leftover_units = mod( x = units_sold, y = units_per_package )`

But having the compiler use `leftover_units = mod( units_sold, units_per_package )` **if and only if** the names and positions of x and y are still consistent with when I wrote the code, could be helpful.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [September 5, 2023, 4:52pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/66 "2023-09-05T16:52:04Z")

</div>

> [@rdavis120](#):
>
> I think there are people that would really like to know if the function `mod(x, y)` was changed to `mod(x, z)`

why? I mean, I know never say never, and Hyrum’s law and all, but why would this possibly matter to the user?

---

<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:** [September 5, 2023, 5:00pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/67 "2023-09-05T17:00:28Z")

</div>

I suppose there are three things going on here:

1. Some folks here want to be able to **write** functions (that is, literally write — as they develop the code) without worrying about the exact order of the positional args. Just the names are enough, in any order. You’d still need to look up/know the names, but you wouldn’t need to know the order.

2. Some folks here want to be able to **read** function calls as being self-documented, even if the args already match the defined positional order. Comments, variable names, etc, can all do the trick, but, yeah, none are _enforced_ to match the defined argument names.

3. But either way, I fear this is a [PSA: Julia is not at that stage of development anymore](https://discourse.julialang.org/t/psa-julia-is-not-at-that-stage-of-development-anymore/44872) thing. Making a change like this would drastically expand the surface area of every language and package API that’s ever existed in ways that weren’t necessarily intended.

---

<div class="post-metadata">

**Author:** ![xiaoxi](https://avatars.discourse-cdn.com/v4/letter/x/a9adbd/32.png) [@xiaoxi](https://discourse.julialang.org/u/xiaoxi)\
**Post date:** [September 5, 2023, 5:24pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/68 "2023-09-05T17:24:28Z")

</div>

Advanced LSP servers solve these problems. They can show the definition of functions/methods when you type them and show the name of parameters with inline hints.

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [September 5, 2023, 6:03pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/70 "2023-09-05T18:03:05Z")

</div>

> [@jar1](#):
>
> It seems obvious what’s going on in this case. Is there another example of where named arguments would help?

It’s also obvious if you already know what `NUTS` and `Xoshiro` are, but for a new user who’s used to (let’s say) BUGS, it’s probably very confusing.

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [September 5, 2023, 6:14pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/71 "2023-09-05T18:14:37Z")

</div>

> [@Benny](#):
>
> If a language that doesn’t need to deal with the extra factor of multimethods still moved toward position-only arguments, maybe we shouldn’t interfere with our own.

I think it’s fine to have position-only arguments as an option. Sometimes, position-or-keyword arguments don’t make sense, if the keyword argument is poorly-named. However, the default should be position-or-keyword (like Python, Swift, R, and most other languages have adopted). The problem is that right now, people just default to positional arguments (because keywords are slower).

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [September 5, 2023, 6:38pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/72 "2023-09-05T18:38:42Z")

</div>

> [@ParadaCarleton](#):
>
> (like Python, Swift, R, and most other languages have adopted)

“most” is doing a whole lot of heavy lifting there. there’s definitely a decent chunk that have made that choice (e.g. the ones you listed + Kotlin, Fortran, Scala, F#, probably a few more), but I wouldn’t be so bold as to present this as a universal/“obvious” design choice

In any case, the omission of this type of parameter certainly doesn’t make Julia an _outlier_

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [September 5, 2023, 6:59pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/73 "2023-09-05T18:59:03Z")

</div>

> [@goerz](#):
>
> but it can also come at a performance cost

> [@ParadaCarleton](#):
>
> people just default to positional arguments (because keywords are slower).

Is this true? Certainly Julia 0.6 or something had slow keywords, but I think that (in almost all cases?) this was fixed long ago.

All the examples [here](https://discourse.julialang.org/t/performance-of-typed-keyword-arguments/11579/) show no cost for me. As do simple tests… including ones which rely on constant propagation:

```julia
julia> fun(; x, y) = fun(x, y); fun(x, y) = /(x, y); # very simple fast function

julia> @btime fun(x=$1, y=$2); @btime fun($1, $2);
  min 2.500 ns, mean 2.610 ns (0 allocations)
  min 2.458 ns, mean 2.583 ns (0 allocations)

julia> tup(n) = sum(ntuple(i -> i^2, n)); tup(; n) = tup(n);

julia> @btime tup(5); @btime tup($5); @btime tup(n = 5); @btime tup(n = $5);
  min 0.875 ns, mean 0.957 ns (0 allocations)
  min 15.071 ns, mean 15.269 ns (0 allocations)
  min 0.834 ns, mean 0.957 ns (0 allocations)
  min 15.071 ns, mean 15.306 ns (0 allocations)

julia> @code_warntype tup(5) # Tuple{Vararg{Int64}}, without constant propagation!

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 5, 2023, 7:01pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/74 "2023-09-05T19:01:53Z")

</div>

> [@ParadaCarleton](#):
>
> However, the default should be position-or-keyword (like Python, Swift, R, and most other languages have adopted). The problem is that right now, people just default to positional arguments (because keywords are slower).

No, it’s definitely the multimethod aspect that makes position-only arguments much more useful, as I explained earlier about KeywordCalls.jl.

> [@Benny](#):
>
> using keywords to specify positional arguments out of order really relies on a function having 1 method for each unique set of names.

> [@adienes](#):
>
> e.g. the ones you listed + Kotlin, Fortran, Scala, F#, probably a few more

I’m not sure if function overloading counts as multimethods but it’s similar to compile-time method dispatch so I checked if any language had both function overloading and position-or-keyword arguments. I ran into Kotlin, and when I adapted the ambiguous method dispatch example I wrote earlier, it just caused a `Overload resolution ambiguity`. The positional arguments calls worked just fine.

---

<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:** [September 5, 2023, 7:02pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/75 "2023-09-05T19:02:28Z")

</div>

> [@GregVernon](#):
>
> But when I see `mod( x, y )` it triggers my memory of `x divided by y`.

I cannot imagine why “x divided by y” would be more natural than “y divided by x”.

And what if the developer decided to go with opposite choice of your preferred mnemonic?

And what if different people wrote different methods of the same function, sometimes going with x/y and sometimes y/x and sometimes alpha/beta?

---

<div class="post-metadata">

**Author:** ![GregVernon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregvernon/32/5611_2.png) [@GregVernon](https://discourse.julialang.org/u/GregVernon)\
**Post date:** [September 5, 2023, 7:46pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/76 "2023-09-05T19:46:50Z")

</div>

It’s perhaps not more natural, the pedantic approach would have been, perhaps, to name the arguments to `mod` to be `dividend` and `divisor`, but the mental picture I’m reminded of when I see `mod( x, y )` are the various lists of elementary operators:

Julia

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/4/f4e01d7517bbb8f0675ea2b1dfe2aab79eccce6b.png)

Python

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/8/981ac8ca9171a0e2530ae0b4ea4958fd9edc45ec.png)

Irrational, I know, but it’s an example of why _ **I** _ like to write my code in such a manner. I find that “future-Greg” curses “past-Greg” a lot less when I do.

---

<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:** [September 5, 2023, 8:31pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/77 "2023-09-05T20:31:02Z")

</div>

The choices `x` and `y` are prototypical examples of names devoid of any intrinsic meaning.

That list is a perfect example of why this whole idea is misguided, not just in practice, but in principle.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [September 5, 2023, 9:19pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/78 "2023-09-05T21:19:02Z")

</div>

> [@ParadaCarleton](#):
>
> The problem is that right now, people just default to positional arguments

The rule-of-thumb for API functions is this:

Public methods should have no more than three positional arguments, with three discouraged unless it really makes sense, e.g. `mapreduce(f, op, x)` or `fit(model, X, y)`.

…Granted, not all package developers have heard of this rule-of-thumb. I don’t have a reference, but I have seen something like it mentioned on Discourse.

And, of course, it is just a rule-of-thumb. It is subjective, and there are exceptions.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [September 5, 2023, 10:17pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/79 "2023-09-05T22:17:42Z")

</div>

A similar rule should apply for keyword arguments. Imho, Python/R tend to overuse keyword arguments tending towards one-stop kitchen-sink functions with many, many keyword arguments due to the perceived readability of them. Yet, eventually the number of concepts you have to hold in your head trumps the perceived simplicity of surface syntax.  
Especially with keyword arguments acting like configuration switches this leads to excessive branching inside a single function. Further, different parts of keyword options are often meaningful together, but independent of other groups of arguments without any visual cues about that. Personally, I would prefer smaller functions with clear separation of concerns, i.e.,

- put options that belong together into dedicated structs and pass these – naturally giving you named arguments for free
- separate different aspects of the functionality into designated functions, e.g., instead of an `iterations` argument return an iterator and let the caller choose the desired number of iterations or pass in a function/functor for handling termination conditions etc.
- use a fluent interface building up the final configuration in small logical steps, e.g., compare reading CSV in Spark and Pandas.
- …

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [September 6, 2023, 7:00pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/80 "2023-09-06T19:00:33Z")

</div>

> [@adienes](#):
>
> why? I mean, I know never say never, and Hyrum’s law and all, but why would this possibly matter to the user?

I don’t really care about variables named `x` and `z`. On the other hand, I’d certainly prefer it if these names were exposed, so that developers could give them better names than `x` and `y`, like `mod(dividend, modulus)`.

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [September 6, 2023, 7:24pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/81 "2023-09-06T19:24:30Z")

</div>

> [@bertschi](#):
>
> Yet, eventually the number of concepts you have to hold in your head trumps the perceived simplicity of surface syntax.

The main point is that with keywords you don’t have to hold those concepts in your head, unlike with positional arguments (where you have to remember the concept associated with each argument).

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 6, 2023, 11:43pm UTC](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526/83 "2023-09-06T23:43:52Z")

</div>

> [@rdavis120](#):
>
> I don’t see how using named arguments `venus(x = 1.100)` , when used in the same order/type as the definition breaks anything, other than my own code when the package developer changes the name.

Even assuming the developer makes the name public API and won’t change it, that syntax right now specifies `x` as a keyword argument (keyword-only, there’s no such thing as positional-or-keyword).

```julia
julia> function venus(;x::Real) # continuing from your example
           println("Key x")
       end
venus (generic function with 3 methods)

julia> venus(x = 1.100)
Key x

```

So there’s no way we can make that syntax use positional arguments without breaking Julia v1. A macro can transform that syntax to something that does, or transform the function like KeywordCalls.jl does.

[Previous page](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526.md?page=3)

[Next page](https://discourse.julialang.org/t/naming-positional-arguments-at-call-site/103526.md?page=5)
