Swift allows optional prefixing of method calls and property references to the current object instance via self
. Removing these prefixes can declutter code but, depending on the length of the method and number of parameters and local variables, it can also make variable scope ambiguous.
Perhaps the answer is to refactor into smaller classes and methods, but sometimes that isn't always practical. Common examples are things like UIViewController
subclasses and custom viewDidLoad()
implementations or complex action handlers for UIGestureRecognizer
subclasses. I've thought about this a bit since diving into developing an iOS app in Swift but am still on the fence. My ideal answer(s) would touch on:
If not referencing
self
has an practical downsides.Whether mixing implicit and explicit references to
self
is worse that sticking to one or the other (I'm leaning towards yes).Overall stylistic concerns with a focus on readability.
Instance variable naming styles versus local variable naming styles to mitigate ambiguity.
References to similar issues in other languages.