identification of refused bequest code smells e. ligu, a. chatzigeorgiou, t. chaikalis, n....
TRANSCRIPT
Identification of Refused Bequest Code Smells
E. Ligu, A. Chatzigeorgiou, T. Chaikalis, N. Ygeionomakis
Department of Applied Informatics
University of Macedonia, Greece
ICSM’2013, Early Research Achievements Track, September 24, 2013
oC
0
10
20
30
40
Abstract Superclass / Interface
- Overriding- Failures
- Some overriding
- No failures
- No overriding- Some failures
- invocation of super
- No overriding- Some failures
- No invocation of super
- No overriding- No failures
Ref
used
Beq
uest
Indi
cato
r
Context
In the world of OO systems, inheritance is not a panaceaMammal
+ breatheAir()+ regulateBodyTemp()+ giveBirthToLiveYoung()+ produceMilkIfFemale()+ swim()
+ breatheAir()+ regulateBodyTemp()+ giveBirthToLiveYoung()+ produceMilkIfFemale()+ swim()
+ breatheAir()+ regulateBodyTemp()+ giveBirthToLiveYoung()+ produceMilkIfFemale()+ swim()
Problem
Goal: Identification of Refused Bequest Code Smells
Refused Bequest: “a subclass does not want to support the interface inherited from its parent class” [Fowler]
non-trivial problem to resolve
Appropriate Refactoring: Replace Inheritance with Delegation
Famous quote: “Favor Composition over Inheritance” [GoF]
Key Concept
public interface which is inherited
is the subclass using the inherited interface?
Alpha
+ m1()+ m2()+ m3()
Beta
+ m4()+ m5()
+ m1()+ m2()+ m3()
OK
indication of Refused Bequest
Smell Thermometer
Refused Bequest is highly improbable: -inherited methods have been re-implemented to provide functionality that is specific to the subclass -> goal is to enable polymorphism. -the presence of errors -> inherited functionality is actually employed.
Signs of Refused Bequest : -no superclass method is overridden-no inherited method is invoked on subclass instances-no super class method invocationsNo argument in favor of inheritance
oC
0
10
20
30
40
Abstract Superclass / Interface
- Overriding- Failures
- Some overriding- No failures
- No overriding- Some failures- invocation of super
- No overriding- Some failures- No invocation of super
- No overriding- No failures
Re
fuse
d B
eque
st In
dica
tor
oC
0
10
20
30
40
Abstract Superclass / Interface
- Overriding- Failures
- Some overriding- No failures
- No overriding- Some failures- invocation of super
- No overriding- Some failures- No invocation of super
- No overriding- No failures
Re
fuse
d B
eque
st In
dica
tor
Example
SweetHome 3D v.4.0LOC: 76K, 460 classes, 69 hierarchies, 42 test cases
URLContent
5 public methods
TemporaryURLContent HomeURLContent ResourceURLContent
No overriden methodsNo new method
0 clients invokinginherited methodson instances of the subclass
No overriden methodsNo new method
1 Error
No overriden methods1 new method
33 Errors
Tool
Implementation on top of the JDeodorant tool
http://java.uom.gr/ref_bequest/
Student# name : String- grades : Map<String, Double>
+ calculateGPA() : double+ printRecord()
UnderGrad
+ printRecord()
Client invoking calculateGPA()
- Overriding- Failures
PostGrad
+ printRecord()
Client NOT invoking
calculateGPA()
- Overriding- No Failures
ForeignStudent
+ printTranscript()
Client invoking calculateGPA()
- No Overriding- Failures- invocation of super
super.printRecord()
Doctoral
Client invoking calculateGPA()
- No Overriding- Failures- No invocation of super
PostDoc
+ printFundingBody()
Client NOT invoking
calculateGPA()
- No Overriding- No Failures
+ printFundingBody()
Threat to Validity
Assumption: unit tests exercise thoroughly system functionality
Threat to construct validity: introduced errors might not lead to test failures
because tests have not been designed to cause the invocation of the corresponding methods
and not because the methods are not actually utilized.
Mitigation: perform identification on projects with extensive test coverage
Question 1 (academic): Would you trust an automated tool to identify and resolve design defects on its own?
Question 2 (provocative): Do you think that industrial engineers actually regard the resolution of smells as a (serious) way to improve the code?
ICSM’2013, Early Research Achievements Track, September 24, 2013