Http:/pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE 517 Fall 2010/ch7 7f cc

From Expertiza_Wiki
Jump to navigation Jump to search

Call Super anti-pattern


Definition

Call super anti pattern requires a user to override a method in the superclass and call the overridden method from the subclass. An anti-pattern is a design pattern that is a re-occurring non-solution to a problem.

Introduction

Anti-patterns are Design Patterns that appear to be effective initially, but prove to be impractical and lead to serious consequences than the short-term benefits. Briefly, anti-patterns are problems that occur frequently and should be avoided. Call super[1] is type of Object Oriented Programming anti pattern or code smell. A Code Smell is a symptom in the source code that indicates the presence of a much larger problem.

Description

Call super is an anti-pattern of object oriented programming. The call super anti-pattern comes to the fore when the super class method is intentionally overridden, where a super class method is incomplete or limited in scope and the user intends to implement additional functionality in the sub class method to augment the existing super class method. But, the actual problem arises as the programming language cannot enforce all conditions prescribed by this call. So this is an anti-pattern though this pattern seems a good solution to many problems. Object-oriented programming allows inheritance to allow code reuse and avoid duplication of code. So the subclasses can inherit all properties of a superclass. The user while implementing the subclass has the choice of overriding all methods of the superclass and can have own implementations of the overridden methods. Or else he can override some methods of the super class and use the same functionality as the superclass offers some methods. In the latter case the super class methods need to be called to have the same implementation. Therefore most language implementations permit this. The “call super” anti-pattern comes into force when a superclass method is overridden and overriding method requires calling the overridden method from the overriding method. This is required in cases where the super class method performs the initial steps or the subclass only augments the superclass functionality. So the anti-pattern comes into force only when the parent class is called from the child class overriding method.

Here the superclass is inherited to the subclass and the subclass overrides the superclass method and still calls the superclass method from the subclass. Example:

A class student is taken and a sub-class gradstudent is taken. The subclass gradstudent extends the student class. So all methods of superclass are inherited to the subclass.

 public class student
 {
 public void getDetails (String e)
 {
 //perform necessary logic
 }
 } 
 public class gradstudent extends student
 {
 public void getDetails(String e)
 {
 Super.getDetails(e);
 //perform additional functioanlity
 }
 }

Here the subclass, after the inheritance overrides the superclass method. The subclass in addition to implementing the functionality of the overridden method, adds some functionality to the existing one. This is a case where call super anti pattern can’t be avoided, but this way of implementation produces an anti-pattern.

Avoiding call super Anti-Pattern

It is inevitable in object-oriented programming to avoid Inheritance. There is an alternative implementation to avoid the call super anti-pattern.

A Template method pattern is implemented so as to avoid the call super anti-pattern without coming into force. The template pattern can be implemented by creating a purely abstract method in the super class and have the subclass implement the method. This allows the subclass to call the superclass methods after overriding and still avoid the call super anti-pattern. Here the template method is created.

For e.g.

Here the method is a pure abstract method and the method1 is a subclass method. So we can see that the call super anti-pattern can be avoided by applying template method pattern. Considering the above example where call super anti-pattern is in force we can modify it to avoid the anti-pattern by making the calling method a template method. Then the abstract method is called by the subclass. The superclass creates a public method that is not overridden and then a separate method for the subclass to override. This is the introduction of a template method.

 public class student
 {
 public void Details (String s)
 {
 //perform necessary logic 
 }
 protected void getDetails(String s)
 {
 }
 }
  
 public class gradstudent extends student
 {
 public void getDetails(String s)
 {
 //perform additional functioanlity
 }
 public void perform(String s)
 {
 super.Details(s);
 //add functionality in a different subclass method
 }
 }

Here a separate public method is provided by the superclass and another method is defined to override. The method defined by the superclass to override is called a “hook”. The hook method can be defined in various ways which avoids the call super anti-pattern to come into force. In the above example we have given an empty implementation of the hook method. This way of implementing a template method is used when many subclasses need not implement the same functionality of a superclass. If many subclasses need to implement the same functionality of a superclass, then a default implementation of the template method can be added with the necessary implementation. If every subclass needs to provide unique behavior, then we can make the hook method abstract.

The problem with the implementation of a hook method is there is no particular way to show that the method is a hook. This doesn't pose a problem when there is only one hook method. But there may exist multiple hook methods depending on the control the subclass needs to take. The subclass may want to override the smaller hooks to implement some other functionality or override the lager scoped methods to implement a much different functionality. this depends on the subclass method. So one way to determine the hook is to carefully examine the functionality of the subclass methods.

In some languages you can seal the superclass method to avoid overriding by the subclass. In Java, superclass method is sealed using a final keyword, in C# sealed keyword is used. C# and C++ consider the methods to be sealed by default[2].

How to Detect a Call Super Anti-Pattern

Usually anti-patterns are difficult to detect as they have no precise UML diagram descriptions. They are usually textual descriptions. A Call Super anti-pattern can be identified by seeing what the subclass does. There are metrics to detect the presence of anti-patterns, but the algorithm becomes complex on introduction of dynamic aspects of the program. There are some implementations in PROLOG [3](e.g.JTransformer-a plugin in eclipse) to identify the call super anti pattern in Java using certain procedures that scan the classes and identify the presence of call super anti pettern based on some conditions. The call super anti pattern is detected by scanning the classes and methods and verifying the overriding and superclass calls.

References

[1] http://en.wikipedia.org/wiki/Call_super

[2] http://www.worldlingo.com/ma/enwiki/en/Anti-pattern

[3] http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=05491288

[4] http://www.compwisdom.com/topics/Anti-patterns

[5] http://coding.derkeiler.com/Archive/Java/comp.lang.java.programmer/2008-05/msg00445.html

[6] http://beust.com/weblog2/archives/000252.html

[7] http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/