Swift : use of self when you come from a long background of ObjectiveC :D

Hi guys,

I’ve been an iOS developper since iOS 3.0 (yes, there was dinosaurs back in those days :D) and I started Swift about 6 months after it was released (and I f***ing love it of course).

I don’t know for you guys, but I’m having the hardest time not using self. for every property and functions I call. I just wanted to have your opinion : what’s it like for you and is it really necessary not to use self. in swift though it is recommended by the Swift Algorithm Club ^^

Thanks for reading :slight_smile:

You can use ‘self.’ in your code if you want to. It’s not necessary to use it, or necessary to not use it. Being in the habit of using it makes it less likely that you’ll confuse a local variable and type property with the same name (Swift will assume you mean the local variable), but writing ‘self.’ everywhere can feel redundant and make the code harder to read.

If you’re contributing to the Swift Algorithm Club, use the group’s style guidelines to keep the project consistent.

The compiler allows us to have stylistic opinions, but adopting consistent–albeit which you can feel open to changing at any time, when you feel that you’ve learned better–practice, will help you not drain any cognitive resources when coding.

I recommend using playgrounds to document the standards you want to use, and sharing them on GitHub, for others to consider. Here’s mine, for self.