Code refactor by using ternary operator instead of if..else..if statement in swift

Code duplication is a common issue which violates the DRY principle.

In order to solve the code duplication in case of a if..else..if statement where the else statement returns void.

if..else..if statement with else statement returning void

we can use a ternary operator for example, ternary conditional operator (question ? answer1 : answer2 ).

replacing if..else..if statement with ternary operator

we can solve the code duplication issue by using ternary conditional operator. Thus resolving the code duplication issue.

Junior Software Engineer | M.Sc. Computer Science from Technische Universitaet Berlin

Junior Software Engineer | M.Sc. Computer Science from Technische Universitaet Berlin

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store