11

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

eyebrowsoffire
  • 957
  • 7
  • 15
  • Could you provide the source? calling super is just a part of object oriented programming, I don't know why this should be an anti-pattern.. – IluTov Dec 21 '12 at 16:56
  • Link to the relevant wikipedia article ? (Remember Wikipedia is collaboratively edited, they _could_ be wrong). – driis Dec 21 '12 at 16:56
  • 2
    This is really sad that this question is mark as "not constructive". I found it very important – Jakub Aug 11 '15 at 14:33
  • If you get down to calling this an anti-pattern, you have to call a lot of other OO things anti-patterns as well - like overriding in general! In addition, template method only helps for first layer down of inheritance. Implementation two layers down must be enforced by consumer. Of course that much inheritance is an anit-pattern too, right? Bottom line: Fowler is a genius, and has tons of useful advice. But even a genius hits a _fowl_ ball over the backstop on occasion! – FastAl Sep 28 '15 at 20:25

2 Answers2

19

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.

weltraumpirat
  • 22,544
  • 5
  • 40
  • 54
  • Correct me if I'm wrong, but are there templates in Objective-C? –  Dec 21 '12 at 16:58
  • but that's not what OP is asking about. –  Dec 21 '12 at 17:00
  • He's asking why call super is an anti pattern, correct. I answered that, didn't I? – weltraumpirat Dec 21 '12 at 17:02
  • 1
    No, not at all. Not being able to call `super` (because it raises an exception) has nothing to do with this. If you want a response with C++ terminology: you answered a question about virtual methods and not templates. –  Dec 21 '12 at 17:05
  • Raising the exception does the same thing, only it does so at runtime instead of at compile time - you will notice that you've called a method that is supposed to be overridden. – weltraumpirat Dec 21 '12 at 17:06
  • 1
    @H2CO3 btw I'm talking about the pattern called "template method", not C++ templates, and this is about the pattern called "call super", not just calling the super class - those are different things. – weltraumpirat Dec 21 '12 at 17:31
  • +1 Great answer. [This](http://journal.stuffwithstuff.com/2012/12/19/the-impoliteness-of-overriding-methods/) seems relevant. – Jordão Dec 21 '12 at 21:09
4

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.

Peter M
  • 7,309
  • 3
  • 50
  • 91
  • 1
    So why is a template pattern considered better than calling super exactly? – eyebrowsoffire Dec 21 '12 at 17:02
  • 1
    @While Peter is correct in saying there are no virtual functions it is easy to produce similar semantics (e.g. see answer http://stackoverflow.com/questions/13793864/abstract-class-for-objective-c/13797335#13797335). So if you wish you can implement the template pattern. – CRD Dec 21 '12 at 17:18