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/Dusty_Coder Sep 11 '25

Wrong. The needle should expose methods that search haystacks.

needle.findwithin(haystack)

Unless I am wrong too, in which case there should be a NeedleFinder object that takes in needles and haystacks