QUESTION OF THE WEEK
“
How do you determine the best way to organize and split your Swift projects into separate files, including deciding what should go in each file and when to break content into multiple files?
— Sam
Separation of concern: it's a golden rule, especially when your keen on testing your code. Large files are hard to read, test, and navigate.
First make it work, then make it shine. I always find myself writing a solution first, all within the same file. For example, a SwiftUI View, several extensions, and a view model all within a single Swift file.
Once it all works, I start moving code around. Xcode 16 introduces a new feature "Create file from clipboard content" which makes this extra easy.
Eventually, you might even want to consider moving files into an individual package. Ideally, you would even indicate this beforehand and start writing any code within that package. I don't recommend doing this for every feature of your app, but if you know that code will be large or reused, it might be a good consideration.
These best practices will cover most cases, but you might occasionally still run into unexpected large files. Progressively migrate these into individual files whenever you touch the code for new changes. Don't go over your whole project after reading this answer, but take it step by step while in the context.
Your goal should be readable, self-explanatory code, and large files don't contribute.
Good luck!
Want to have your question answered next week? Ask your question via this form (anonymously)