Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule Request: Avoid optional collections #1885

Closed
2 tasks done
andyjohns opened this issue Oct 5, 2017 · 4 comments
Closed
2 tasks done

Rule Request: Avoid optional collections #1885

andyjohns opened this issue Oct 5, 2017 · 4 comments
Labels
rule-request Requests for a new rules.

Comments

@andyjohns
Copy link

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.

@marcelofabri marcelofabri added the rule-request Requests for a new rules. label Oct 5, 2017
@marcelofabri
Copy link
Collaborator

@andyjohns Just for completeness, could you please add a link to the quote source?

@andyjohns
Copy link
Author

Unfortunately the quote is not available in a public link. My company has been working with Apple, and this came up during a review of some the API we're working with them on. I can remove it from the request if that's a problem.... thx

@marcelofabri
Copy link
Collaborator

No worries. Thanks for filling this!

@marcelofabri
Copy link
Collaborator

Implemented by @ornithocoder in #2001 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule-request Requests for a new rules.
Projects
None yet
Development

No branches or pull requests

2 participants