Refactor if/else with match