8000 Merge pull request #5 from niwatako/niwatako/master · sync2003/swift-style-guide@bd5c551 · GitHub
[go: up one dir, main page]

Skip to content

Commit bd5c551

Browse files
authored
Merge pull request jarinosuke#5 from niwatako/niwatako/master
Update to github/swift-style-guide@b1b2914
2 parents f058399 + 4e38313 commit bd5c551

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
This guide is primarily intended to be _descriptive_, detailing the usage
2+
that Swift developers at GitHub have arrived at in cases of disagreement.
3+
Consider whether a proposed addition would make more sense in a fork used
4+
by your projects/organization instead.
5+
16
If you want to suggest a change or addition that will help accomplish
27
[the goals](README.md) of this style guide, please open a pull request that:
38

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,35 @@ Use `let foo = …` over `var foo = …` wherever possible (and when in doubt).
2828

2929
_Rationale:_ The intent and meaning of both keywords is clear, but *let-by-default* results in safer and clearer code.
3030

31-
A `let`-binding guarantees and *clearly signals to the programmer* that its value is supposed to and will never change. Subsequent code can thus make stronger assumptions about its usage.
31+
A `let`-binding guarantees and *clearly signals to the programmer* that its value will never change. Subsequent code can thus make stronger assumptions about its usage.
3232

3333
It becomes easier to reason about code. Had you used `var` while still making the assumption that the value never changed, you would have to manually check that.
3434

3535
Accordingly, whenever you see a `var` identifier being used, assume that it will change and ask yourself why.
3636

37+
### Return and break early
38+
39+
When you have to meet certain criteria to continue execution, try to exit early. So, instead of this:
40+
41+
```swift
42+
if n.isNumber {
43+
// Use n here
44+
} else {
45+
return
46+
}
47+
```
48+
49+
use this:
50+
```swift
51+
guard n.isNumber else {
52+
return
53+
}
54+
// Use n here
55+
```
56+
57+
You can also do it with `if` statement, but using `guard` is prefered, because `guard` statement without `return`, `break` or `continue` produces a compile-time error, so exit is guaranteed.
58+
59+
3760
#### Avoid Using Force-Unwrapping of Optionals
3861

3962
If you have an identifier `foo` of type `FooType?` or `FooType!`, don't force-unwrap it to get to the underlying value (`foo!`) if possible.
@@ -59,7 +82,7 @@ _Rationale:_ Explicit `if let`-binding of optionals results in safer code. Force
5982

6083
#### Avoid Using Implicitly Unwrapped Optionals
6184

62-
Where possible, use `let foo: FooType?` instead of `let foo: FooType!` if foo may be nil (Note that in general, `?` can be used instead of `!`).
85+
Where possible, use `let foo: FooType?` instead of `let foo: FooType!` if `foo` may be nil (Note that in general, `?` can be used instead of `!`).
6386

6487
_Rationale:_ Explicit optionals result in safer code. Implicitly unwrapped optionals have the potential of crashing at runtime.
6588

@@ -190,7 +213,7 @@ class Vehicle {
190213
}
191214

192215
func maximumTotalTirePressure(pressurePerWheel: Float) -> Float {
193-
return pressurePerWheel * numberOfWheels
216+
return pressurePerWheel * Float(numberOfWheels)
194217
}
195218
}
196219

@@ -215,7 +238,7 @@ protocol Vehicle {
215238
}
216239

217240
func maximumTotalTirePressure(vehicle: Vehicle, pressurePerWheel: Float) -> Float {
218-
return pressurePerWheel * vehicle.numberOfWheels
241+
return pressurePerWheel * Float(vehicle.numberOfWheels)
219242
}
220243

221244
struct Bicycle: Vehicle {
@@ -284,3 +307,5 @@ _Rationale:_ Operators consist of punctuation characters, which can make them di
284307

285308
* [中文版](https://github.com/Artwalk/swift-style-guide/blob/master/README_CN.md)
286309
* [日本語版](README_JP.md)
310+
* [한국어판](https://github.com/minsOne/swift-style-guide/blob/master/README_KR.md)
311+
* [Versión en Español](https://github.com/antoniosejas/swift-style-guide/blob/spanish/README-ES.md)

README_JP.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,37 @@ Swift コーディング規約
2828

2929
_理由:_ 二つのキーワードの意図と意味は明瞭ですが、*デフォルトでlet*を使うことは、より安全でより明確なコードになります。
3030

31-
`let`宣言は、その変数の値が変わらないとを想定されていて、かつ実際に変わらない
32-
ことを保証すると同時に、それを*プログラマに明確に伝えます*。そのため、その後に続くコードにおいて、その変数の用途を推測しやすくなります。
31+
`let`宣言は、その変数の値が変わらないことを保証すると同時に、それを*プログラマに明確に伝えます*。そのため、その後に続くコードにおいて、その変数の用途を推測しやすくなります。
3332

3433
コードを論理的に理解するのがより簡単になります。値が決して変わらないと考えているにもかかわらず`var`を使うと、本当に値が変わらないかどうかを手動でチェックしなければいけません。
3534

3635
この方法に従うと結果的に、`var`宣言が使われているのを発見した時は必ず、その値が変わり得ると推測したり、その理由を考えることができます。
3736

3837

38+
#### Return と break を早期に行う
39+
40+
実行を継続するには基準を満たす必要があるような場合は、なるべく早期にコードブロックから抜け出すようにしましょう。
41+
42+
その際、以下のようにする代わりに:
43+
```swift
44+
if n.isNumber {
45+
// Use n here
46+
} else {
47+
return
48+
}
49+
```
50+
51+
以下のように書きましょう:
52+
```swift
53+
guard n.isNumber else {
54+
return
55+
}
56+
// Use n here
57+
```
58+
59+
If文でも同じことが可能ですが、`guard`を使うことが推奨されます。`guard`文では`return``break``continue`が無いとコンパイルエラーとなるため、コードブロックから抜け出すことが保証されるからです。
60+
61+
3962
#### オプショナル型の開示指定は避ける
4063

4164
`FooType?`もしくは`FooType!`型の変数`foo`がある場合、そこにある`foo!`を得ようとして`foo`に対して開示指定を行うのは可能な限り避けないといけません。
@@ -188,7 +211,7 @@ class Vehicle {
188211
}
189212

190213
func maximumTotalTirePressure(pressurePerWheel: Float) -> Float {
191-
return pressurePerWheel * numberOfWheels
214+
return pressurePerWheel * Float(numberOfWheels)
192215
}
193216
}
194217

@@ -213,7 +236,7 @@ protocol Vehicle {
213236
}
214237

215238
func maximumTotalTirePressure(vehicle: Vehicle, pressurePerWheel: Float) -> Float {
216-
return pressurePerWheel * vehicle.numberOfWheels
239+
return pressurePerWheel * Float(vehicle.numberOfWheels)
217240
}
218241

219242
struct Bicycle: Vehicle {
@@ -282,3 +305,6 @@ _理由:_ オペレータは記号でできており、それらが型や値の
282305
#### 翻訳
283306

284307
* [中文版](https://github.com/Artwalk/swift-style-guide/blob/master/README_CN.md)
308+
* [日本語版](README_JP.md)
309+
* [한국어판](https://github.com/minsOne/swift-style-guide/blob/master/README_KR.md)
310+
* [Versión en Español](https://github.com/antoniosejas/swift-style-guide/blob/spanish/README-ES.md)

0 commit comments

Comments
 (0)
0