does your design smell - tushar sharma
TRANSCRIPT
Does Your Design Smell?
Tushar Sharma
http://www.tusharma.in
Cities grow, cities evolve, (...), each cityhas to be renewed in order to meet theneeds of its populace. From time to timecities are intentionally refactored (...);most times cities are refactored in bits(...), but sometimes the literal andfigurative debt is so great a once greatcity falls into despair (...).
Grady Booch
Missing road
Unrestricted access
Hub-like road
Footpath is not a road
Deadlock!!
Multitasking
Useless… unnecessary
Complex
Beyond capacity
Why?
Every engineering discipline has to deal withnon-optimal and sub-optimal solutions.
Such solutions violate fundamentalprinciples of the discipline.
Smells
Design smells
Design smells are certain structures in the design thatindicate violation of fundamental design principles and
negatively impact design quality.
Design smells
Design smells are certain structures in the design thatindicate violation of fundamental design principles and
negatively impact design quality.
Missing road
This smell arises when clumps ofdata or encoded strings are usedinstead of creating a class or an
interface.
Missing abstraction
public class Throwable {public void printStackTrace(); //other methods elided.
}
(Java 1)
Throwable class in JDK
public class Throwable {public void printStackTrace();public StackTraceElement[] getStackTrace();//other methods elided.
}
public final class StackTraceElement {public String getFileName();public int getLineNumber();public String getClassName();public String getMethodName();public boolean isNativeMethod();
}
Refactored in Java 1.4
(Java 1.4)
Unrestricted access
This smell occurs when the declared accessibilityof one or more members of an abstraction ismore permissive than actually required.
Deficient Encapsulation
In JDK7• 508 classes - at least one
public field• 96 classes - at least five
public fields
Hub-like road
This smell arises when an abstraction has dependencies(both incoming and outgoing) with a large number ofother abstractions.
Hub-likeModularization
Component class in JDK
Footpath is not a road
This smell arises when a supertype and its subtypeconceptually do not share an “IS-A” relationshipresulting in broken substitutability.
Broken Hierarchy
Stack is not a Vector
This smell arises when two or more abstractionsdepend on each other directly or indirectly.
Cyclically-dependentModularization
Deadlock!! Cycle!!
This smell arises when an abstraction has more thanone responsibility assigned to it.
Multifaceted Abstraction
Multitasking
Image class• load• process• render• store
Example
multiplereasons tochange
granularity of a responsibility
This smell occurs when an abstraction that is actuallynot needed (and thus could have been avoided) getsintroduced in a software design.
Unnecessary Abstraction
Useless… unnecessary
public interface WindowConstants {/**The do-nothing default window close operation*/public static final int DO_NOTHING_ON_CLOSE = 0;/**The hide-window default window close operation*/public static final int HIDE_ON_CLOSE = 1;/**The dispose-window default window close operation*/public static final int DISPOSE_ON_CLOSE = 2;/**The exit application default window close operation.*/public static final int EXIT_ON_CLOSE = 3;
}
Example
In javax.swing.WindowConstants
InsufficientModularization
Beyond capacity!!Complex!!
This smell arises when an abstraction exists that has notbeen completely decomposed, and a furtherdecomposition could reduce its size, implementationcomplexity, or both.
InsufficientModularization
Examples• SocketPermission
getMask method – CC=81• Component
259 public methods
Tools to detect design smells
Infusion
Tools to detect design smellsDesignite
http://www.designite-tools.com
Tools to detect design smells
Designite
http://www.designite-tools.com
Tools to detect design smells
Designite
http://www.designite-tools.com
Tools to detect design smells
Designite
http://www.designite-tools.com
Tools to detect design smells
Designite
http://www.designite-tools.com
Themessage
Every engineering discipline has to deal withnon-optimal and sub-optimal solutions.
Software engineering is not an exception;in fact, it is more prone to such smells.
Design smells – know them, avoidthem, and refactor them!
References
“Refactoring for software designsmells: Managing technical debt”By Girish Suryanarayana, Ganesh SG,and Tushar Sharma.Morgan Kaufmann. ISBN 978-0128013977
http://www.designsmells.com
Tushar Sharma@[email protected]://www.tusharma.in