Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SwiftUI Property declaration to sub section ordering list #282

Merged
merged 8 commits into from
Aug 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 66 additions & 94 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3864,85 +3864,81 @@ _You can enable the following settings in Xcode by running [this script](resourc
* If the type in question is a simple value type (e.g. fewer than 20 lines), it is OK to omit the `// MARK:`s, as it would hurt legibility.

* <a id='subsection-organization'></a>(<a href='#subsection-organization'>link</a>) **Within each top-level section, place content in the following order.** This allows a new reader of your code to more easily find what they are looking for. [![SwiftFormat: organizeDeclarations](https://img.shields.io/badge/SwiftFormat-organizeDeclarations-008489.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#organizeDeclarations)
- **Nested types and type aliases**:
- **Static properties**:
- **Static property with body**:
- **Class properties with body**:
- **SwiftUI dynamic properties (@State, @Environment, @Binding, etc)**: SwiftUI Dynamic properties should appear grouped since they share a common semantic meaning and cause a SwiftUI view to re-render its body.

<details>

```swift
// WRONG

struct CustomSlider: View {

// Internal

var body: some View {
...
}

// Private

@Binding private var value: Value
private let range: ClosedRange<Double>
@Environment(\.DLSSliderStyle) private var style
private let step: Double.Stride
@Environment(\.layoutDirection) private var layoutDirection
* Nested types and type aliases
* Static properties
* Static property with body
* Class properties with body
* SwiftUI dynamic properties (@State, @Environment, @Binding, etc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@miguel-jimenez-0529 what do you think about generalizing this section to be any property with a property wrapper? I'm curious about the reason to scope this specifically to SwiftUI property wrappers. cc: @calda

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me and Cal had the same conversation. Our conclusion was that there are @ properties like @objc that are not property wrappers. Also a property wrapper doesn't have a common semantic meaning, examples of those are @Referenced, @UniBindable, @Atomic, @Resilient. So grouping all property wrappers when we don't know upfront their functionality was a bit opinionated. Engs may have a better idea on where to place them.
This is different for SwiftUI property wrappers since they all conform to the DynamicProperty protocol and have a common semantic meaning in the sense that they interact with the UI.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense. Thanks for explaining!

* Instance properties
* Instance properties with body
* Static methods
* Class methods
* Instance methods

<details>

Computed properties and properties with property observers should appear at the end of the set of declarations of the same kind. (e.g. instance properties.) [![SwiftFormat: organizeDeclarations](https://img.shields.io/badge/SwiftFormat-organizeDeclarations-008489.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#organizeDeclarations)
miguel-jimenez-0529 marked this conversation as resolved.
Show resolved Hide resolved

```swift
// WRONG
var atmosphere: Atmosphere {
didSet {
print("oh my god, the atmosphere changed")
}

// RIGHT

struct CustomSlider: View {

// Internal

var body: some View {
...
}

// Private

@Environment(\.DLSSliderStyle) private var style
@Environment(\.layoutDirection) private var layoutDirection
@Binding private var value: Value

private let range: ClosedRange<Double>
private let step: Double.Stride
}
var gravity: CGFloat
// RIGHT
var gravity: CGFloat
var atmosphere: Atmosphere {
didSet {
print("oh my god, the atmosphere changed")
}
```
}
```

SwiftUI Properties are a special type of property that lives inside SwiftUI views. These views conform to the DynamicProperty protocol and cause the view's body to re-compute. Given this common functionality and also a similar syntax, it is preferred to group them.
miguel-jimenez-0529 marked this conversation as resolved.
Show resolved Hide resolved

```swift
// WRONG

struct CustomSlider: View {

</details>
// MARK: Internal

var body: some View {
...
}

- **Instance properties**:
- **Instance properties with body**: Computed properties and properties with property observers should appear at the end of the set of declarations of the same kind. (e.g. instance properties.) [![SwiftFormat: organizeDeclarations](https://img.shields.io/badge/SwiftFormat-organizeDeclarations-008489.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#organizeDeclarations)
// MARK: Private

<details>
@Binding private var value: Value
private let range: ClosedRange<Double>
@Environment(\.sliderStyle) private var style
private let step: Double.Stride
@Environment(\.layoutDirection) private var layoutDirection
}

```swift
// WRONG
var atmosphere: Atmosphere {
didSet {
print("oh my god, the atmosphere changed")
}
}
var gravity: CGFloat
// RIGHT

// RIGHT
var gravity: CGFloat
var atmosphere: Atmosphere {
didSet {
print("oh my god, the atmosphere changed")
}
}
```
struct CustomSlider: View {

// MARK: Internal

</details>
var body: some View {
...
}

- **Static methods**:
- **Class methods**:
- **Instance methods**:
// MARK: Private

@Environment(\.sliderStyle) private var style
@Environment(\.layoutDirection) private var layoutDirection
@Binding private var value: Value

private let range: ClosedRange<Double>
private let step: Double.Stride
}
```
</details>


* <a id='newline-between-subsections'></a>(<a href='#newline-between-subsections'>link</a>) **Add empty lines between property declarations of different kinds.** (e.g. between static properties and instance properties.) [![SwiftFormat: organizeDeclarations](https://img.shields.io/badge/SwiftFormat-organizeDeclarations-008489.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#organizeDeclarations)
Expand All @@ -3964,30 +3960,6 @@ _You can enable the following settings in Xcode by running [this script](resourc

</details>

**Computed properties and properties with property observers should appear at the end of the set of declarations of the same kind.** (e.g. instance properties.) [![SwiftFormat: organizeDeclarations](https://img.shields.io/badge/SwiftFormat-organizeDeclarations-008489.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#organizeDeclarations)

<details>

```swift
// WRONG
var atmosphere: Atmosphere {
didSet {
print("oh my god, the atmosphere changed")
}
}
var gravity: CGFloat

// RIGHT
var gravity: CGFloat
var atmosphere: Atmosphere {
didSet {
print("oh my god, the atmosphere changed")
}
}
```

</details>

**[⬆ back to top](#table-of-contents)**

## Objective-C Interoperability
Expand Down
Loading