r/javahelp • u/hibbelig • 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
View all comments
Show parent comment
1
u/Temporary_Pie2733 Sep 09 '25
Haskell has an
elemfunction that takes the needle first, though that’s partly because it’s intended to be used as an infix operator usingneedle `elem` haystack. The OOP version of that might be an finding wrapper around the needle with a method that takes a haystack as an argument,Find(needle).inside(haystack).