Ask HN: How do you write quality iOS codebases?

7 points by gregkerzhner ↗ HN
I have been doing both front end development and iOS development for 5 years. I have found that on the front end, its gotten pretty easy to write clean maintainable code. Tools like Redux lets you cleanly move businesses logic out of the view layer and React allows you to have an easily written declarative interface.

iOS seems pretty far behind in code quality. Most iOS projects I have seen have thousands of lines long view controllers containing tons of logic, globals everywhere, 200 line functions, little to no unit testing, and no architecture. For those experienced iOS devs out there - do you have healthy iOS code bases you are proud of, and what do those look like from an architecture standpoint?

I have personally tried to introduce different types of architectures like MVVM and even Redux, but this has always been rocky on iOS, both from a tech standpoint because you are working against the ecosystem, and from a human perspective because (and this is a huge generalization) a lot of iOS devs tend to have an old school mentality and don't see the issues I am pointing out as problems - thats just how things work on iOS.

I would love to hear from someone that has experience cleaning up messy legacy iOS codebases, or examples of healthy iOS architectures that are open source.

5 comments

[ 2.7 ms ] story [ 16.3 ms ] thread
I'm currently at a company where our iOS product has millions of paying customers. FWIW:

We run a flavor of the coordinator pattern + MVVM. We use delegation to handle messaging between view models and view controllers. Services are currently handled by a super ugly singleton, but that's a by-product of legacy architecture. New features usually inject the service directly into the view model.. and there has been some talk in potentially moving the service to the coordinator to mimic a unidirectional data flow.

The issues you mention, i.e. globals, lack of unit testing, fat view controllers and lengthy functions... none of that stuff, if included in a pull request, would pass our code review. So generally we self police each other to avoid code smell.

I think the thing that is very tempting to do is to just have a ViewModelDelegate (which would be the controller) that has one method `func stateChanged(state: ViewState)` and then have the controller rerender itself whenenver that function is called. This would be nice and redux-y since the view could be a pure function of the state. But since iOS doesn't have a declarative user interface or the virtual dom, you would potentially be rerendering the whole screen when just one part of it needs to change. How do you guys overcome that? Or do you have multiple delegate methods that the view model calls on the controller depending on what needs to happen?
I don’t have much experience in iOS/Swift, but would SwiftUI address the declarative part?

From what I’ve seen you can supply state to a view and it’ll take care of the rendering for you.

I have a similar skillset to you and to be fair I've seen some pretty horrible React codebases in my time as well. You're correct though the architecture story is far behind on iOS, SwiftUI and Combine are attempts to address this and in my opinion they work really well. MVVM and Redux / Elm style architectures are really trivial to implement now. There is also a push for more functional approaches in general, if you look at videos from talk.objc.io or pointfree.co both of these focus on functional approaches to common architecture patterns. I hope that these recent developments from Apple are a signal of things to come in terms of providing better developer experience and insentives to use the platform's toolkit. To remain relevant I feel Apple has a huge amount of work to do in this area, documentation, XCode improvements and making SwiftUI a proper UIKit competitor (currently you will have to combine them for anything non-trivial).
Thanks for those examples. The kickstarter app is opensourced and seems to be a good example of the MVVM pattern - https://github.com/kickstarter/ios-oss/tree/master/Kickstart...

Ive been out of the iOS game for about two years and now getting back into it. Have you had luck convincing your organization to use SwiftUI? What have been the painpoints? I am finding that people on my team are pretty traditional, even hesitating to use autolayout, so pushing SwiftUI might be an uphill battle.