-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rule Request: Prohibit subclassing #2079
Comments
I don't get it. You should call |
@marcelofabri If a class is not a subclass, then it does not need to call super. |
But this is the wrong assumption. You can have subclasses that don't call super (if they don't override any methods) but you can't have classes that call super and are not subclasses (the compiler doesn't let you do that). |
If a class is a subclass it will have
Although I agree, I do not understand how this statement is applicable to this rule. Again, if a class is not a subclass, then it will not have |
You don't need to override a constructor in a subclass. |
@marcelofabri If you introduce new properties or create a new constructor in a subclass, you will inevitably end up having a call to the superclass in one of the constructors. (You might not have the Also, see this. |
That's simply not true. You can have properties with default values. And you can have subclasses without adding new properties. This is common when using UIKit. |
@marcelofabri is correct. This compiles: class BaseClass {}
class Subclass : BaseClass {} So does this: class BaseClass {
var baseProperty: Any? = nil
init() {}
}
class IntermediateSubclass : BaseClass {
var addedProperty: Any
init(argument: Any) {
addedProperty = argument
}
}
class FinalSubclass : IntermediateSubclass {
var anotherAddedProperty: Any? = 0
} I do not really see the value in prohibiting subclassing. Yes, protocols are a better solution in many cases, but they cannot do everything a class cluster can. For example, you cannot store an array of Most Swift developers work with However, if you really want to discourage the subclassing paradigm, you need to target the bottom of the class tree instead. That means instead of punishing the client developer for using the tools he has been given, it would be better to discourage the vending developer from designing a subclass‐based API in the first place. To that end you could conceivably make an effective rule to prohibit defining a |
Please read this, this and this. I agree with you. I think there could be two rules. One that prohibits subclassing and another one that requires marking new classes as final. In the environment where certain classes of the libraries and frameworks are not final, the first rule encourages the creation of new classes and interfaces instead of creating subclasses. The second rule would encourage creating and designing new classes as final ones. |
You can always trust me to read something the first time. 😉 Those articles do a good job of highlighting some misuses of the subclassing paradigm, but there are other cases when it is the best (or only) tool in the box. Both articles are written about Java, and Swift is simply not the same; maybe that is a cause of confusion.
If you want to redesign UIViewController as a protocol, no one will stop you. I just do not want to recommend to every single iOS developer that they should go to that amount of work. Apple writes:
|
@SDGGiesbrecht By the way, here is an article on this topic in Swift. |
@driver733 I do agree that we should include even controversial rules as long as they add value to some Swift-based projects and improve the actual purpose of the Swift programming language. But this rule – if I understand it correctly – basically says: Swift has a feature that shouldn't be used and this rule makes sure you don't use it. In my opinion, while there should be rules that enforce different code styles even if they are controversial like you suggested in #2075 and #2077, I don't think we should add rules that are diametrical to Swift's own design. If anything at all, we should find cases where subclassing isn't the best solution and other solutions should be preferred. But putting subclassing under general suspicion and warning against it by default simply sounds like a rule not helping anybody. Linters shouldn't be used like this, this would lead to way too many false positives to be considered useful. |
Very well said @Dschee, you captured my own feelings about this better than I could have 😊. Closing. |
New Issue Checklist
Rule Request
Description
Read this for more info.
What would trigger?
What would not trigger?
Configurable?
The rule should not be configurable.
Opt-in or enabled?
This rule could be a personal preference or coding style from various individuals, so it could be kept as opt-in.
The text was updated successfully, but these errors were encountered: