42
How to Apply Design Principles in Practice? Ganesh Samarthyam Entrepreneur; Author; Conf. speaker [email protected] www.designsmells.com; www.codeops.tech

How to Apply Design Principles in Practice

Embed Size (px)

Citation preview

Page 1: How to Apply Design Principles in Practice

How to Apply Design Principles in Practice?

Ganesh SamarthyamEntrepreneur; Author; Conf. speaker

[email protected] www.designsmells.com; www.codeops.tech

Page 2: How to Apply Design Principles in Practice

"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

Page 3: How to Apply Design Principles in Practice

Why care about design quality and design principles?

Page 4: How to Apply Design Principles in Practice

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

Page 5: How to Apply Design Principles in Practice

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)'

Page 6: How to Apply Design Principles in Practice

Booch’s fundamental principles

Principles*

Abstrac/on*

Encapsula/on*

Modulariza/on*

Hierarchy*

Page 7: How to Apply Design Principles in Practice

How to apply principles in practice?Principles

Code

How to bridge the gap?

Page 8: How to Apply Design Principles in Practice

Proactive application: enabling techniques

Page 9: How to Apply Design Principles in Practice

Discussion example

Violates Single Responsibility

Principle (SRP)

Page 10: How to Apply Design Principles in Practice

Completeness: Example

Page 11: How to Apply Design Principles in Practice

Discussion Example

Page 12: How to Apply Design Principles in Practice

Principle of Least Astonishment: Example

Page 13: How to Apply Design Principles in Practice

Discussion Example

Page 14: How to Apply Design Principles in Practice

Use “enabling techniques” to apply design principles in practice

Key-takeaway #1

Page 15: How to Apply Design Principles in Practice

Design smells: example

Page 16: How to Apply Design Principles in Practice

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 17: How to Apply Design Principles in Practice

“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 18: How to Apply Design Principles in Practice

Refactoring for Date

Replace inheritance with delegation

Page 19: How to Apply Design Principles in Practice

java.time package!

Page 20: How to Apply Design Principles in Practice

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 21: How to Apply Design Principles in Practice

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 22: How to Apply Design Principles in Practice

More classes in Date/Time API

Page 23: How to Apply Design Principles in Practice

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 24: How to Apply Design Principles in Practice

Don’t repeat yourself (DRY) principleprotected(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 25: How to Apply Design Principles in Practice

Refactor “bad smells” to apply design principles in practice

Key-takeaway #2

Page 26: How to Apply Design Principles in Practice

Tool driven approach for design quality

Page 27: How to Apply Design Principles in Practice

Comprehension tools

STANhttp://stan4j.com

Page 28: How to Apply Design Principles in Practice

Comprehension tools

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

Page 29: How to Apply Design Principles in Practice

Comprehension tools

Imagix 4Dhttp://www.imagix.com

Page 30: How to Apply Design Principles in Practice

Critique, code-clone detectors, and metric tools

Infusionwww.intooitus.com/products/infusion

Page 31: How to Apply Design Principles in Practice

Critique, code-clone detectors, and metric tools

Designitewww.designite-tools.com

Page 32: How to Apply Design Principles in Practice

Critique, code-clone detectors, and metric tools

PMD Copy Paste Detector (CPD)http://pmd.sourceforge.net/pmd-4.3.0/cpd.html

Page 33: How to Apply Design Principles in Practice

Critique, code-clone detectors, and metric tools

Understandhttps://scitools.com

Page 34: How to Apply Design Principles in Practice

Technical debt quantification/visualization tools

Sonarqubehttp://www.sonarqube.org

Page 35: How to Apply Design Principles in Practice

Refactoring tools

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

Page 36: How to Apply Design Principles in Practice

Use design analysis tools to apply design principles in practice

Key-takeaway #3

Page 37: How to Apply Design Principles in Practice

Key take-aways

❖ Thee effective ways to apply design principles in practice:

❖ Use “enabling techniques”

❖ Refactoring “bad smells”

❖ Use design analysis tools

Page 38: How to Apply Design Principles in Practice

Proactive application: enabling techniques

Page 39: How to Apply Design Principles in Practice

Reactive application: refactoring smells

Page 40: How to Apply Design Principles in Practice

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)&

Page 41: How to Apply Design Principles in Practice

Our upcoming workshops

Modern Software Architecture - July 2 Modern Programming with Java 8 - July 16 Software Refactoring in Practice - July 23