Single Responsibility Principle for Methods

In my last post, you learned what the Single Responsibility Principle is, why it is important, and its benefits. You’re now able to apply SRP at the class level.

Today, I’ll show you how to apply SRP at the method level.

By looking at some practical examples, you’ll learn how to spot potential code smells so that you can refactor your methods into shorter methods. You’ll write shorter methods with each doing just one thing. This’ll make it much easier to write unit tests because the inputs and outputs become obvious. There are also fewer edge cases per method.

Continue reading →

Single Responsibility Principle for Class

You saw how the Dependency Inversion Principle, although lessor known, is the core guiding principle for a good clean architecture such as Clean Swift.

There’s a much more popular principle called the Single Responsibility Principle. You’ve likely heard of it. It’s easy to throw it around to say:

Make sure each of your methods does only one thing.

It sounds easy, simple, and logical. I mean, it makes sense, right?

But, in practice, how do you apply it to your app?

Continue reading →

All About Routing in Clean Swift

Since I started talking about Clean Swift iOS architecture, I’ve received a lot of positive feedback. Many people have downloaded my Xcode templates and put them to good use. I am truly grateful that I am able to help you write better apps.

Many of you also asked some very insightful questions. I’ve answered some but not all. One question jumps out to me is routing and data passing.

In my original post on Clean Swift, I only showed the CreateOrder scene. And then I wrote a few posts about TDD that adds a ListOrders scene as the initial scene. When the user taps the + button in the navigation bar, it routes to the CreateOrder scene.

But that’s only a very simple segue. It’s not enough to show you exactly how routing should work. Especially how passing data should be done.

In this post, I’ll add a ShowOrder scene. When the user selects an order in the table view in the ListOrders scene, it’ll transition to the ShowOrder scene to show the order details.

Continue reading →

Dependency Inversion – A Little Swifty Architecture

Uncle Bob’s recent post A Little Architecture explains what the Dependency Inversion Principle is. It shows why it is the foundation for a clean application architecture.

The Dependency Inversion Principle is the most important software design principle. If you aren’t into reading programming literature, you should at least understand this one by reading this post. It’ll guide all your future application architecture design decisions.

Uncle Bob’s code examples are written in Java. But in this post, I’ll explain the Dependency Inversion Principle in terms of Swift.

If your app is properly architected, even though Parse is shutting down, it’ll be easy to replace it with whatever you want. I’ll show you why and how.

In addition to just showing the Swift code, you’ll also learn:

  • What is the Dependency Inversion Principle?
  • Why is it important?
  • What makes it possible?
  • What benefits does it provide?

Continue reading →

Refactoring table view data source and delegate methods

A lot has been said about the massive view controller problem and how to slim it down. Specifically, you’ve most likely seen how to move the table view data source and delegate methods out of the view controller. The result is fewer lines of code in the view controller.

In this post, we are going to look at some code and different approaches to see if it’s really such a good idea. If not, what should we do instead? Hopefully, you’ll come away with new insights on what you want to do with your own apps.

Continue reading →