I came across such a code:
1 private boolean checkForSymmetricNulls(Object lhs, Object rhs) { 2 return (lhs == null) ? (rhs == null) : (rhs != null); 3 } 4 5 private boolean isSymmetricallyNullOrEquals(Object lhs, Object rhs) { 6 if (checkForSymmetricNulls(lhs, rhs)) { 7 if (lhs != null) { 8 return lhs.equals(rhs); 9 } 10 return true; 11 } 12 return false; 13 }
I was trying to figure out what this does for good 5 minutes and I realized that isSymmetricallyNullOrEquals and checkForSymmetricNulls could be replaced with one method:
1 private boolean equal(Object lhs, Object rhs) { 2 if (lhs == null || rhs == null) return lhs == rhs; 3 return lhs.equals(rhs); 4 }
I simplified the code slightly and also used the following refactoring:
http://www.refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html
but the following seem to be relevant:
http://www.refactoring.com/catalog/decomposeConditional.html
http://www.refactoring.com/catalog/consolidateConditionalExpression.html
http://www.refactoring.com/catalog/consolidateDuplicateConditionalFragments.html
Enjoy!
