refactoring for software design smells

Post on 15-Jan-2017

619 Views

Category:

Software

19 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Refactoring for Software Design Smells

Ganesh Samarthyamganesh.samarthyam@gmail.com

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

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

Who coined the term “code

smell”?

Kent Beck

Who coined the acronym “SOLID”?

(as in SOLID principles)

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

Why Care About Design Quality?

When,duetoconstraints,Idesignquicklyanddirty,myprojectisloadedwithtechnicaldebt

Tool: Sonar!

Sonarqubehttp://www.sonarqube.org

The City Metaphor

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

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.

Tool: CodeCity!

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

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

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

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

Architecture Smell Example

Design Smell Example

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

Focus on 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)

… and a Conference Paper

Finally a Book

For Architects: Design is the Key!

Why Care About Principles?

Equivalent Principles in Software Design?

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

Equivalent Principles in Software Design!

Principles*

Abstrac/on*

Encapsula/on*

Modulariza/on*

Hierarchy*

Proactive Application: Enabling Techniques

Reactive Application: Smells

Design Smells: Example #1

Discussion Example

Design Smells: Example #2

Discussion Example

Design Smells: Example #3

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!

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?

java.time package!

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!

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]

More classes in Date/Time API

IMPaCT Process Model

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?

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

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

email sgganesh@gmail.com

website www.designsmells.com

twitter @GSamarthyam

linkedin bit.ly/sgganesh

slideshare slideshare.net/sgganesh

top related