Swift: 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.

If you like my articles please follow me. You can also support me by https://www.buymeacoffee.com/akarshseggemu

--

--

Akarsh Seggemu

IT Team Lead | MSc in Computer Science from Technische Universität Berlin | Writing articles to Empower Software Engineers and IT Leaders