Swift-style-guide: 初始化空数组和字典不一致

创建于 2017-04-07  ·  4评论  ·  资料来源: raywenderlich/swift-style-guide

我不得不说,我同意指南中的大部分内容,但这一点似乎不一致:

https://github.com/raywenderlich/swift-style-guide#type -annotation-for-empty-arrays-and-dictionaries

该指南指出:

// preferred
var names: [String] = []
var lookup: [String: Int] = [:]

优先于

// not preferred
var names = [String]()
var lookup = [String: Int]()

但是为了紧凑代码,该指南通常更喜欢推断类型:

// preferred
let message = "Click the button"
let currentBounds = computeViewBounds()

```迅速
// 不是首选
让消息:String = "点击按钮"
让 currentBounds: CGRect = computeViewBounds()

If we were being consistent, shouldn't we always prefer inferred types over explicit declaration?  I would think this is more consistent
```swift
// should be preferred
var names = [String]();
var lookup = [String: Int]();

最有用的评论

在我看来,指南在这里是正确的。 它只是说,在需要时,指定类型。 没有时,放下它。

好吧,但这真的不是一种风格选择吗? 不,不是真的。 让我们看一些例子:

// preferred
var colors: [UIColor]?
var colors: [UIColor] = []
var colors: [UIColor] = [.red, .green, .blue]

现在,让我们将这 3 个示例转换为您建议的偏好。

var colors: [UIColor]?
var colors = [UIColor]()
var colors = [UIColor.red, UIColor.green, UIColor.blue]

看看那里发生了什么? 对于最后一行,我们现在需要为数组中的每个元素重复 UIColor。 您可能还会争辩说,这些声明现在彼此之间不太一致。

那么方法呢? 好吧,您当然可以选择始终指定类型以保持一致。 但是,我想大多数开发人员都希望消除冗余代码。

// do we need to specify type?
var colors: [UIColor] = rainbowColors()
// no we don't
var colors = rainbowColors()

那有意义吗?

_Edit:平心而论,您也可以推断出这样的数组,但考虑到我们正在谈论一致性,我正在考虑将其排除在外。 只是觉得我是对的。_

// preferred
var colors: [UIColor] = [.red, .green, .blue]
// not preferred
var colors = [.red, UIColor.green, .blue]

所有4条评论

在我看来,指南在这里是正确的。 它只是说,在需要时,指定类型。 没有时,放下它。

好吧,但这真的不是一种风格选择吗? 不,不是真的。 让我们看一些例子:

// preferred
var colors: [UIColor]?
var colors: [UIColor] = []
var colors: [UIColor] = [.red, .green, .blue]

现在,让我们将这 3 个示例转换为您建议的偏好。

var colors: [UIColor]?
var colors = [UIColor]()
var colors = [UIColor.red, UIColor.green, UIColor.blue]

看看那里发生了什么? 对于最后一行,我们现在需要为数组中的每个元素重复 UIColor。 您可能还会争辩说,这些声明现在彼此之间不太一致。

那么方法呢? 好吧,您当然可以选择始终指定类型以保持一致。 但是,我想大多数开发人员都希望消除冗余代码。

// do we need to specify type?
var colors: [UIColor] = rainbowColors()
// no we don't
var colors = rainbowColors()

那有意义吗?

_Edit:平心而论,您也可以推断出这样的数组,但考虑到我们正在谈论一致性,我正在考虑将其排除在外。 只是觉得我是对的。_

// preferred
var colors: [UIColor] = [.red, .green, .blue]
// not preferred
var colors = [.red, UIColor.green, .blue]

我会和

var colors: [UIColor]?
var colors = [UIColor]()
var colors: [UIColor] = [.red, .green, .blue]

而不是你上面所做的。 除非需要显式声明,否则我们是否不喜欢使用推断类型? 从这个意义上说,第三行似乎是一致的。

你是对的,我可能过度简化了规则。 话虽如此,这两种方法都没有错,这只是取决于偏好。 就我而言,在来回空/非空状态时,我发现以下内容更一致、更易读且更实用。 我认为我们应该让其他人加入进来。

// preferred
var colors: [UIColor]?
var colors: [UIColor] = []
var colors: [UIColor] = [.red]
// not preferred (in my opinion)
var colors: [UIColor]?
var colors = [UIColor]()
var colors: [UIColor] = [.red]

您遇到的问题是这种不一致:

// inconsistent?
var colors: [UIColor] = []
var colors = rainbowColors()
// your preference
var colors = [UIColor]()
var colors = rainbowColors()

@RobertGummesson ,您只考虑了最常与数组/字典文字语法一起使用的类型。 如果我们只有数组来填充数组文字,那么两种解决方案都可以。 我什至曾经使用你的建议。 但是后来我们得到了集合,我不得不标准化一个包含它们的约定。 您建议的那个比显式键入更丑陋。

var ints: Set = [1, 1, 2, 2]
var ints = Set([1, 1, 2, 2])
var ints = Set(arrayLiteral: 1, 1, 2, 2)

因此,在确定了对非空集更好的首选之后,空版本和匹配的数组约定可以反向形成。

var ints: Set<Int> = []
var ints = Set<Int>() // instead of this, which, without the above, seems fine.

var ints = [1, 1, 2, 2]
var ints: [Int] = []
此页面是否有帮助?
0 / 5 - 0 等级

相关问题

xezun picture xezun  ·  6评论

Lweek picture Lweek  ·  5评论

luki picture luki  ·  3评论

gokselkoksal picture gokselkoksal  ·  9评论

rwenderlich picture rwenderlich  ·  29评论