refactoring for software design smells - tech talk
TRANSCRIPT
![Page 2: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/2.jpg)
–Craig Larman
"The critical design tool for software development is a mind well educated in design principles"
![Page 3: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/3.jpg)
Warm-up question #1What does this tool
visualize?
![Page 4: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/4.jpg)
Warm-up question #2
Who coined the term
“code smell”?
Hint: He also originated the
terms TDD and XP
![Page 5: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/5.jpg)
Warm-up question #3
Who wrote the foreword for our book?
![Page 6: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/6.jpg)
Why care about design quality and design principles?
![Page 7: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/7.jpg)
Poor software quality costs more than $150 billion per year
in U.S. and greater than $500 billion per year worldwide
- Capers Jones
Why care? Reason #1
![Page 8: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/8.jpg)
*h$p://sqgne.org/presenta2ons/2012-13/Jones-Sep-2012.pdf
0
20
40
60
80
100
120
IBMCorporta+on
(MVS)
SPRCorpora+on(ClientStudies)
TRWCorpora+on
MITRECorpora+on
NipponElectricCorp
PercentageContribu+on
IndustryDataonDefectOrigins
Adminstra+veErrors
Documenta+onErrors
BadFixes
CodingErrors
DesignErrors
RequirementsErrors
Upto64%ofsoNwaredefectscanbetracedbacktoerrorsinsoNwaredesigninenterprisesoNware!
Why care? Reason #1
![Page 9: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/9.jpg)
Source: Consortium of IT Software Quality (CISQ), Bill Curtis, Architecturally Complex Defects, 2012
![Page 10: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/10.jpg)
“The problem with quick and dirty...is that dirty remains long after quick has been forgotten”
Steve C McConnell
![Page 11: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/11.jpg)
Why care? Reason #3
“Technical debt is the debt that accrues when you knowingly or unknowingly make
wrong or non-optimal design decisions”
![Page 12: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/12.jpg)
Why care? Reason #3
Reference: Zen and the art of software quality – Jim Highsmith Agile 2009 conference
![Page 13: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/13.jpg)
![Page 14: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/14.jpg)
Recent Korean translation of
the book
![Page 15: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/15.jpg)
"... a delightful, engaging, actionable read... you have in your hand a veritable field guide of smells... one of the more interesting and complex expositions of
software smells you will ever find..."
- From the foreword by Grady Booch (IBM Fellow and Chief Scientist for Software Engineering, IBM Research)
![Page 16: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/16.jpg)
"We thought we were just programming on an airplane”- Kent Beck
Why care? Reason #2
![Page 17: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/17.jpg)
"This is a good book about ‘Design Smells’ – actually a great book – nicely organized - clearly written with
plenty of examples and a fair sprinkling of anecdotes."
- Will Tracz (Principal Research Scientist & Fellow, Lockheed Martin)
(review in ACM SIGSOFT Software Engineering Notes)
![Page 18: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/18.jpg)
Fundamental principles in software design
Principles*
Abstrac/on*
Encapsula/on*
Modulariza/on*
Hierarchy*
![Page 19: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/19.jpg)
Effective design: Java parallel streams❖ Applies the principles of abstraction and encapsulation
effectively to significantly simplify concurrent programming
Parallel code
Serial code
![Page 20: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/20.jpg)
Parallel streams: example
import java.util.stream.LongStream;
class PrimeNumbers { private static boolean isPrime(long val) { for(long i = 2; i <= val/2; i++) { if((val % i) == 0) { return false; } } return true; } public static void main(String []args) { long numOfPrimes = LongStream.rangeClosed(2, 50_000) .parallel() .filter(PrimeNumbers::isPrime) .count(); System.out.println(numOfPrimes); } }
![Page 21: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/21.jpg)
long numOfPrimes = LongStream.rangeClosed(2, 100_000) .filter(PrimeNumbers::isPrime) .count();
System.out.println(numOfPrimes);
Prints 9592
2.510 seconds
![Page 22: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/22.jpg)
Parallel code
Serial code
Let’s flip the switch by calling parallel() function
![Page 23: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/23.jpg)
long numOfPrimes = LongStream.rangeClosed(2, 100_000) .parallel() .filter(PrimeNumbers::isPrime) .count();
System.out.println(numOfPrimes);
Prints 9592
1.235 seconds
![Page 24: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/24.jpg)
Proactive application: enabling techniques
![Page 25: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/25.jpg)
Reactive application: smells
![Page 26: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/26.jpg)
What are smells?“Smells'are'certain'structures'
in'the'code'that'suggest'(some4mes'they'scream'for)'the'possibility'of'refactoring.”''
![Page 27: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/27.jpg)
What is refactoring?Refactoring (noun): a change
made to the internal structure of software to make it easier to understand and cheaper to modify without changing its
observable behavior
Refactor (verb): to restructure software by applying a series
of refactorings without changing its observable
behavior
![Page 28: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/28.jpg)
Design smells: example #1
![Page 29: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/29.jpg)
Discussion example
![Page 30: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/30.jpg)
Design smells: example #2
![Page 31: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/31.jpg)
Discussion example
![Page 32: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/32.jpg)
Design smells: example #3
![Page 33: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/33.jpg)
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!
![Page 34: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/34.jpg)
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?
![Page 35: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/35.jpg)
Refactoring for Date
Replace inheritance with delegation
![Page 36: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/36.jpg)
Joda API
Stephen Colebourne
![Page 37: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/37.jpg)
java.time package!
Date, Calendar, and TimeZone Java 8 replaces these types
![Page 38: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/38.jpg)
Refactored solutionLocalDate 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!
![Page 39: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/39.jpg)
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]
![Page 40: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/40.jpg)
More classes in Date/Time API
![Page 41: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/41.jpg)
What’s that smell?
![Page 42: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/42.jpg)
Liskov’s Substitution Principle (LSP)
It#should#be#possible#to#replace#objects#of#supertype#with#objects#of#subtypes#without#
altering#the#desired#behavior#of#the#program#
Barbara#Liskov#
![Page 43: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/43.jpg)
Refactoring
Replace inheritance with delegation
![Page 44: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/44.jpg)
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);'
}'
![Page 45: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/45.jpg)
Replace conditional with polymorphism
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);(
}!
![Page 46: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/46.jpg)
Refactoring: Practical concerns
“Fear of breaking working code”
Is management buy-in necessary for refactoring?
How to refactor code in legacy projects (no automated tests, difficulty in understanding, lack of motivation, …)?
Where do I have time for refactoring?
![Page 47: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/47.jpg)
Tool driven approach for design quality
![Page 49: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/49.jpg)
Comprehension tools
Code Cityhttp://www.inf.usi.ch/phd/wettel/codecity.html
![Page 50: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/50.jpg)
Comprehension tools
Imagix 4Dhttp://www.imagix.com
![Page 51: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/51.jpg)
Smell detection tools
Infusionwww.intooitus.com/products/infusion
![Page 53: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/53.jpg)
Clone analysers
PMD Copy Paste Detector (CPD)http://pmd.sourceforge.net/pmd-4.3.0/cpd.html
![Page 54: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/54.jpg)
Metric tools
Understandhttps://scitools.com
![Page 55: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/55.jpg)
Technical debt quantification/visualization tools
Sonarqubehttp://www.sonarqube.org
![Page 56: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/56.jpg)
Refactoring tools
ReSharperhttps://www.jetbrains.com/resharper/features/
![Page 57: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/57.jpg)
Source: Neal Ford, Emergent Design: https://dl.dropbox.com/u/6806810/Emergent_Design%28Neal_Ford%29.pdf
![Page 59: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/59.jpg)
Tangles in JDK
![Page 60: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/60.jpg)
![Page 61: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/61.jpg)
![Page 62: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/62.jpg)
Refactoring for Software Architecture SmellsGanesh Samarthyam, Tushar Sharma and Girish Suryanarayana
http://www.softrefactoring.com/
![Page 63: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/63.jpg)
Meetups
h"p://www.meetup.com/JavaScript-Meetup-Bangalore/h"p://www.meetup.com/Container-Developers-Meetup-Bangalore/h"p://www.meetup.com/So>ware-Cra>smanship-Bangalore-Meetup/h"p://www.meetup.com/Core-Java-Meetup-Bangalore/h"ps://www.meetup.com/Mobile-App-Developers-Bangalore-Meetup/h"p://www.meetup.com/CloudOps-Meetup-Bangalore/h"p://www.meetup.com/Bangalore-SDN-IoT-NetworkVirtualizaIon-Enthusiasts/h"p://www.meetup.com/So>wareArchitectsBangalore/
![Page 64: Refactoring for Software Design Smells - Tech Talk](https://reader038.vdocuments.mx/reader038/viewer/2022103105/58f184f61a28abc51c8b4603/html5/thumbnails/64.jpg)
[email protected] @GSamarthyam
www.codeops.tech slideshare.net/sgganesh
+91 98801 64463 bit.ly/ganeshsg