eliminating code duplication if-else