4

I've been reading Robert Martin's Clean Code book where it suggests we should separate a programs startup/construction process from its run time logic.

In Java (the language the book uses) this involves moving all aspects of construction to main and designing the rest of the system assuming all objects have been constructed and wired up appropriately with no knowledge of main.

How can I apply this to iOS development?

My immediate thought is to construct all my business logic and models within the AppDelegate didFinishLaunchingWithOptions function. Although I'm not sure if this is the right function to use and whether it's feasible or wise to attempt to setup the ViewControllers my app will use all in one place.

1

Take a look at this iOS lifecycle. I have never read Clean Code, but I would suggest sticking to an MVC /MVVM pattern for iOS. iOS should do most of the heavy startup/construction process that I believe Robert is referring to.

Your business logic will go into your respective controllers (note I did not read that link I just grabbed the first google hit)

and MVVM... Full disclosure I am a little outdated on native iOS best practices I have since migrated to frameworks ... Ionic / React Native / etc...

  • I'm asking about setting up the classes and the dependencies of those classes which is unique to the app I create. iOS won't do this for me. I want to separate the construction of my classes with the runtime code. – Declan McKenna Apr 14 '18 at 13:35
1

It doesn't work like that. Most of the work is done directly or indirectly in your view controllers. You don't create view controllers when the app is launched, but when they are needed. And the real work is done in viewDidLoad. And viewControllers get destroyed when not needed anymore.

Lots of work is done in Singletons, and they are created when they are needed. Sometimes there are Singletons whose existence is needed, you create them in appDidFinishLaunching. Singletons stay alive forever.

You don't assume all objects have been created. You can't assume that because many objects will never be created. You create objects when you need them.

  • The book addresses objects that need to be created at runtime by setting up abstract factories implementations within main and passing the interface for the factory in to the runtime code. Although I've never seen anyone do this with ViewControllers. – Declan McKenna Apr 17 '18 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.