ottijp blog

raywenderlich.com 公式Swiftスタイルガイドを読みました

Swiftのスタイルガイドとしてよく挙げられるraywenderich.comの公式スタイルガイドを読んで,参考になったところを翻訳してみました.

raywenderlich/swift-style-guide

このスタイルガイドはMITライセンスです.

Copyright (c) 2016-2017 Razeware LLC
https://github.com/raywenderlich/swift-style-guide/blob/master/LICENSE.txt

Use classes for things that do have an identity or a specific life cycle. You would model a person as a class because two person objects are two different things. Just because two people have the same name and birthdate, doesn’t mean they are the same person. But the person’s birthdate would be a struct because a date of 3 March 1950 is the same as any other date object for 3 March 1950. The date itself doesn’t have an

identity.

同一性や特定のライフサイクルを持つものについてはクラスを使ってください.例えば2人の人間は2つの異なったものなので,人間はクラスとしてモデル化します.もし2人が同じ名前と生年月日だとしても,2人が同じ人間というわけではないからです.それとは異なり,1950年3月3日という日は他の1950年3月3日という日と同じオブジェクトであると言えるので,構造体としてモデルするのがよいでしょう.

For conciseness, avoid using self since Swift does not require it to access an object’s properties or invoke its methods. Use self only when required by the compiler (in @escaping closures, or in initializers to disambiguate properties from arguments). In other words, if it compiles without self then omit it.

簡潔な表現のために,selfを使うのは避けましょう.Swiftはオブジェクトのプロパティやメソッドの呼び出しのためにselfを必要としません. コンパイラエラーが発生する場合のみselfを使うようにしましょう(@escapingクロージャやイニシャライザにおいて,引数とプロパティが曖昧な場合にそうなります.)言い方を変えると,selfを付けず にコンパイラエラーが出なければ,selfを省略してください.

Unused (dead) code, including Xcode template code and placeholder comments should be removed. An exception is when your tutorial or book instructs the user to use the commented

code.

Xcodeが生成するテンプレートコードやプレースホルダコメントを含む使用していない(不要な)コードは削除してください.チュートリアルや本においてユーザにコメントアウトされたコードを使わせる場合は除きます.

For functions with long signatures, add line breaks at appropriate points and add an extra indent on subsequent lines:

func reticulateSplines(spline: [Double], adjustmentFactor: Double,
    translateConstant: Int, comment: String) -> Bool {
  // reticulate code goes here
}

長いシグネチャの関数は適切な長さで改行し,後続の行は余分にインデントを追加してください.

Use trailing closure syntax only if there’s a single closure expression parameter at the end of the argument list. Give the closure parameters descriptive

names.

引数の最後に1つだけクロージャパラメータがある場合のみ,パラメータ名を省略してください.それ以外の場合はパラメータ名を記述するようにしてください.

Note: The advantage of using a case-less enumeration is that it can’t accidentally be instantiated and works as a pure namespace.

注意: case無しの列挙型は間違ってインスタンス化される心配がないため,純粋なネームスペースとして働きます.

このスタイルガイド,こちらで日本語翻訳されている方がいるんですが,Swift3版へのアップデートに追随していないみたいなので,翻訳してみようかな・・・.

GitHubのスタイルガイドなど,他のものも読んでみようと思います.


ottijp
都内でアプリケーションエンジニアをしています
© 2024, ottijp