64
Software Design in Practice Ganesh Samarthyam Entrepreneur; author; speaker [email protected] www.codeops.tech

Software Design in Practice (with Java examples)

Embed Size (px)

Citation preview

Page 1: Software Design in Practice (with Java examples)

Software Design in Practice

Ganesh SamarthyamEntrepreneur; author; [email protected]

Page 2: Software Design in Practice (with Java examples)

Discussion Question

Which one of the following is MOST IGNORED aspect in software development?

❖ Code quality

❖ Software testing

❖ Software design

❖ Programming

Page 3: Software Design in Practice (with Java examples)
Page 4: Software Design in Practice (with Java examples)
Page 5: Software Design in Practice (with Java examples)

"... 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 6: Software Design in Practice (with Java examples)

Why Care About Principles?

Page 7: Software Design in Practice (with Java examples)

Poor software quality costs more than $150 billion per year

in U.S. and greater than $500 billion per year worldwide

- Capers Jones

Page 8: Software Design in Practice (with Java examples)

Source: Estimating the Principal of an Application's Technical Debt, Bill Curtis, Jay Sappidi, Alexandra Szynkarski, IEEE Software, Nov.-Dec. 2012.

Page 9: Software Design in Practice (with Java examples)

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

Page 10: Software Design in Practice (with Java examples)

–Craig Larman

"The critical design tool for software development is a mind well educated in design principles"

Page 11: Software Design in Practice (with Java examples)

For Architects: Design is the Key!

Page 12: Software Design in Practice (with Java examples)

Design Thinking

Page 13: Software Design in Practice (with Java examples)

Fundamental Principles in Software Design

Principles*

Abstrac/on*

Encapsula/on*

Modulariza/on*

Hierarchy*

Page 14: Software Design in Practice (with Java examples)

Proactive Application: Enabling Techniques

Page 15: Software Design in Practice (with Java examples)

Reactive Application: Smells

Page 16: Software Design in Practice (with Java examples)

$ cat limerick.txt There was a young lady of Niger Who smiled as she rode on a tiger. They returned from the ride With the lady inside And a smile on the face of the tiger.

Page 17: Software Design in Practice (with Java examples)

$ cat limerick.txt | tr -cs "[:alpha:]" "\n" | awk '{print length(), $0}' | sort | uniq

1 a 2 as 2 of 2 on 3 And 3 Who 3 she 3 the 3 was 4 They 4 With 4 face 4 from 4 lady 4 ride 4 rode 5 Niger 5 There 5 smile 5 tiger 5 young 6 inside 6 smiled 8 returned

Page 18: Software Design in Practice (with Java examples)

List<String> lines = Files.readAllLines(Paths.get("./limerick.txt"), Charset.defaultCharset());

Map<Integer, List<String>> wordGroups = lines.stream() .map(line -> line.replaceAll("\\W", "\n").split("\n")) .flatMap(Arrays::stream) .sorted() .distinct() .collect(Collectors.groupingBy(String::length));

wordGroups.forEach( (count, words) -> { words.forEach(word -> System.out.printf("%d %s %n", count, word)); });

1 a 2 as 2 of 2 on 3 And 3 Who 3 she 3 the 3 was 4 They 4 With 4 face 4 from 4 lady 4 ride 4 rode 5 Niger 5 There 5 smile 5 tiger 5 young 6 inside 6 smiled 8 returned

Page 19: Software Design in Practice (with Java examples)

Parallel Streams

Page 20: Software Design in Practice (with Java examples)

race conditions

Page 21: Software Design in Practice (with Java examples)
Page 22: Software Design in Practice (with Java examples)

deadlocks

Page 23: Software Design in Practice (with Java examples)
Page 24: Software Design in Practice (with Java examples)

I really really hate concurrency problems

Page 25: Software Design in Practice (with Java examples)

Parallel code

Serial code

Page 26: Software Design in Practice (with Java examples)

long numOfPrimes = LongStream.rangeClosed(2, 100_000) .filter(PrimeNumbers::isPrime) .count();

System.out.println(numOfPrimes);

Prints 9592

2.510 seconds

Page 27: Software Design in Practice (with Java examples)

Parallel code

Serial code

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

Page 28: Software Design in Practice (with Java examples)

long numOfPrimes = LongStream.rangeClosed(2, 100_000) .parallel() .filter(PrimeNumbers::isPrime) .count();

System.out.println(numOfPrimes);

Prints 9592

1.235 seconds

Page 29: Software Design in Practice (with Java examples)

Wow! That’s an awesome flip switch!

Page 30: Software Design in Practice (with Java examples)

Internally, parallel streams make use of fork-join framework

Page 31: Software Design in Practice (with Java examples)

Discussion Question

What is refactoring?

❖ Needless rework!

