# Design Q: Order of arguments: input data and algorithm type

**URL:** <https://discourse.julialang.org/t/design-q-order-of-arguments-input-data-and-algorithm-type/91768>\
**Category:** General Usage\
**Tags:** question, design\
**Created:** [December 17, 2022, 10:00am UTC](https://discourse.julialang.org/t/design-q-order-of-arguments-input-data-and-algorithm-type/91768 "2022-12-17T10:00:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [December 17, 2022, 10:00am UTC](https://discourse.julialang.org/t/design-q-order-of-arguments-input-data-and-algorithm-type/91768/1 "2022-12-17T10:00:30Z")

</div>

Hey all,

(wasn’t sure in which category to post it)

We are doing a lot of refactoring across many JuliaDynamics packages at the moment. A question that always comes up in the refactoring concerns the order of arguments of typically 2-argument functions. They way we’ve set up most of our systems is to have a collection of types that decide what algorithm will be used to compute the “quantity” we care about. And we have the following set up:

```julia
abstract type Supertype end

struct Type1 <: Supertype end
struct Type2 <: Supertype end

quantity(x, t::Type1) = ...
quantity(x, t::Type2) = ...

```

Notice that the return type of `quantity` is concrete irrespecitively of `Type`. E.g., it will always be a `Vector`. The types decide what algorithm will be used to fill the values of the vector. A concrete example would be our [`probabilities(x, est::ProbabilitiesEstimator)`](https://juliadynamics.github.io/Entropies.jl/dev/probabilities/#Entropies.probabilities) which extracts probabilities from data using one of 15 different approaches.

My big design question is about the argument order of `quantity`: **Which should be first? The input data `x` or the type deciding the algorithm `t`**? The [Julia Style Guide]( [Style Guide · The Julia Language](https://docs.julialang.org/en/v1/manual/style-guide/#Write-functions-with-argument-ordering-similar-to-Julia-Base)) doesn’t resolve this because `t` doesn’t decide the output type. What is the majority of the community doing? We in JuliaDynamics use the `quantity(x, t)` signature, but we recently realized it doesn’t lent itself to generalizations of multiple inputs, such as `quantity(x, y, t)`. (I.e., if multi-input is possible, `quantity(t, args...)` makes for an easier to learn interface).

---

<div class="post-metadata">

**Author:** ![brenhinkeller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brenhinkeller/32/36648_2.png) [@brenhinkeller](https://discourse.julialang.org/u/brenhinkeller)\
**Post date:** [December 17, 2022, 12:48pm UTC](https://discourse.julialang.org/t/design-q-order-of-arguments-input-data-and-algorithm-type/91768/2 "2022-12-17T12:48:11Z")

</div>

I would tend to put the type first, for the multi-args reason among others. This also seems to be what Base does in methods such as `convert(T, x)` and `reinterpret(type, A)`

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [December 17, 2022, 1:39pm UTC](https://discourse.julialang.org/t/design-q-order-of-arguments-input-data-and-algorithm-type/91768/3 "2022-12-17T13:39:14Z")

</div>

The first argument you provide is valid, but the second calling the the design in Base doesnt’ fit here, because this targets the case where the type decides the return type, as listed in the Julia style guide.

But actually Base follows the design of type first in the implementation of `rand` functions, where the RNG type is used first.

---

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [December 17, 2022, 2:00pm UTC](https://discourse.julialang.org/t/design-q-order-of-arguments-input-data-and-algorithm-type/91768/4 "2022-12-17T14:00:45Z")

</div>

Irrespective of what order you come down on: I would probably have `quantity(args…; estimator)` as the only _public_ method. That is, with a keyword argument, and leave the positional version as an internal implementation detail to exploit multiple dispatch. That’s what I do e.g. in [QuantumPropagators.jl/propagate.jl at 84ab059936bec838e75c1857cbb146c30e22f78f · JuliaQuantumControl/QuantumPropagators.jl · GitHub](https://github.com/JuliaQuantumControl/QuantumPropagators.jl/blob/84ab059936bec838e75c1857cbb146c30e22f78f/src/propagate.jl#L146)

---

<div class="post-metadata">

**Author:** ![brenhinkeller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brenhinkeller/32/36648_2.png) [@brenhinkeller](https://discourse.julialang.org/u/brenhinkeller)\
**Post date:** [December 17, 2022, 4:17pm UTC](https://discourse.julialang.org/t/design-q-order-of-arguments-input-data-and-algorithm-type/91768/5 "2022-12-17T16:17:34Z")

</div>

> [@Datseris](#):
>
> But actually Base follows the design of type first in the implementation of `rand` functions, where the RNG type is used first.

That’s arguably only because ` rand(rng, ...)` mutates `rng`, and should possibly be called `rand!(rng, ...)`
