Ask user to provide points coordinates as two vectors or as a single vector of tuples?

When asking the user to provide coordinates of plan points for my package RingStarProblems.jl I initially thought the following was best:

xycoordinates = tuple.(1:10, rand(1:10, 10))
somefunction(xycoordinates)

However, looking at Concorde.jl, they do the following instead:

x = 1:10
y = rand(1:10, 10)
somefunction(x,y)

The major con I see in their choice is that the user could provide different sizes for x and y that’d lead to errors.

What is the best programming practice? Note that perhaps this is not entirely related to Julia but programming in general.

I would suggest that you implement it in whichever form is best (performant, simple, whatever) for the package and then just provide a few different methods

somefunction(coords)
somefunction(xs, ys)

If performance is important perhaps document which is the most performant method.

1 Like

I would give the opposite advice. Just pick one, and make that the only interface. People that want to use or integrate your package into a wider piece of software can handle the translation.

2 Likes

The most natural here is to take an AbstractVector of tuples (or svectors). Then, if your algorithm is natively working with a vector of tuples – then use it as-is. If it’s most efficient with two separate vectors – convert all abstractvectors to StructArray.

This way, both you and users only care about one input “type”, limiting potential confusion.

2 Likes

A vector of tuples defines a set of points, two separate vectors defines a 2D grid.

If a function requires two separate vector arguments, my intuition tells me I’m defining a grid.

1 Like

Thank you to all of you for your relevant remarks! Does that mean one could provide a PR to improve Concorde.jl on that point? I believe they did not mean to provide a 2D-Grid but points instead.

It wouldnt be an improvement. It would be a breaking change :smile:

Just pick something and be consistent. There are arguments each way.

1 Like

Allowing vector-of-tuples might not be a breaking change. Disallowing multiple vectors would, definitely, though.

1 Like

I would go with two distinct vectors if it is reasonable to expect that the user will frequently have the data already in this format, and no unnecessary copy is needed. It is what is often done in mathematical programming.

Isn’t it basically a Matlabism? Matlab doesn’t support vectors of tuples, so you have to provide something less natural.

StructArrays is great for this usecase: no copy is needed, just do StructArray(; x, y) where x and y are your vectors. And you get all benefits, like ensuring consistent axes, convenient access to elements, and uniform familiar array interface.

2 Likes