SOLID Principles for iOS Apps

SOLID is a group of principles that lead you to write clear and organized code without additional effort. Learn how to apply it to your SwiftUI iOS apps. By Ehab Amer.

4.6 (29) · 3 Reviews

Download materials
Save for later
Share
You are currently viewing page 5 of 5 of this article. Click here to view the first page.

Auditing the App Again

Take another look at your app. With the changes you made, you resolved all the issues you identified in the first round:

  1. Core Data setup is no longer in AppMain and you separated it.
  2. Your app doesn't rely on Core Data. It can now freely use any kind of storage with minimal changes to your code.
  3. Adding new report types is a matter of adding a new value in an enum.
  4. Creating previews and tests is a lot easier than before, and you no longer need any complex mock objects.

There's a big improvement between how the project was before you started and how it is now. It didn't require much effort, and you reduced the amount of code as a side benefit.

Following SOLID isn't about executing a set of rules or about the setup of your architecture. Rather, SOLID gives you some guidelines to help you write code in a more organized way.

It makes fixing bugs safer since your objects aren't entangled. Writing unit tests is easier. Even reusing your code from one project to another is effortless.

Writing clean and organized code is a goal that always pays off. If you say, "I'll clean this up later", when that later moment comes, things will usually be too complicated to actually clean.

Where to Go From Here?

You can download the completed project files by clicking the Download Materials button at the top or bottom of the tutorial.

Using design patterns in your code offers simple solutions to what seems like a complex problem. Whether you know the fundamental iOS design patterns or not, it's always good to refresh your memory about them. Our Fundamental iOS Design Patterns tutorial can help.

Unit testing is a critical aspect of software development. Your tests need to focus on small parts of your code. Learn all about Dependency Injection to write great unit tests.

Another interesting concept that would improve the way you think about writing your apps is Defensive Programming. It's about having your code anticipate what might go wrong so your app isn't fragile and doesn't crash when it receives unexpected input.

A simple example of defensive coding is using guard let instead of force unwrapping when you deal with optionals. Being aware of such topics improves the quality of your work without any additional effort.

We hope you enjoyed this tutorial. If you have any questions or comments, please join the forum discussion below!