❖ Changing internal structure without changing external behaviour

❖ Changing external quality (scalability, performance, etc)

❖ Fixing hidden or latent defects

Page 32: Software Design in Practice (with Java examples)

What’s that smell?public'Insets'getBorderInsets(Component'c,'Insets'insets){'

''''''''Insets'margin'='null;'

'''''''''//'Ideally'we'd'have'an'interface'defined'for'classes'which'

''''''''//'support'margins'(to'avoid'this'hackery),'but'we've'

''''''''//'decided'against'it'for'simplicity'

''''''''//'

''''''''if'(c'instanceof'AbstractBuEon)'{'

'''''''''''''''''margin'='((AbstractBuEon)c).getMargin();'

''''''''}'else'if'(c'instanceof'JToolBar)'{'

''''''''''''''''margin'='((JToolBar)c).getMargin();'

''''''''}'else'if'(c'instanceof'JTextComponent)'{'

''''''''''''''''margin'='((JTextComponent)c).getMargin();'

''''''''}'

''''''''//'rest'of'the'code'omiEed'…'

Page 33: Software Design in Practice (with Java examples)

Refactoring

Page 34: Software Design in Practice (with Java examples)

Refactoring

!!!!!!!margin!=!c.getMargin();

!!!!!!!!if!(c!instanceof!AbstractBu8on)!{!

!!!!!!!!!!!!!!!!!margin!=!((AbstractBu8on)c).getMargin();!

!!!!!!!!}!else!if!(c!instanceof!JToolBar)!{!

!!!!!!!!!!!!!!!!margin!=!((JToolBar)c).getMargin();!

!!!!!!!!}!else!if!(c!instanceof!JTextComponent)!{!

!!!!!!!!!!!!!!!!margin!=!((JTextComponent)c).getMargin();!

!!!!!!!!}

Page 35: Software Design in Practice (with Java examples)

Design Smells: Example

Page 36: Software Design in Practice (with Java examples)

Discussion Example

Page 37: Software Design in Practice (with Java examples)

Design Smells: Example

Page 38: Software Design in Practice (with Java examples)

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 39: Software Design in Practice (with Java examples)

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 40: Software Design in Practice (with Java examples)

Refactoring for Date

Replace inheritance with delegation

Page 41: Software Design in Practice (with Java examples)

Joda API

JSR 310: Java Date and Time API

Stephen Colebourne

Page 42: Software Design in Practice (with Java examples)

java.time package!

Page 43: Software Design in Practice (with Java examples)

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 44: Software Design in Practice (with Java examples)

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 45: Software Design in Practice (with Java examples)

More classes in Date/Time API

Page 46: Software Design in Practice (with Java examples)

What’s that smell?

Page 47: Software Design in Practice (with Java examples)

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 48: Software Design in Practice (with Java examples)

Refactoring

Replace inheritance with delegation

Page 49: Software Design in Practice (with Java examples)

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 50: Software Design in Practice (with Java examples)

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 51: Software Design in Practice (with Java examples)

Discussion Question

What is the biggest deterrent to performing refactoring?

❖ Deadlines! (Lack of time and resources)

❖ Fear of breaking the working code

❖ Not able to get management buy-in for refactoring

❖ Lack of technical skills to perform refactoring

Page 52: Software Design in Practice (with Java examples)

Configuration Smells!

Page 53: Software Design in Practice (with Java examples)

Language Features & Refactoring public static void main(String []file) throws Exception { // process each file passed as argument

// try opening the file with FileReader try (FileReader inputFile = new FileReader(file[0])) { int ch = 0; while( (ch = inputFile.read()) != -1) { // ch is of type int - convert it back to char System.out.print( (char)ch ); } } // try-with-resources will automatically release FileReader object }

public static void main(String []file) throws Exception { Files.lines(Paths.get(file[0])).forEach(System.out::println); }

Java 8 lambdas and streams

Page 54: Software Design in Practice (with Java examples)

Tangles in JDK

Page 55: Software Design in Practice (with Java examples)

Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Page 56: Software Design in Practice (with Java examples)

Copyright © 2015, Oracle and/or its affiliates. All rights reserved

Page 57: Software Design in Practice (with Java examples)

Structural Analysis for Java (stan4j)

Page 58: Software Design in Practice (with Java examples)

JArchitect

Page 59: Software Design in Practice (with Java examples)

InFusion

Page 60: Software Design in Practice (with Java examples)

SotoArc

Page 61: Software Design in Practice (with Java examples)

CodeCity

Page 62: Software Design in Practice (with Java examples)

Name the first object

oriented programming

language

Page 63: Software Design in Practice (with Java examples)

BANGALORE CONTAINER CONFERENCE 2017

Sponsorship Deck

Apr 07Bangalore

www.containerconf.in