Home Writing SOLID Code in Swift
Post
Cancel

Writing SOLID Code in Swift

Introduction

SOLID is a popular set of design principles that was popularized by Robert C. Martin and is used in object-oriented programming. It is an acronym that stands for five distinct design principles that empowers developers to write better software that is needlessly complex, fragile or rigid.

Single responsibility Principle TL;DR → An entity should only do one thing, and have only one reason to change
Open Closed Principle TL;DR → An entity should be open to extension but closed to modification. Basically, you should be able to extend a class’s behavior without modifying it
Liskov Substitution Principle TL;DR → Objects should be replaceable with objects of their subtype, without changing the correctness of usage
Interface Segregation Principle TL;DR → Many specific interfaces (protocols in swift) are better than one large interface.
Dependency Inversion Principle TL;DR → Depend on abstractions, not concretions.

As a north star, the SOLID principles can provide software developers with a set of checklists to affirm a code base’s cleanness. A clean code in the software world is one that is clear about what it does, maintainable and easy for others to understand. Following the SOLID principles in it’s essence can help us write cleaner code.

Misconceptions

Language, framework and tech stack agnostic

The most common misconceptions about the SOLID principles is that it is made for specific programming languages, frameworks or technology. The reality is, the SOLID principles are language agnostic. They can be deployed to any of your favorite tech stacks or used in any of your favorite programming languages. And therein lies the beauty of the SOLID principles; learn them once, and apply them in every paradigm of software development that is applicable to you.

The Order

There is no defined optimal order to applying the principles in your projects. The SOLID acronym was simply arranged that way by Robert C. Martin’s friend because it is easier to spell and remember.

The right way to apply them

There is no reason to apply all 5 principles in every project, class or module. They should only be applied when they are applicable to your project’s needs. Remember each principle proposes a solution that addresses a specific problem, therefore only apply when applicable.

Going Forward

I’ll be creating a series of posts that explains each of the SOLID principles from an iOS-centric Swift POV. The articles will include swift tutorials with examples that addresses the problems that each principle helps us solve.

Thanks for reading👋

This post is licensed under CC BY 4.0 by the author.