r/javahelp • • Sep 09 '25

`find(needle, haystack)` or `find(haystack, needle)`?

This is to learn about established conventions in the Java world.

If I write a new method that searches for a needle in a haystack, and receives both the needle and the haystack as arguments, in which order should they go?

Arrays.binarySearch has haystack, needle. But perhaps that's influenced by the class name, given that the class name is “arrays” and the haystack is also an array?

12 Upvotes

59 comments sorted by

View all comments

Show parent comment

1

u/sedj601 Sep 09 '25

Is this true? I have never seen that in any Java conventions. I could be wrong. I think that this is 100% the programmer's call. One counterexample is String.join(String delimiter, Collection)

https://www.oracle.com/java/technologies/javase/codeconventions-contents.html