64
Refactoring for Software Design Smells Ganesh Samarthyam [email protected]

Refactoring for Software Design Smells - Tech Talk

Embed Size (px)

Citation preview

Page 1: Refactoring for Software Design Smells - Tech Talk

Refactoring for Software Design Smells

Ganesh [email protected]

Page 2: Refactoring for Software Design Smells - Tech Talk

–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

Warm-up question #1What does this tool

visualize?

Page 4: Refactoring for Software Design Smells - Tech Talk

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

Warm-up question #3

Who wrote the foreword for our book?

Page 6: Refactoring for Software Design Smells - Tech Talk

Why care about design quality and design principles?

Page 7: Refactoring for Software Design Smells - Tech Talk

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

*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

Source: Consortium of IT Software Quality (CISQ), Bill Curtis, Architecturally Complex Defects, 2012

Page 10: Refactoring for Software Design Smells - Tech Talk

“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

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

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
Page 14: Refactoring for Software Design Smells - Tech Talk

Recent Korean translation of

the book

Page 15: Refactoring for Software Design Smells - Tech Talk

"... 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

"We thought we were just programming on an airplane”- Kent Beck

Why care? Reason #2

Page 17: Refactoring for Software Design Smells - Tech Talk

"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

Fundamental principles in software design

Principles*

Abstrac/on*

Encapsula/on*

Modulariza/on*

Hierarchy*

Page 19: Refactoring for Software Design Smells - Tech Talk

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

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

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

Parallel code

Serial code

Let’s flip the switch by calling parallel() function

Page 23: Refactoring for Software Design Smells - Tech Talk

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

Proactive application: enabling techniques

Page 25: Refactoring for Software Design Smells - Tech Talk

Reactive application: smells

Page 26: Refactoring for Software Design Smells - Tech Talk

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

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

Design smells: example #1

Page 29: Refactoring for Software Design Smells - Tech Talk

Discussion example

Page 30: Refactoring for Software Design Smells - Tech Talk

Design smells: example #2

Page 31: Refactoring for Software Design Smells - Tech Talk

Discussion example

Page 32: Refactoring for Software Design Smells - Tech Talk

Design smells: example #3

Page 33: Refactoring for Software Design Smells - Tech Talk

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

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

Refactoring for Date

Replace inheritance with delegation

Page 36: Refactoring for Software Design Smells - Tech Talk

Joda API

Stephen Colebourne

Page 37: Refactoring for Software Design Smells - Tech Talk

java.time package!

Date, Calendar, and TimeZone Java 8 replaces these types

Page 38: Refactoring for Software Design Smells - Tech Talk

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

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

More classes in Date/Time API

Page 41: Refactoring for Software Design Smells - Tech Talk

What’s that smell?

Page 42: Refactoring for Software Design Smells - Tech Talk

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

Refactoring

Replace inheritance with delegation

Page 44: Refactoring for Software Design Smells - Tech Talk

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

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

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

Tool driven approach for design quality

Page 48: Refactoring for Software Design Smells - Tech Talk

Comprehension tools

STANhttp://stan4j.com

Page 49: Refactoring for Software Design Smells - Tech Talk

Comprehension tools

Code Cityhttp://www.inf.usi.ch/phd/wettel/codecity.html

Page 50: Refactoring for Software Design Smells - Tech Talk

Comprehension tools

Imagix 4Dhttp://www.imagix.com

Page 51: Refactoring for Software Design Smells - Tech Talk

Smell detection tools

Infusionwww.intooitus.com/products/infusion

Page 52: Refactoring for Software Design Smells - Tech Talk

Smell detection tools

Designitewww.designite-tools.com

Page 53: Refactoring for Software Design Smells - Tech Talk

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

Metric tools

Understandhttps://scitools.com

Page 55: Refactoring for Software Design Smells - Tech Talk

Technical debt quantification/visualization tools

Sonarqubehttp://www.sonarqube.org

Page 56: Refactoring for Software Design Smells - Tech Talk

Refactoring tools

ReSharperhttps://www.jetbrains.com/resharper/features/

Page 57: Refactoring for Software Design Smells - Tech Talk

Source: Neal Ford, Emergent Design: https://dl.dropbox.com/u/6806810/Emergent_Design%28Neal_Ford%29.pdf

Page 58: Refactoring for Software Design Smells - Tech Talk

http://xpconference.in/2016/

Page 59: Refactoring for Software Design Smells - Tech Talk

Tangles in JDK

Page 60: Refactoring for Software Design Smells - Tech Talk
Page 61: Refactoring for Software Design Smells - Tech Talk
Page 62: Refactoring for Software Design Smells - Tech Talk

Refactoring for Software Architecture SmellsGanesh Samarthyam, Tushar Sharma and Girish Suryanarayana

http://www.softrefactoring.com/

Page 63: Refactoring for Software Design Smells - Tech Talk

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/