Search code examples
iosswiftfinal

Swift. Make all classes final?


My question is about using final keyword im Swift code. I know that final helps compiler to compile code faster because of dynamic dispatch. So, if I definitely know that I will not inherit some of my classes, should I make all of them final?


Solution

  • There was this protective approach taught by the iOS Stanford course.

    The approach was, define all your APIs private. It increases encapsulation. Later if you needed to give away something then remove the privacy.

    Because it's bad to do it the other way around ie design something public and then later change it to private.

    Similarly here, I think making a class final and then later deciding it shouldn't be final is better than making a class non-final, allowing multiple classes to subclass it and then later attempt to revert it back to final because of some design decisions.