Where can I find recommendations for the order of things in code such as subtypes, constants, stored properties, computed properties, initializers, and methods?
These things often come down to a style adopted by a team or an individual programmer's preferences if you're working solo.
If you're looking for a reference to adopt your own style, I think taking a look at the Swift Standard Library Programmers Manual would be a great place to start: swift/StandardLibraryProgrammersManual.md at main · apple/swift · GitHub
I covered order as it pertains to SwiftUI in a recent post.
The book “Clean Code” has some good advice on order generally.
The closest thing to community best practices are two opt-in rules in SwiftLint I wrote a couple of years ago:
They are customizable in case you find to dislike the defaults, but I recommend the defaults.
#if DEBUG isn’t necessary—they’re stripped by the compiler automatically. IIRC, the
#if DEBUG statements were included by Xcode in early SwiftUI templates because there was a bug that prevented the stripping, but they were removed from the templates when the bug was fixed.
Thanks! Based on this, in the definition of a SwiftUI view you would include the definition of the “body” computed property before initializers. Is that correct?
That is correct. If you dislike that, just override this setting by allowing initializers to be mixed with properties. The defaults were created way before SwiftUI was announced, so it is possible they are not perfect for SwiftUI, but in general they reflect a good order, so unless you have a good reason, don’t change the order of the rest.
I put them inside #if DEBUG anyways, because first that communicates also to the developer that they will be removed and second it allows to easily define further preview related stuff like extensions that are only available in previews and won’t compile to the released software. It’s about consistency, beating “minimal code” for me.