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

Refactoring for Software Design Smells

Embed Size (px)

Citation preview

Page 1: Refactoring for Software Design Smells

Refactoring for Software Design Smells

Ganesh [email protected]

Page 2: Refactoring for Software Design Smells

“Applying design principles is the key to creating high-quality software!”

Architectural principles: Axis, symmetry, rhythm, datum, hierarchy, transformation

Page 3: Refactoring for Software Design Smells

Who coined the term “code

smell”?

Page 4: Refactoring for Software Design Smells

Kent Beck

Page 5: Refactoring for Software Design Smells

Who coined the acronym “SOLID”?

(as in SOLID principles)

Page 6: Refactoring for Software Design Smells

Michael Feathers

S Single Responsibility Principle

Every object should have a single responsibility and that should be encapsulated by the class

O Open Closed Principle Software should be open for extension, but closed for modification

L Liskov’s Substitution Principle

Any subclass should always be usable instead of its parent class

I Interface Segregation Principle

Many client specific interfaces are better than one general purpose interface

D Dependency Inversion Principle

Abstractions should not depend upon details. Details should depend upon abstractions

Page 7: Refactoring for Software Design Smells

Why Care About Design Quality?

When,duetoconstraints,Idesignquicklyanddirty,myprojectisloadedwithtechnicaldebt

Page 8: Refactoring for Software Design Smells

Tool: Sonar!

Sonarqubehttp://www.sonarqube.org

Page 9: Refactoring for Software Design Smells

The City Metaphor

Source: http://indiatransportportal.com/wp-content/uploads/2012/04/Traffic-congestion1.jpg

Page 10: Refactoring for Software Design Smells

The City Metaphor

“Cities grow, cities evolve, cities have parts that simply die while other

parts flourish; each city has to be renewed in order to meet the needs of its populace… Software-intensive systems

are like that. They grow, they evolve, sometimes they wither away, and

sometimes they flourish…”

Grady Booch in the foreword for “Refactoring for Software Design Smells: Managing Technical Debt”, Girish Suryanarayana, Ganesh Samarthyam, Tushar Sharma, Morgan Kaufmann/Elsevier, 2014.

Page 11: Refactoring for Software Design Smells

Tool: CodeCity!

Page 12: Refactoring for Software Design Smells

What are Smells?“Smells'are'certain'structures'

in'the'code'that'suggest'(some4mes'they'scream'for)'the'possibility'of'refactoring.”''

Page 13: Refactoring for Software Design Smells

Smell Example

logger.severe(“details”)

errlog.log(“details”)logger.error(“details”)

System.err.println(“details”)

Since ROI (Return On Investment) is not clear, how to get

management buy-in for this refactoring?

Log4j java.util.logging custom log library SOPs

Page 14: Refactoring for Software Design Smells

Granularity of SmellsArchitectural+

+Cyclic&dependencies&between&modules&Monolithic&modules&&Layering&viola9ons&(back&layer&call,&skip&layer&call,&ver9cal&layering,&etc)&

Design++God&class&Refused&bequest&&Cyclic&dependencies&between&classes&

Code+(implementa6on)+++Internal&duplica9on&(clones&within&a&class)&&Large&method&&Temporary&field&&

Page 15: Refactoring for Software Design Smells

Architecture Smell Example

Page 16: Refactoring for Software Design Smells

Design Smell Example

Page 17: Refactoring for Software Design Smells

Code Smell Example

class%GraphicsDevice%

%public%void%setFullScreenWindow(Window%w)%{%

%%%%%%%%if%(w%!=%null)%{%

%%%%%%%%%%%%if%(w.getShape()%!=%null)%{%w.setShape(null);%}%

%%%%%%%%%%%%if%(w.getOpacity()%<%1.0f)%{%w.setOpacity(1.0f);%}%

%%%%%%%%%%%%if%(!w.isOpaque())%{%

%%%%%%%%%%%%%%%%Color%bgColor%=%w.getBackground();%

%%%%%%%%%%%%%%%%bgColor%=%new%Color(bgColor.getRed(),% %

%% %bgColor.getGreen(), %

%%%% %bgColor.getBlue(),%255);%

%%%%%%%%%%%%%%%%w.setBackground(bgColor);%

%%%%%%%%%%%%}%

%%%%%%%%}%

…%

}%

This%code%in%

GraphicsDevice%uses%

more%methods%of%

Window%than%calling%

its%own%methods!%

“Feature envy” smell

Page 18: Refactoring for Software Design Smells

Focus on Design Smells

Page 19: Refactoring for Software Design Smells

Initial Journal Paper

PublishedinJournalofObjectTechnology(Vol.12,No.2,2013)   SGGanesh,TusharSharma,GirishSuryanarayana.TowardsaPrinciple-basedClassifica4onofStructuralDesignSmells.InJournalofObjectTechnology,vol.12,no.2,2013,pages1:1–29.doi:10.5381/jot.2013.12.2.a1  URL:hLp://www.jot.fm/issues/issue_2013_06/arPcle1.pdf(openaccess)

Page 20: Refactoring for Software Design Smells

… and a Conference Paper

Page 21: Refactoring for Software Design Smells

Finally a Book

Page 22: Refactoring for Software Design Smells

For Architects: Design is the Key!

Page 23: Refactoring for Software Design Smells

Why Care About Principles?

Page 24: Refactoring for Software Design Smells

Equivalent Principles in Software Design?

Architectural principles: Axis, symmetry, rhythm, datum, hierarchy, transformation

Page 25: Refactoring for Software Design Smells

Equivalent Principles in Software Design!

Principles*

Abstrac/on*

Encapsula/on*

Modulariza/on*

Hierarchy*

Page 26: Refactoring for Software Design Smells

Proactive Application: Enabling Techniques

Page 27: Refactoring for Software Design Smells

Reactive Application: Smells

Page 28: Refactoring for Software Design Smells

Design Smells: Example #1

Page 29: Refactoring for Software Design Smells

Discussion Example

Page 30: Refactoring for Software Design Smells

Design Smells: Example #2

Page 31: Refactoring for Software Design Smells

Discussion Example

Page 32: Refactoring for Software Design Smells

Design Smells: Example #3

Page 33: Refactoring for Software Design Smells

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

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

java.time package!

Page 36: Refactoring for Software Design Smells

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 37: Refactoring for Software Design Smells

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 38: Refactoring for Software Design Smells

More classes in Date/Time API

Page 39: Refactoring for Software Design Smells

IMPaCT Process Model

Page 40: Refactoring for Software Design Smells

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 41: Refactoring for Software Design Smells

“Applying design principles is the key to creating high-quality software!”

Architectural principles: Axis, symmetry, rhythm, datum, hierarchy, transformation