Skip to content

Commit 38e6e0e

Browse files
committed
Update README.md
1 parent 60a59fd commit 38e6e0e

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,70 @@ func outputDetailsForFile(file: File) { ... }
110110
func outputDetailsForPath(path: String, fileSize: Int, extension: String,
111111
encoding enc: NSStringEncoding) { ... }
112112
```
113+
114+
115+
### Whitespaces around binary and unary operators.
116+
Rationale: Operators are usually special symbols(+-/) and when they dont have enough whitespaces they make code much harder to read. The rule is rather universal and holds for all operators:
117+
####Binary operators
118+
One space before and one space after
119+
###Unary operators
120+
Just once space either before or after the operator
121+
Examples:
122+
**Good:**
123+
```swift
124+
func <|< <A>(lhs: A, rhs: A) -> A
125+
```
126+
**Bad:**
127+
```swift
128+
func <|<<A>(lhs: A, rhs: A) -> A
129+
```
130+
The same holds for arithmetic operators
131+
**Good:**
132+
```swift
133+
let size = viewWidth - 2 * FIRST_CONSTANT - secondConstant - totalConstant
134+
```
135+
**Bad:**
136+
```swift
137+
let size=viewWidth-2*FIRST_CONSTANT-secondConstant-totalConstant
138+
```
139+
140+
The same rule for the return type of functions:
141+
142+
**Good:**
143+
```swift
144+
func sizeOfObject() -> Float {
145+
}
146+
```
147+
**Bad:**
148+
```swift
149+
func sizeOfObject()->Float{
150+
}
151+
```
152+
153+
The same rule holds for the Swift ternary operator (from Apple's Swift guide)
154+
**Good:**
155+
```swift
156+
let rowHeight = hasHeader ? 50 : 20
157+
```
158+
**Bad:**
159+
```swift
160+
let rowHeight=hasHeader?50:20
161+
```
162+
163+
Also, it stands for if else statements:
164+
**Good:**
165+
```swift
166+
if (condition) {
167+
performTask()
168+
} else {
169+
sleep()
170+
}
171+
```
172+
**Bad:**
173+
```swift
174+
if(condition){
175+
performTask()
176+
}else{
177+
sleep()
178+
}
179+
```

0 commit comments

Comments
 (0)