mirror of
https://github.com/wnagrodzki/iOSProgrammingGuidelines.git
synced 2025-05-05 18:41:32 +02:00
Added "Golden (or Happy) Path" section
This commit is contained in:
parent
deb69201e3
commit
6dcc008fe8
1 changed files with 46 additions and 0 deletions
46
README.md
46
README.md
|
@ -112,3 +112,49 @@ guard !array.isEmpty else { return }
|
||||||
|
|
||||||
> This is to restrain overusing of `guard` statement where `if` would suffice.
|
> This is to restrain overusing of `guard` statement where `if` would suffice.
|
||||||
> `if array.isEmpty { return }`
|
> `if array.isEmpty { return }`
|
||||||
|
|
||||||
|
### Golden (or Happy) Path
|
||||||
|
|
||||||
|
Nesting `if` statements should be avoided.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
func buy(soda id: SodaID, with money: [Coin]) throws -> (Soda, [Coin]) {
|
||||||
|
if let soda = soda(for: id) {
|
||||||
|
if value(of: money) >= soda.price {
|
||||||
|
if let change = change(from: money, minus: soda.price) {
|
||||||
|
return (soda, change)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw PurchaseError.noChange
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw PurchaseError.insufficientFunds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw PurchaseError.outOfStock
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
All error conditions should be handled at the beginning of the function leaving "the working code" on the first indentation level.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
func buy(soda id: SodaID, with money: [Coin]) throws -> (Soda, [Coin]) {
|
||||||
|
guard let soda = soda(for: id) else {
|
||||||
|
throw PurchaseError.outOfStock
|
||||||
|
}
|
||||||
|
|
||||||
|
if value(of: money) < soda.price {
|
||||||
|
throw PurchaseError.insufficientFunds
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let change = change(from: money, minus: soda.price) else {
|
||||||
|
throw PurchaseError.noChange
|
||||||
|
}
|
||||||
|
|
||||||
|
return (soda, change)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
> It is easier to understand error conditions written in linear manner than as a nested structure.
|
||||||
|
|
Loading…
Add table
Reference in a new issue