[swift-evolution] Proposal: Re-instate mandatory self for accessing instance properties and functions

Andrey Tarantsov andrey at tarantsov.com
Mon Dec 14 13:17:08 CST 2015


I agree with Ilya here:

> > but they make code editing easier at the expense of readability. 
> Disagree. This really depends on the example. E.g. which is more readable:
> 
>     var length: Double { return sqrt(dx*dx + dy*dy) }
> 
>     var length: Double { return sqrt(@dx*@dx + @dy*@dy) }
> 
>     var length: Double { return sqrt(self.dx*self.dx + self.dy*self.dy) }

I find that this holds true in larger classes as well (or even more). I have tons of view controllers and views that do stuff like:

obj1.prop1 = X
obj1.prop2 = Y
...
obj1.prop10 = Z
superview1.addSubview(obj1)

Adding “self.” everywhere adds a significant amount of visual noise; I've tried _property and self.property access styles in Objective-C, and find the former one to be noticeably more readable.

To be fair, there are cases when it's not immediately clear if something is a property or a variable. That's why you can still add an explicit self. if you want. Some of those are also probably code smells.

A.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151215/6606d330/attachment.html>


More information about the swift-evolution mailing list