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 golden path using guard #154

Merged
merged 1 commit into from
Apr 13, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 71 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Writing Objective-C? Check out our [Objective-C Style Guide](https://github.com/
* [Type Inference](#type-inference)
* [Syntactic Sugar](#syntactic-sugar)
* [Control Flow](#control-flow)
* [Golden Path](#golden-path)
* [Failing Guards](#failing-guards)
* [Semicolons](#semicolons)
* [Copyright Statement](#copyright-statement)
* [Smiley Face](#smiley-face)
Expand Down Expand Up @@ -526,6 +528,75 @@ for var i = 0; i < attendeeList.count; i++ {
}
```

## Golden Path

When coding with conditionals, the left hand margin of the code should be the "golden" or "happy" path. That is, don't nest `if` statements. Multiple return statements are OK. The `guard` statement is built for this.

**Preferred:**
```swift
func computeFFT(context: Context?, inputData: InputData?) throws -> Frequencies {

guard let context = context else { throw FFTError.NoContext }
guard let inputData = inputData else { throw FFTError.NoInputData }

// use context and input to compute the frequencies

return frequencies
}
```

**Not Preferred:**
```swift
func computeFFT(context: Context?, inputData: InputData?) throws -> Frequencies {

if let context = context {
if let inputData = inputData {

// use context and input to compute the frequencies

return frequencies
}
else {
throw FFTError.NoInputData
}
}
else {
throw FFTError.NoContext
}
}
```

When multiple optionals are unwrapped either with `guard` or `if let`, minimize nesting by using the compound version when possible. Example:

**Preferred:**
```swift
guard let number1 = number1, number2 = number2, number3 = number3 else { fatalError("impossible") }
// do something with numbers
```

**Not Preferred:**
```swift
if let number1 = number1 {
if let number2 = number2 {
if let number3 = number3 {
// do something with numbers
}
else {
fatalError("impossible")
}
}
else {
fatalError("impossible")
}
}
else {
fatalError("impossible")
}
```

### Failing Guards

Guard statements are required to exit in some way. Generally, this should be simple one line statement such as `return`, `throw`, `break`, `continue`, and `fatalError()`. Large code blocks should be avoided. If cleanup code is required for multiple exit points, consider using a `defer` block to avoid cleanup code duplication.

## Semicolons

Expand Down