How confusing is `new SomeCollection(values…)::contains` as a Predicate? [closed]
https://softwareengineering.stackexchange.com/questions/306181
-
10-12-2020 - |
سؤال
Traditionally, a function that want to skip certain items during processing will accept a Collection
or var-args argument, but in the Java 8 world I think I should switch to Predicate
s.
Now, since the code I am working on is mainly for testing, most of the exclusion will be ad-hoc "literals", so I am suggesting users use the form
Arrays.of(a, b, ...)::contains
I am just wondering how acceptable this is?
(I see little value in wrapping the above form in an overload because it will be one more signature to learn and prevents the simple optimisation of extrating this "literal" to a variable.)
Edit:
Old function will be like
public void processUnlessNameIn(String... names)
I want the new one to be
public void processUnlessName(Predicate<String> pred)
المحلول
To directly answer your question: "somewhat confusing", since the code example you gave just doesn't sit right, for me anyway.
TBH, if your collection always contains string
s, then I think adding predicates/lambdas is overkill in this situation. What does it really get you? If you have a collection of a richer object type, and want the user to be able to operate on that, then supplying a lambda instead of many overrides for every possible filtering choice would be sensible.
(Also, I'd rename processUnlessName(...)
to processExcept(...)
, which seems clearer, but that's purely my opinion.)