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
andyjohns opened this Issue Oct 5, 2017 · 4 comments

Comments

Projects
None yet
2 participants
@andyjohns

andyjohns commented Oct 5, 2017

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

This comment has been minimized.

Collaborator

marcelofabri commented Oct 5, 2017

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

@andyjohns

This comment has been minimized.

andyjohns commented Oct 5, 2017

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

This comment has been minimized.

Collaborator

marcelofabri commented Oct 5, 2017

No worries. Thanks for filling this!

@marcelofabri

This comment has been minimized.

Collaborator

marcelofabri commented Mar 27, 2018

Implemented by @ornithocoder in #2001 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment