SwiftLint
SwiftLint copied to clipboard
Empty Array Initialize
trafficstars
New rule request
array initialize using [] instead ()
Bad
var emptyArray = [Int]()
Good
var emptyArray: [Int] = []
- using [] is more common and preferred due to its readability and simplicity. It is a more straightforward way to indicate that you are initializing an empty array of a specific type.
- using () to initialize an empty array might lead to confusion, as it resembles tuple syntax, which may suggest that you are creating a tuple.
I think this is a matter of taste. Which style is "good" and which is "bad" is subjective.
For instance, people might like to omit types wherever possible, so they write let b = true, var i = 3, ... and in turn would also prefer let a = [Int]() to be consistent.
That said, this potential new rule should support both styles (by configuration).
If you compare the machine code of these, "Bad" is actually more efficient (and less typing) 😄