Does Your Design Smell?
Tushar Sharma
http://www.tusharma.in
Cities grow, cities evolve, (...), each city
has to be renewed in order to meet the
needs of its populace. From time to time
cities are intentionally refactored (...);
most times cities are refactored in bits
(...), but sometimes the literal and
figurative debt is so great a once great
city 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 with
non-optimal and sub-optimal solutions.
Such solutions violate fundamental
principles of the discipline.
Smells
Design smells
Design smells are certain structures in the design that
indicate violation of fundamental design principles and
negatively impact design quality.
Design smells
Design smells are certain structures in the design that
indicate violation of fundamental design principles and
negatively impact design quality.
Missing road
This smell arises when clumps of
data or encoded strings are used
instead 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 accessibility
of one or more members of an abstraction is
more 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 of
other abstractions.
Hub-like Modularization
Component class in JDK
Footpath is not a road
This smell arises when a supertype and its subtype
conceptually do not share an “IS-A” relationship
resulting in broken substitutability.
Broken Hierarchy
Stack is not a Vector
This smell arises when two or more abstractions
depend on each other directly or indirectly.
Cyclically-dependent
Modularization
Deadlock!! Cycle!!
This smell arises when an abstraction has more than
one responsibility assigned to it.
Multifaceted Abstraction
Multitasking
Image class
• load
• process
• render
• store
Example
multiple
reasons to
change
granularity of a responsibility
This smell occurs when an abstraction that is actually
not needed (and thus could have been avoided) gets
introduced 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
Insufficient Modularization
Beyond capacity!!Complex!!
This smell arises when an abstraction exists that has not
been completely decomposed, and a further
decomposition could reduce its size, implementation
complexity, or both.
Insufficient Modularization
Examples
• SocketPermission
getMask method – CC=81
• Component
259 public methods
Tools to detect design smells
Infusion
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
Tools to detect design smells
Designite
http://www.designite-tools.com
The message
Every engineering discipline has to deal with
non-optimal and sub-optimal solutions.
Software engineering is not an exception;
in fact, it is more prone to such smells.
Design smells – know them, avoid
them, and refactor them!
References
“Refactoring for software design
smells: Managing technical debt”
By Girish Suryanarayana, Ganesh SG,
and Tushar Sharma.
Morgan Kaufmann. ISBN 978-0128013977
http://www.designsmells.com
Tushar Sharma
@Sharma__Tushar
tusharsharma@ieee.org
http://www.tusharma.in

Does your design smell - Tushar Sharma

  • 1.
    Does Your DesignSmell? Tushar Sharma http://www.tusharma.in
  • 2.
    Cities grow, citiesevolve, (...), each city has to be renewed in order to meet the needs of its populace. From time to time cities are intentionally refactored (...); most times cities are refactored in bits (...), but sometimes the literal and figurative debt is so great a once great city falls into despair (...). Grady Booch
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
    Why? Every engineering disciplinehas to deal with non-optimal and sub-optimal solutions. Such solutions violate fundamental principles of the discipline. Smells
  • 16.
    Design smells Design smellsare certain structures in the design that indicate violation of fundamental design principles and negatively impact design quality.
  • 17.
    Design smells Design smellsare certain structures in the design that indicate violation of fundamental design principles and negatively impact design quality.
  • 18.
    Missing road This smellarises when clumps of data or encoded strings are used instead of creating a class or an interface. Missing abstraction
  • 19.
    public class Throwable{ public void printStackTrace(); //other methods elided. } (Java 1) Throwable class in JDK
  • 20.
    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)
  • 21.
    Unrestricted access This smelloccurs when the declared accessibility of one or more members of an abstraction is more permissive than actually required. Deficient Encapsulation
  • 22.
    In JDK7 • 508classes - at least one public field • 96 classes - at least five public fields
  • 23.
    Hub-like road This smellarises when an abstraction has dependencies (both incoming and outgoing) with a large number of other abstractions. Hub-like Modularization
  • 24.
  • 25.
    Footpath is nota road This smell arises when a supertype and its subtype conceptually do not share an “IS-A” relationship resulting in broken substitutability. Broken Hierarchy
  • 26.
    Stack is nota Vector
  • 27.
    This smell ariseswhen two or more abstractions depend on each other directly or indirectly. Cyclically-dependent Modularization Deadlock!! Cycle!!
  • 29.
    This smell ariseswhen an abstraction has more than one responsibility assigned to it. Multifaceted Abstraction Multitasking
  • 30.
    Image class • load •process • render • store Example multiple reasons to change granularity of a responsibility
  • 31.
    This smell occurswhen an abstraction that is actually not needed (and thus could have been avoided) gets introduced in a software design. Unnecessary Abstraction Useless… unnecessary
  • 32.
    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
  • 33.
  • 34.
    This smell ariseswhen an abstraction exists that has not been completely decomposed, and a further decomposition could reduce its size, implementation complexity, or both. Insufficient Modularization Examples • SocketPermission getMask method – CC=81 • Component 259 public methods
  • 36.
    Tools to detectdesign smells Infusion
  • 37.
    Tools to detectdesign smells Designite http://www.designite-tools.com
  • 38.
    Tools to detectdesign smells Designite http://www.designite-tools.com
  • 39.
    Tools to detectdesign smells Designite http://www.designite-tools.com
  • 40.
    Tools to detectdesign smells Designite http://www.designite-tools.com
  • 41.
    Tools to detectdesign smells Designite http://www.designite-tools.com
  • 42.
    The message Every engineeringdiscipline has to deal with non-optimal and sub-optimal solutions. Software engineering is not an exception; in fact, it is more prone to such smells. Design smells – know them, avoid them, and refactor them!
  • 43.
    References “Refactoring for softwaredesign smells: Managing technical debt” By Girish Suryanarayana, Ganesh SG, and Tushar Sharma. Morgan Kaufmann. ISBN 978-0128013977 http://www.designsmells.com
  • 44.