How to Apply Design
Principles in Practice?
Ganesh Samarthyam
Entrepreneur; Author; Conf. speaker
ganesh@codeops.tech
www.designsmells.com; www.codeops.tech
"The critical design tool for software development
is a mind well educated in design principles.
It is not the UML or any other technology"
- Craig Larman
Why care about design quality and
design principles?
"We thought we were just programming on an airplane”
- Kent Beck
SOLID principles
•  There&should&never&be&more&than&one&reason&for&a&class&to&
change&&
Single'Responsibility'
Principle'(SRP)'
•  So6ware&en88es&(classes,&modules,&func8ons,&etc.)&should&
be&open&for&extension,&but&closed&for&modifica8on&
Open'Closed'Principle'
(OCP)'
•  Pointers&or&references&to&base&classes&must&be&able&to&use&
objects&of&derived&classes&without&knowing&it&
Liskov’s'Subs<tu<on'
Principle'(LSP)'
•  Depend&on&abstrac8ons,&not&on&concre8ons&
Dependency'Inversion'
Principle'(DIP)'
•  Many&clientGspecific&interfaces&are&beHer&than&one&
generalGpurpose&interface&
Interface'Segrega<on'
Principle'(ISP)'
Booch’s fundamental principles
Principles*
Abstrac/on*
Encapsula/on*
Modulariza/on*
Hierarchy*
How to apply principles in practice?
Principles
Code
How to bridge
the gap?
Proactive application: enabling techniques
Discussion example
Violates Single
Responsibility
Principle (SRP)
Completeness: Example
Discussion Example
Principle of Least Astonishment: Example
Discussion Example
Use “enabling techniques” to apply
design principles in practice
Key-takeaway
#1
Design smells: example
Discussion example
// using java.util.Date
Date today = new Date();
System.out.println(today);
$ java DateUse
Wed Dec 02 17:17:08 IST 2015
Why should we get the
time and timezone details
if I only want a date? Can
I get rid of these parts?
No!
“So what”!
Date today = new Date();
System.out.println(today);
Date todayAgain = new Date();
System.out.println(todayAgain);
System.out.println(today.compareTo(todayAgain) == 0);
Thu Mar 17 13:21:55 IST 2016
Thu Mar 17 13:21:55 IST 2016
false
What is going
on here?
Refactoring for Date
Replace inheritance
with delegation
java.time package!
Refactored solution
LocalDate today = LocalDate.now();
System.out.println(today);
LocalDate todayAgain = LocalDate.now();
System.out.println(todayAgain);
System.out.println(today.compareTo(todayAgain) == 0);
2016-03-17
2016-03-17
true
Works fine
now!
Refactored example …
You can use only date,
time, or even timezone,
and combine them as
needed!
LocalDate today = LocalDate.now();
System.out.println(today);
LocalTime now = LocalTime.now();
System.out.println(now);
ZoneId id = ZoneId.of("Asia/Tokyo");
System.out.println(id);
LocalDateTime todayAndNow = LocalDateTime.now();
System.out.println(todayAndNow);
ZonedDateTime todayAndNowInTokyo = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));
System.out.println(todayAndNowInTokyo);
2016-03-17
13:28:06.927
Asia/Tokyo
2016-03-17T13:28:06.928
2016-03-17T16:58:06.929+09:00[Asia/Tokyo]
More classes in Date/Time API
What’s that smell?
switch'(transferType)'{'
case'DataBuffer.TYPE_BYTE:'
byte'bdata[]'='(byte[])inData;'
pixel'='bdata[0]'&'0xff;'
length'='bdata.length;'
break;'
case'DataBuffer.TYPE_USHORT:'
short'sdata[]'='(short[])inData;'
pixel'='sdata[0]'&'0xffff;'
length'='sdata.length;'
break;'
case'DataBuffer.TYPE_INT:'
int'idata[]'='(int[])inData;'
pixel'='idata[0];'
length'='idata.length;'
break;'
default:'
throw' new' UnsupportedOperaQonExcepQon("This' method' has' not' been' "+' "implemented'
for'transferType'"'+'transferType);'
}'
Don’t repeat yourself (DRY) principle
protected(int(transferType;! protected(DataBuffer(dataBuffer;!
pixel(=(dataBuffer.getPixel();(
length(=(dataBuffer.getSize();!
switch((transferType)({(
case(DataBuffer.TYPE_BYTE:(
byte(bdata[](=((byte[])inData;(
pixel(=(bdata[0](&(0xff;(
length(=(bdata.length;(
break;(
case(DataBuffer.TYPE_USHORT:(
short(sdata[](=((short[])inData;(
pixel(=(sdata[0](&(0xffff;(
length(=(sdata.length;(
break;(
case(DataBuffer.TYPE_INT:(
int(idata[](=((int[])inData;(
pixel(=(idata[0];(
length(=(idata.length;(
break;(
default:(
throw( new( UnsupportedOperaRonExcepRon("This( method(
has( not( been( "+( "implemented( for( transferType( "( +(
transferType);(
}!
Refactor “bad smells” to apply
design principles in practice
Key-takeaway
#2
Tool driven approach for design
quality
Comprehension tools
STAN
http://stan4j.com
Comprehension tools
Code City
http://www.inf.usi.ch/phd/wettel/codecity.html
Comprehension tools
Imagix 4D
http://www.imagix.com
Critique, code-clone detectors, and metric tools
Infusion
www.intooitus.com/products/infusion
Critique, code-clone detectors, and metric tools
Designite
www.designite-tools.com
Critique, code-clone detectors, and metric tools
PMD Copy Paste Detector (CPD)
http://pmd.sourceforge.net/pmd-4.3.0/cpd.html
Critique, code-clone detectors, and metric tools
Understand
https://scitools.com
Technical debt quantification/visualization tools
Sonarqube
http://www.sonarqube.org
Refactoring tools
ReSharper
https://www.jetbrains.com/resharper/features/
Use design analysis tools to apply
design principles in practice
Key-takeaway
#3
Key take-aways
❖ Thee effective ways to apply design principles in
practice:
❖ Use “enabling techniques”
❖ Refactoring “bad smells”
❖ Use design analysis tools
Proactive application: enabling techniques
Reactive application: refactoring smells
Enablers: tools for refactoring
Jhawk&
(Java)&
CodeCity&&
(C++,&Java,&C#)&&
CppDepend&
(C++)&
Sotograph&
(C++,&Java,&C#)&
Imagix&4D&&
(C,&C++,&Java)&
La?x&
(C/C++,&Java,&C#)&&
SolidSX&&
(C++,&Java,&C#)&
Bauhaus&
(C/C++,&Java,&C#)&
Structure101&&
(Java,&C#)&
Understand&&
(C/C++,&Java,&C#)&
Simian&
(C/C++,&Java,&C#,&…)&
Jarchitect&
(Java)&
Ndepend&
(C#)&
Stan4J&
(Java)&
InFusion&
(C/C++,&Java)&
InCode&
(C/C++,&Java)&
Our upcoming workshops
Modern Software Architecture - July 2
Modern Programming with Java 8 - July 16
Software Refactoring in Practice - July 23
www.codeops.tech
www.designsmells.com
ocpjava.wordpress.com
ganesh@codeops.tech
bit.ly/sgganesh
@GSamarthyam

How to Apply Design Principles in Practice