Java – inversion of return value
•
Java
I have a flag that I want to pass to a function that returns true or false according to the value in the map:
// userList is a List<String> and is stored as the value field in a map // user is a String if(flag) { if (userList == null) return false; else if(userList.size() == 0) return true; return userList.contains(user); } else { if (userList == null) return true; else if(userList.size() == 0) return false; return !userList.contains(user); }
My problem is: there are many copies to clean up the code anyway (if and else blocks are the same, except that their return values are opposite to each other)
I am not a very experienced code, I really appreciate some guidance!
Solution
Use flag values instead of constants
if (userList == null) return !flag; else if(userList.size() == 0) return flag;
XOR will be used for the final presentation (exercise for the reader: - P)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码