Unnecessary Self Refences In Your Code?

Get Rid Of Them?

Steven Curtis
3 min readJun 13, 2022
Photo by Nijwam Swargiary on Unsplash

Self self, you’re all about your self.. Is it that important to mark properties class or struct instances with self?

Can you mark too many properties class or struct instances as self?

Where you should use self

I’ve already written an article describing the difference between self and Self: https://stevenpcurtis.medium.com/self-or-self-in-swift-code-b0d2199ec2ef.

For the purposes of this article, we can think of self applying to properties (it’s easier to show!).

The issue? I keep leaving references to self everywhere that I shouldn’t be!

Let us take a look at an example in hand.

final class ViewController: UIViewController {    let viewModel: ViewModel    init(viewModel: ViewModel) {
self.viewModel = viewModel
}
...
}

self is required here to differentiate between the viewModel property passed in the…

--

--