Bilue / swift-style-guide

Style guide & coding conventions for Swift projects
Creative Commons Zero v1.0 Universal
14 stars 3 forks source link

Referencing Self #11

Open sboddeus opened 8 years ago

sboddeus commented 8 years ago

In regards to only referencing self when explicitly required to do so:

https://github.com/apple/swift-evolution/blob/master/proposals/0009-require-self-for-accessing-instance-members.md

I agree with a couple of the community points and would like to reconsider that we actually require the use of self in instance and class methods to make scope explicitly clear.

tizaks commented 8 years ago

I'm definitely agreed on this one. Even if the proposal isn't accepted, I think for clarity, especially with multiple people working on projects, it's worth mandating it in our style guide.

Tchelow commented 8 years ago

As I was saying the other day, it's a bit confusing having self just inside blocks. Making it being used in class methods would make it much clearer.

sboddeus commented 8 years ago

Pro as I see it:

Con as I see it:

rhysforyou commented 8 years ago

I'm going to say I'm against this proposal. For one thing, you can already distinguish instance variables visually since they're highlighted in a different colour, and failing that you can always ⌥-click.

In my mind, adding the explicit self increases the verbosity of the code without adding meaning or clarity.

rhysforyou commented 8 years ago

Some follow up on this: after some long, drawn out discussion on the matter, the swift core team rejected a proposal to make self mandatory for accessing instance properties. This doesn't necessarily dictate our decision, but it's worth reading their response to the proposal: https://lists.swift.org/pipermail/swift-evolution-announce/2016-January/000009.html

cuteiosdev commented 8 years ago

Yup, it is now correctly a decision for the styleguide rather than the language to dictate.

So with the main focus being upon style and clarity, we should ask what gets clarified and what gets less clear if we introduce mandatory self in the styleguide.

From the discussions had, I see (though can be proven wrong) that the main issue wanting to be made clearer is when you have a class variable the same name as a variable defined within a function of that class. This is a good thing to try and clear up, though am not convinced that explicitly specifying self is the best approach to solving this problem.

A different approach (though here for illustration rather than being my preferred solution as it has its own benefits and shortcomings) would be to identify class variables with a naming convention. In the same way, variables defined within a function or passed in as parameters could be defined by a convention as well.

Ultimately we need to be careful about how we name things and we should use the style guide to encourage good and appropriate naming.

Lets keep the discussion going.