Skip to content

Rule Request: Avoid optional collections #1885

Closed
@andyjohns

Description

New Issue Checklist

Rule Request

  1. Why should this rule be added? Share links to existing discussion about what
    the community thinks about this.

Collections types, such as dictionaries, arrays, etc. should default to empty collections rather than optional collections. Using optional collections often means a double check of ensuring the collection is not nil, and checking the collection has members; while optional chaining can make this somewhat simple, it can still complicate the code. There's little point to a function returning an empty collection, rather than a nil collection, or defining a property as an optional collection rather than just providing it with a default empty collection.

Additionally, Apple strongly discourages the use of optional collections. This is a quote from Apple during an API review:

There's usually no meaningful distinction between an empty collection and "nothing", so it's somewhat rare to see collections as Optionals in Swift. If there's no particular reason to distinguish between [:] and nil, then the return value might as well drop the question mark.

  1. Provide several examples of what would and wouldn't trigger violations.

Would trigger:

var foo: [Int]?
var bar: [String: Int]?
var moreFoo: Set<String>?
func doFoo()->[]? {}
func doBar()->[String: String]? {}
func someFunc(input: [String: String]?) {}

Would not trigger:

// properties can default to empty
var foo: [Int] = []
var bar: [String: Int] = [:]
var moreFoo: Set<String> = []

// functions should return empty collections rather than nil
func doFoo()->[] {}
func doBar()->[String: String] {}

// input parameters can default to empty collections rather than optionals
func someFunc(input: [String: String] = [:]) {}

// Optional collections can still come from nested collections:
var complexDic: [String: [String: Int]] = [:]
let item = complexDic["bob"] 
// note: item is an optional collection here...
if let item = item {
 ....
}
  1. Should the rule be configurable, if so what parameters should be configurable?

It's not likely there are any valid configurable options

  1. Should the rule be opt-in or enabled by default? Why?
    See README.md for guidelines on when to mark a
    rule as opt-in.

I'd suggest the rule be enabled by default. To reference above, it's already rare to use optional collections in Swift, and there's usually no meaningful distinction between an empty collection and nil.

Metadata

Assignees

No one assigned

    Labels

    rule-requestRequests for a new rules.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions