Good coding practices: How to cluster many variables in function argument

Another good option is Julia has NamedTuple packing/unpacking which is really good for quickly doing this:

function fun_clear(phys_params)
    (;mass, speed, height) = phys_params
    ...
end

phys_params = (;mass, speed, height) # if mass, speed, height are variables already defined somewhere
phys_params = (;mass=1, speed=2, height=3) # if you want to define them right here

fun_clear(phys_params)

The unpacking also works if phys_params is a custom struct like mentioned above.

7 Likes