Why is “call super” considered an anti-wikipedia pattern?

Wikipedia classifies the “super call” as an anti-pattern, and I really don't understand why. The pattern is used quite often in objective-C / cocoa; e.g. init / dealloc, drawrect, awakefromnib all require you to call super. I do not understand the concept here?

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

+6
source share
2 answers

As stated in the article , it is necessary to call a super method that is antipattern, i.e. the superclass "expects" you to override this method to complete the functionality, but it does this without making this expectation explicit. And he also expects you to call a super implementation. Both programs are necessary for the program to work.

This is antipattern because the programmer’s goal cannot be inferred from code. If your colleagues decided to work on this, they would not know what you expected from the class, and therefore might run into problems and / or annoyances.

So, if you expect some parts of the method to be redefined, but other things should remain in place, it is recommended to use a template template where you save all things that should not be replaced by one (personal) method, which then calls another - completely separate - which must be implemented in order for the program to work (in some languages ​​it won’t even compile otherwise). This way you will make sure that important things remain where they should be, and whoever extends the class knows exactly what to do, remaining blissfully unaware of the other implementation details.

Objective-C has no abstract or virtual methods, but you can achieve the same effect by explicitly throwing an exception if the super method is called. Thus, if your colleagues forget to override the method, the program crashes - and it crashes with an error message that they understand, and this will allow them to quickly understand and fix the problem than some unstable behavior without explanation, due to lack of functionality.

+13
source

The essence of the problem is this:

. However, the fact that the language itself cannot ensure compliance with all the conditions prescribed for this challenge is an anti-model.

Thus, the authors are analogues about programmers who must do something that, in their opinion, the language should do for them.

The solution mentioned in the wikepedia Call Super article:

The best approach to solving these problems is to use the template method, in which the superclass includes a purely abstract method that must be performed by subclasses and have the method call this method

The problem is that Objective-c does not have virtual functions. It has only messages, so you cannot implement what wikepedia offers.

+3
source

All Articles