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 style guide rules. #368

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
87 changes: 87 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,69 @@ let color = "red"
```swift
let colour = "red"
```
### Acronym Capitalisation

Acronyms should be capitalised, except when used at the beginning of a method or variable name.

__Preferred:__

```swift
func loadURL(url: String) {
}
```

__Not Preferred:__

```swift
func loadUrl(url: String) {
}
```
__Rationale:__

This matches the style used in the Cocoa Touch frameworks, e.g [`HTTPURLResponse`](https://developer.apple.com/documentation/foundation/nshttpurlresponse?language=objc) instead of `HttpUrlResponse`, or [`value(forHTTPHeaderField:)`](https://developer.apple.com/documentation/foundation/nsurlrequest/1409376-value) instead of `value(forHttpHeaderField:)`.

### Enumerations

Use lowerCamelCase for enumeration cases. Prefer one enum case per line:

```swift
enum Shape {
case rectangle
case square
case triangle
case circle
}
```

#### Raw values

Enumerations that are declared as storing integer or string raw values should implictly assign the raw values for each case wherever possible.

__Preferred:__

```swift
enum CompassPoint: String {
case north
case south
case east
case west
}
```

__Not Preferred:__

```swift
enum CompassPoint: String {
case north = "north"
case south = "south"
case east = "east"
case west = "west"
}
```

__Rationale:__

The raw values for each case of enumerations that are declared with integer or string raw value types are [assigned automatically by Swift](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/enumerations/#Raw-Values) if no raw value is explicitly assigned for a given case.

## Code Organization

Expand Down Expand Up @@ -286,6 +349,30 @@ import UIKit
var deviceModels: [String]
```

### Function Odering

Ordering of functions should be parent -> child descending where possible. This is primarily to make it easier to follow without to much jumping and define some kind of order. It may not always be possible to achieve exactly but maintain the principle that things call downwards.

```swift
func doSomething() {
let number = doMore()
doEvenMore(with: number)
}

func doMore() -> Int {
return findANumber()
}

func findANumber() -> Int {
return 0
}

func doEvenMore(with number: Int) {
print(number)
}
```


## Spacing

* Indent using 2 spaces rather than tabs to conserve space and help prevent line wrapping. Be sure to set this preference in Xcode and in the Project settings as shown below:
Expand Down