Assuming you want to test if an input is one of several constant Strings, and ignoring performance, is it an anti-pattern to code:
if ("yes oui ja da".contains(answer)) {
// answer was in the affirmative
}
instead of the more conventional:
private static List<String> affirmativeAnswers = Arrays.asList("yes", "oui", "ja", "da");
if (affirmativeAnswers.contains(answer)) {
// answer was in the affirmative
}
It’s a lot less code and easier to read, but is it a "hack"?
Edit:
For more safety, if you are worried about partial matches, you can code it as:
if (",yes,oui,ja,da,".contains(',' + answer + ','))
It’s still much less code (although getting ugly)
“Java – he don’t care!!”. But “Eww!! Nasty!!”1.
But seriously, you should be aiming to make your code easy to read and easy to maintain. (Or performant … if that matters.)
Tricky, obscure code that expresses something using the minimum number of keystrokes is none of the above. It is bad style … even if you get the code functionally correct.
1 – if you’ve been living under a rock for the past year or so … I’m alluding to this – http://knowyourmeme.com/memes/honey-badger.