You can use a map to simplify the second when
# codereview
e
You can use a map to simplify the second when