Skip to content

Commit b408d36

Browse files
authored
Add rule to prefer for loops over functional forEach(…) method (#234)
1 parent 741e55b commit b408d36

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,40 @@ _You can enable the following settings in Xcode by running [this script](resourc
12391239

12401240
</details>
12411241

1242+
* <a id='prefer-for-loop-over-forEach'></a>(<a href='#prefer-for-loop-over-forEach'>link</a>) **Prefer using `for` loops over the functional `forEach()` method**, unless using `forEach()` as the last element in a functional chain. [![SwiftFormat: forLoop](https://img.shields.io/badge/SwiftFormat-forLoop-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#forLoop)
1243+
1244+
<details>
1245+
1246+
#### Why?
1247+
For loops are more idiomatic than the `forEach()` method, and are typically familiar to all developers who have experience with C-family languages.
1248+
1249+
For loops are also more expressive than the `forEach()` method. For loops support the `return`, `continue`, and `break` control flow keywords, while `forEach()` only supports `return` (which has the same behavior as `continue` in a for loop).
1250+
1251+
```swift
1252+
// WRONG
1253+
planets.forEach { planet in
1254+
planet.terraform()
1255+
}
1256+
1257+
// WRONG
1258+
planets.forEach {
1259+
$0.terraform()
1260+
}
1261+
1262+
// RIGHT
1263+
for planet in planets {
1264+
planet.terraform()
1265+
}
1266+
1267+
// ALSO FINE, since forEach is useful when paired with other functional methods in a chain.
1268+
planets
1269+
.filter { !$0.isGasGiant }
1270+
.map { PlanetTerraformer(planet: $0) }
1271+
.forEach { $0.terraform() }
1272+
```
1273+
1274+
</details>
1275+
12421276
* <a id='omit-internal-keyword'></a>(<a href='#omit-internal-keyword'>link</a>) **Omit the `internal` keyword** when defining types, properties, or functions with an internal access control level. [![SwiftFormat: redundantInternal](https://img.shields.io/badge/SwiftFormat-redundantInternal-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#redundantInternal)
12431277

12441278
<details>
@@ -1564,6 +1598,8 @@ _You can enable the following settings in Xcode by running [this script](resourc
15641598
}
15651599
```
15661600

1601+
</details>
1602+
15671603
* <a id='anonymous-trailing-closures'></a>(<a href='#anonymous-trailing-closures'>link</a>) **Prefer trailing closure syntax for closure arguments with no parameter name.** [![SwiftFormat: trailingClosures](https://img.shields.io/badge/SwiftFormat-trailingClosures-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#trailingClosures)
15681604

15691605
<details>
@@ -1584,6 +1620,8 @@ _You can enable the following settings in Xcode by running [this script](resourc
15841620
planets.first { $0.isGasGiant }
15851621
```
15861622

1623+
</details>
1624+
15871625
### Operators
15881626

15891627
* <a id='infix-operator-spacing'></a>(<a href='#infix-operator-spacing'>link</a>) **Infix operators should have a single space on either side.** Prefer parenthesis to visually group statements with many operators rather than varying widths of whitespace. This rule does not apply to range operators (e.g. `1...3`) and postfix or prefix operators (e.g. `guest?` or `-1`). [![SwiftLint: operator_usage_whitespace](https://img.shields.io/badge/SwiftLint-operator__usage__whitespace-007A87.svg)](https://realm.github.io/SwiftLint/operator_usage_whitespace)

Sources/AirbnbSwiftFormatTool/airbnb.swiftformat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
--someAny disabled # opaqueGenericParameters
3333
--elseposition same-line #elseOnSameLine
3434
--guardelse next-line #elseOnSameLine
35+
--oneLineForEach wrap #forLoop
3536

3637
# We recommend a max width of 100 but _strictly enforce_ a max width of 130
3738
--maxwidth 130 # wrap
@@ -89,3 +90,4 @@
8990
--rules trailingClosures
9091
--rules elseOnSameLine
9192
--rules sortTypealiases
93+
--rules forLoop

0 commit comments

Comments
 (0)