Skip to content

Commit f5258ff

Browse files
committed
Guidelines: Add "Choosing Between Structs and Classes"
1 parent bd52c82 commit f5258ff

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

docs/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@
1313

1414
The guidelines for how Swift should be written and formatted can be found in the [Coding Style Guide](coding-style-guide.md).
1515

16+
### Choosing Between Structures and Classes
17+
18+
In addition to the [Apple guidelines](https://developer.apple.com/documentation/swift/choosing_between_structures_and_classes), we generally prefer to use `struct` for:
19+
20+
- Value types like the [Networking Models](../../../Networking/Networking/Model)
21+
- Stateless helpers
22+
23+
But consider using `class` instead if:
24+
25+
- You need to manage mutable states. Especially if there are more than a few `mutating` functions, the `struct` becomes harder to reason about.
26+
- You have to set a `struct` property declaration as `var` because it has a `mutating` function. In this case, a constant (`let`) `class` property may be easier to reason about.

0 commit comments

Comments
 (0)