I'm designing an API in Java for a set of numerical algorithms that act on arrays of doubles (for real-time financial statistics as it happens). For performance reasons the API has to work with primitive arrays, so List<Double>
and suchlike are not an option.
A typical use case might be an algorithm object that takes two input arrays, and returns an output-array that contains a result computed from the two inputs.
I'd like to establish consistent conventions for how the array parameters are used in the API, in particular:
someFunction(double[] input, int inputOffset, int length)
The objectives are a to achieve a balance of efficiency, simplicity for API users and consistency both within the API and with established conventions.
Clearly there are a lot of options, so what is the best overall API design?
So that really sounds like three questions, so here are my opinions.
Of course, this is very subjective - so - your mileage may vary:
Yes. Always include offset & length. If most use cases for a particular function don't need those parameters, overload the function so that input & length are not required.
For this, I would follow the standard used by arraycopy
:
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
The performance difference here is going to negligible unless the caller repeatedly calls your utility functions. If they're just one off things, there should be no difference. If they are called repeatedly than you should have the caller send you an allocated array.
Assuming that you are working with arrays small enough to be allocated on the stack or in Eden, allocation is extremely fast. Therefore, there is no harm in having functions allocate their own arrays to return results. Doing this is a big win for readability.
I would suggest starting out making your functions operate on whole arrays, and introduce an option to call a function with just a slice of an array only if you find out that it is useful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With