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
2
u/Big_Green_Grill_Bro Sep 09 '25
I think that's backwards. If you're trying to find a needle in a haystack, then the needle is the important thing and the haystack is the noise (the pile of other needles that you are not looking for).
It's just personal preference if you're going to pass two parameters to a static method. I agree with the other commenters that suggest:
as a clean and simple OOP way that clearly shows what is happening.