Skip to content
Advertisement

Optimize the several multiple OR conditions in IF statement

I am new to java and trying to learn a better way of coding.Please let me know if I can replace the multiple OR conditions in the below ELSE IF statement with any other way to execute based on the username passed in the calling method :

JavaScript

Advertisement

Answer

Here’s one way: define a Set<String> with the possible username values before the if block, and check against it. Notice how all the strings were lower-cased to avoid trouble:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement