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

Reconsidering empty array and dictionary initialization #143

Closed
rayfix opened this issue Apr 7, 2016 · 3 comments
Closed

Reconsidering empty array and dictionary initialization #143

rayfix opened this issue Apr 7, 2016 · 3 comments
Assignees

Comments

@rayfix
Copy link
Contributor

rayfix commented Apr 7, 2016

The current style guide says that arrays and dictionaries be initialized in the following way.

var names = [String]()
var lookup = [String: Int]()

I would like to push back on this in favor of:

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

Here is my rationale:

  • [Type]() looks foreign, especially for a newcomer. To me, : [Type] = [] simply reads better in many cases.
  • When I pass an empty array (or OptionSetType) as an argument where type inference is available, I am always using [], not a default initialized object. There is some consistency here.
  • For short, single line arrays, the type can be often be inferred. This is good and should be utilized. However, for arrays that span multiple lines, the type inference can become costly. Consider:
var stuff = [1, 2, 3, 4, 5,  // ... 100 more numbers
                  106.7, 107, "haha", nil, 108, 109]

The type checker needs to scan all of the elements to properly type infer stuff. Even when there are no tricks ("haha"), I have seen this bog down the editor. (Hello rainbow.) Making the type checker work for you to double check your work seems like a good idea.

What do you think?

@jawwad
Copy link
Contributor

jawwad commented Apr 7, 2016

@rayfix I've grown to prefer your suggested format i.e. var names: [String] = []

My rational is that this approach looks more consistent with the approach you would take when you want to define an array with a let constant. (You would not initialize it to an empty array since you would not be able to change it later)

For example in the case of a class:

class SimpleClass {
    let array: [String]

    init(array: [String]) {
        self.array = array
    }    
}

Or when defining a constant array whose value depends on a condition.

let array: [String]
if condition {
    array = array1
} else {
    array = array2
}

@svanimpe
Copy link

svanimpe commented Apr 7, 2016

Big +1 on this!

I chose the var array: [Type] = [] notation for my courses because it allows me to teach arrays (and dictionaries) fairly early on in my courses. My teaching approach is to start from scratch, add only one topic at a time and never leave things unexplained. This notation allows me to do that because it doesn't involve any topics the students aren't yet familiar with at the point, such as initializers or generics.

@rayfix rayfix added this to the Update April 2016 milestone Apr 7, 2016
@rayfix rayfix self-assigned this Apr 7, 2016
@gemmakbarlow
Copy link
Contributor

Love it. I use your suggested syntax in my own codebases; definition and type of the var on one side of the equation, value on the other. Nice and simple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants