Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "call super" considered an anti-pattern according to Wikipedia? [closed]

Wikipedia classifies "call super" as an anti-pattern, and I don't really understand why. The pattern is used pretty frequently in objective-C/cocoa; for example init/dealloc, drawrect, awakefromnib all require you to call super. Am I misunderstanding the concept here?

Link to the article: http://en.wikipedia.org/wiki/Call_super

like image 665
eyebrowsoffire Avatar asked Dec 21 '12 16:12

eyebrowsoffire


People also ask

What does call the super mean?

Call super is a design pattern in which a particular class stipulates that in a derived subclass, the user is required to override a method and call back the overridden function itself at a particular point.

What is a super class call?

super is a keyword. It is used inside a sub-class method definition to call a method defined in the super class. Private methods of the super-class cannot be called. Only public and protected methods can be called by the super keyword. It is also used by class constructors to invoke constructors of its parent class.


2 Answers

As it says in the article, it is the necessity to call the super method that is the antipattern, i.e. the super class "expects" you to override the method to make the functionality complete - but it does so without making this expectation explicit. And it also expects you to call the super implementation. Both are required for the program to work.

This is an antipattern, because the intent of the programmer cannot be deduced from the code. If your co-workers decided to work on it, they wouldn't know what you expected the class to do, and are therefore likely to encounter problems and/or irritations.

So if you expect some parts of a method to be overridden, but other things need to stay in place, it is recommended to use the template method pattern, where you keep all the things that must not be replaced in one (private) method, which then calls another one - completely separate -, which must be implemented in order for the program to work (in some languages, it won't even compile otherwise). That way, you make sure the important things remain where they have to be, and whoever extends the class will know exactly what to do, while remaining blissfully ignorant of the other implementation details.

Objective-C does not have abstract or virtual methods, but you can achieve the same effect by explicitly raising an exception if the super method is called. That way, if your co-workers forget to override the method, the program will crash - and it will crash with an error message that they will understand, and that will enable them to understand and fix the problem faster and more easily than some erratic behavior with no explanation, due to the functionality not being complete.

like image 173
weltraumpirat Avatar answered Oct 22 '22 20:10

weltraumpirat


The gist of the problem is:

..However, the fact that the language itself may not be able to enforce all conditions prescribed on this call is what makes this an anti-pattern.

So the authors are anal about programmers having to do something that they believe the language should do for them.

The solution mentioned in the wikepedia article Call Super is:

A better approach to solve these issues is instead to use the template method pattern, where the superclass includes a purely abstract method that must be implemented by the subclasses and have the original method call that method

The problem is that Objective-c doesn't have virtual functions. It only has messages, so you can't implement what wikepedia suggests.

like image 29
Peter M Avatar answered Oct 22 '22 19:10

Peter M