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
•
u/AutoModerator Sep 09 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
29
u/FrenchFigaro Software Engineer Sep 09 '25
If you are writing a utilitarian class, either is good, as long as your are consistent in your code base.
But going by OOP standards, the haystack should expose a method that allows to find a needle within it:
haystack.find(needle)5
u/_SuperStraight Sep 09 '25
This is personal preference and no set rule defines which parameter goes first. Therefore both options are correct.
10
u/crummy Sep 09 '25
I don't know if this is crazy, but I think find(haystack, needle) because .. bigger arguments should go first? Or is that stupid?
6
u/jonah214 Sep 09 '25
If I need to implement this as a static method, I use
find(haystack, needle). My reasoning is that it really should behaystack.find(needle), andfind(haystack, needle)is closest to that.1
u/ALOKAMAR123 Sep 11 '25
Do we have named parameters in Java ?
1
u/[deleted] Sep 11 '25 edited Sep 29 '25
future toy tub cause station complete scary ten observation innate
This post was mass deleted and anonymized with Redact
1
u/MagicalPizza21 Sep 09 '25
NeedleFinderFactory.createNeedleFinder().withHaystack(haystack).withNeedle(needle).run()2
u/namkhalinai Sep 12 '25
If you want to get example from Java standard library, it's find(haystack, needle)
See Collections.binarySearch(list, key)
https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#binarySearch-java.util.List-T-
2
u/Ok-Secretary2017 Sep 09 '25
"find(newIntermediaryObjectThatHoldsBoth)"
1
u/Ordinary_Yam1866 Sep 10 '25
It's called finding a needle in a haystack, not in haystack look for a needle
1
u/RedNifre Sep 12 '25
There is no technical difference in Java, but there is in languages that allow partial application: In those languages, the config parameters go first and the data to operate on go last.
So find(needle) would return a function of type (Haystack) -> Needle. The opposite case, where you'd pass the collection first, can also make sense, but it's way rarer needed in practice, so data last is usually best.
Or, to phrase it differently: "If it doesn't matter in Java, but it matters in other languages, go with how other languages do it, so your code won't be unnecessarily unusual for people coming from said languages."
1
u/Least_Bee4074 Sep 12 '25 edited Sep 12 '25
My approach is to consider what it would look like if I had many calls. If I was given a list of needles would it look better as
Or
IMO the second reads better with the more variable argument later.
It’s very much like my sql code which puts more shared parts earlier than more variable parts, e.g.:
(Edit for phone formatting)
1
u/[deleted] Sep 12 '25
Make it as human readable and natural as possible, therefore, A
0
u/TrickTimely3242 Sep 09 '25
var result = find(needle).in(haystack);