# How to structure swift code

**URL:** <https://forums.swift.org/t/how-to-structure-swift-code/52570>\
**Category:** Using Swift\
**Tags:** structs, api-design, code-style\
**Created:** [October 3, 2021, 6:48pm UTC](https://forums.swift.org/t/how-to-structure-swift-code/52570 "2021-10-03T18:48:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacubit](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jacubit/32/27365_2.png) [@jacubit](https://forums.swift.org/u/jacubit)\
**Post date:** [October 3, 2021, 6:48pm UTC](https://forums.swift.org/t/how-to-structure-swift-code/52570/1 "2021-10-03T18:48:34Z")

</div>

Coming from C++, I have not yet understood two important things in Swift:

1. How do you structure the code in swift without headers in large projects without loosing track of the main interfaces or putting implementations details where it shouldn't?

2. I've seen some popular approach of writing code in swift where you define a struct or class holding the relevant data and then using `extension` to define `inits`, properties and functions. Is this a good practice? It seems at first sight difficult to navigate through many extensions just to understand what this struct is supposed to do. Why not define the init right away if you are introducing a new type?

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [October 3, 2021, 7:03pm UTC](https://forums.swift.org/t/how-to-structure-swift-code/52570/2 "2021-10-03T19:03:04Z")

</div>

There’s no one canonical way, but here are some articles that provide some reasonable perspectives:

> **[When should you use extensions in Swift? - a free Understanding Swift tutorial](https://www.hackingwithswift.com/quick-start/understanding-swift/when-should-you-use-extensions-in-swift)**
>
> Learn Swift coding for iOS with these free tutorials

> **[Four Clever Uses of Swift Extensions](https://cocoacasts.com/four-clever-uses-of-swift-extensions)**
>
> If you're watching this, then I assume you're familiar with Swift extensions. A Swift extension allows you to add functionality to a type, a class, a struct, an enum, or a protocol. But extensions are more powerful than that. In this episode, I'd...

> **[Organizing code using extensions | Swift by Sundell](https://www.swiftbysundell.com/tips/organizing-code-using-extensions/)**
>
> Frequently published articles about Swift techniques, language features, architectual patterns, and beyond.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [October 3, 2021, 7:08pm UTC](https://forums.swift.org/t/how-to-structure-swift-code/52570/3 "2021-10-03T19:08:17Z")

</div>

Also, note that even though Swift doesn’t have header files, you can view the generated interface of your code. Here’s a bit of a dated document on how to do this in Xcode:

> **[Technical Q&A QA1914: Viewing the interface of your Swift code](https://developer.apple.com/library/archive/qa/qa1914/_index.html)**
>
> QA1914: describes how to view the interface of your Swift code.
