SwiftUI, Combine, Clean Architecture and Why you need it.

Tirta Adi Gunawan
4 min readJul 4, 2020

When we see the clean architecture post, clean MMVN or any clean design pattern, many articles claim that his design is clean architecture, this would be hard to judge by the reader who hasn’t read any clean architecture books, as a sample, there is many clean architecture projects on github, but when we see the code, so many class is depending on Combine/RxSwift/any Reactive framework, from the controllers, VMs, Interactor, Model, this would make the app it self coupling with those framework and make the code base rigid for changes in the future, so we need to know the boundaries of framework or any technical implementation with business logic implementation.

This article is about proposing a new approach MVVM pattern with clean architecture and give an insight of benefit having clean architecture, this article may have flaws or violate few principles of clean architecture, so if you have some doubt, please put a question for that because I also like to challenge my understanding of clean architecture.

for the sample project download here

we will start from the clean architecture flow of control, this about the interaction and segregation between components.

the flow of control notebook app

All of the components are protocol-oriented meaning that all related operation class variables are written as protocol, so with this codebase able to accomplish composition over inheritance practice.

components table

that trying to follow a famous union diagram of clean architecture.

clean architecture union diagram

With those we got several advantages such as :

Composition over inheritance :

  • The Needed component registered as Protocol in the class.
  • Protocol as contract between components.

Clean, easy to maintain and readable business process :

  • Interactor and Entity importing only Foundation framework.
  • Interactor only contains entity to process, no network component or database.
  • covering all the function with unit test, since this would be easy to mock and testing it.

Clear UI or App flow :

  • Presenter for translate raw data given by interactor into more user friendly to viewmodel.
  • Combine framework only use on ViewModel.
  • View component (UIKit/SwiftUI) only use on ViewController as the humble object principle we do not test the viewcontroller.

External Component :

  • NoteService Protocol can have many implementation class, and can be used by many of NoteBookInteractor since all they know is protocol so the open closed principle is possible since we can have many implementation as we wanted.
  • Network, Core Data, file storing, or any external component is the responsiblity of service implementation, unit test in this part more about integration test, make sure that fragility of integration test does not make development harder and slower.

Benefit

  • The low coupling will make you easy to unit test.
  • Composition over inheritance this will make your way of code into a higher level, you can implement (TBD) Trunk Based Development and put a development toggle for a new feature, with TBD your chance to modify production code will be lesser and reduce the regression test by QA.
  • if you can do well unit tests, have a feature toggle and complete it with CI/CD, you able to do frequent release.

Challenges

  • Team knowledge gap, junior programmer who still in the pace of programming will be hard to get the full vision of this, they would likely to find the way to violate this design. Too much-experienced programmer if that person not able to getting taught by others, because no one able to change the person expects the person itself.
  • Strict rule of merge code, this need to make sure every single commit. since the good quality of codebase is long term commitment.
  • Business stakeholder who likes fast and done, if you can’t finish it in a certain time, you’re not a good programmer. Don’t let your ability to crafting a good quality software getting hijacked by a business stakeholder who think about his own personal benefit. Don't let your kid become a monster and that made you hate him over time.

Thank you

--

--