SlideShare ist ein Scribd-Unternehmen logo
1 von 56
Downloaden Sie, um offline zu lesen
Refactoring
Improving The Design of Existing Code
 Reporter:Jiayun Zhou <jiayun.zhou@iisigroup.com>
 Date :2011/07/13
Outline
Introduction
Bad Smells & Refactorings
Conclusion




    Copyright by IISI. All rights reserved   2
Introduction
Eclipse 1.0 – Nov 2001




       Copyright by IISI. All rights reserved   4
Eclipse 2.0 – Jun 2002




       Copyright by IISI. All rights reserved   5
Eclipse 2.1 – Mar 2003




       Copyright by IISI. All rights reserved   6
Eclipse 3.7 – Jun 2011




       Copyright by IISI. All rights reserved   7
Copyright by IISI. All rights reserved   8
Martin Fowler




   Copyright by IISI. All rights reserved   9
Kent Beck




 Copyright by IISI. All rights reserved   10
Bad Smells & Refactorings
Bad Smell

If it stinks, change it.
                          -Grandma Beck,
      discussing child-rearing philosophy




                  Copyright by IISI. All rights reserved   12
WTF


Copyright by IISI. All rights reserved   13
What the Fxxk


    Copyright by IISI. All rights reserved   14
Copyright by IISI. All rights reserved   15
Good Programmer
• 30% Good Habits
• 30% English
• 40% Passion, Talent, Practice,
  and Everything Else



             Copyright by IISI. All rights reserved   16
Duplicated Code (Bad Smell)

           DRY
  (Don't Repeat Yourself)




          Copyright by IISI. All rights reserved   17
Duplicated Code (Bad Smell)

           DRY
  (Don't Repeat Yourself)
            vs.
           WET
    (We Enjoy Typing)

          Copyright by IISI. All rights reserved   18
Pull Up Method
 (Refactoring)




   Copyright by IISI. All rights reserved   19
Pull Up – 1 (Eclipse)




      Copyright by IISI. All rights reserved   20
Pull Up – 2 (Eclipse)




      Copyright by IISI. All rights reserved   21
Pull Up – 3 (Eclipse)




      Copyright by IISI. All rights reserved   22
Pull Up – 4 (Eclipse)




      Copyright by IISI. All rights reserved   23
Pull Up – 5 (Eclipse)




      Copyright by IISI. All rights reserved   24
Long Method (Bad Smell)
• Short methods are good
• Whenever we feel the need to
  comment something, we write
  a method instead
• Method name explains the
  intention

           Copyright by IISI. All rights reserved   25
Extract Method
 (Refactoring)




   Copyright by IISI. All rights reserved   26
Extract Method – 1 (Eclipse)




          Copyright by IISI. All rights reserved   27
Extract Method – 2 (Eclipse)




          Copyright by IISI. All rights reserved   28
Extract Method – 3 (Eclipse)




          Copyright by IISI. All rights reserved   29
Conditionals and loops also give
 signs for extractions.

Use Decompose Conditional to deal
 with conditional expressions.

With loops, extract the loop and the
 code within the loop into its own
 method.
             Copyright by IISI. All rights reserved   30
Large Class (Bad Smell)
• Trying to do too much
• Shows up as too many instance
  variables
• Duplicated code cannot be far
  behind


           Copyright by IISI. All rights reserved   31
Extract Class
(Refactoring)




  Copyright by IISI. All rights reserved   32
Extract Class – 1 (Eclipse)




         Copyright by IISI. All rights reserved   33
Extract Class – 2 (Eclipse)




         Copyright by IISI. All rights reserved   34
Extract Class – 3 (Eclipse)




         Copyright by IISI. All rights reserved   35
Long Parameter List
        (Bad Smell)
• Hard to understand
• Become inconsistent and
  difficult to use
• Forever changing them as you
  need more data


           Copyright by IISI. All rights reserved   36
Replace Parameter with Method
         (Refactoring)




           Copyright by IISI. All rights reserved   37
Preserve Whole Object
     (Refactoring)




       Copyright by IISI. All rights reserved   38
Introduce Parameter Object
       (Refactoring)




         Copyright by IISI. All rights reserved   39
Introduce Parameter Object –
        01 (Eclipse)




          Copyright by IISI. All rights reserved   40
Introduce Parameter Object –
        02 (Eclipse)




          Copyright by IISI. All rights reserved   41
Introduce Parameter Object –
        03 (Eclipse)




          Copyright by IISI. All rights reserved   42
Comments (Bad Smell)
• They are sweet smell
• Don't use as a deodorant




            Copyright by IISI. All rights reserved   43
Conclusion
How to refactor safely?




        Copyright by IISI. All rights reserved   45
Unit Test


  Copyright by IISI. All rights reserved   46
Copyright by IISI. All rights reserved   47
How about the translation?




         Copyright by IISI. All rights reserved   48
中譯
你可以選擇每個 class 和函式的名字,
 這給了你一個解釋自己意圖的機會。
 class 或函式內部則解釋實現這個意圖
 的作法。如果class 和函式內部又以「
 更小單元的意圖」來編寫,你所寫的
 程式碼就可以「與其結構中的大部分
 重要資訊溝通」。 (p.61)

        Copyright by IISI. All rights reserved   49
原文
Choosing the name of each class and the name
 of each method gives you an opportunity to
 explain what you intend. The internals of the
 class or method explain how the intention is
 realized. If the internals also are written in
 terms of intention in yet smaller pieces, you
 can write code that communicates most of
 the important information about its own
 structure.


                 Copyright by IISI. All rights reserved   50
Copyright by IISI. All rights reserved   51
我的翻譯
你可以選擇每個 class 和函式的名字,
 這給了你一個解釋自己意圖的機會。
 class 或函式內部則解釋實現這個意圖
 的作法。如果class 和函式內部又以「
 更小單元的意圖」來編寫,你所寫的
 程式碼就可以「表達出它自己結構的
 大部分重要資訊」。

        Copyright by IISI. All rights reserved   52
盡信書不如無書




  Copyright by IISI. All rights reserved   53
Copyright by IISI. All rights reserved   54
琴鍵有限,琴藝無限




   Copyright by IISI. All rights reserved   55
- Thank You -



Headquarter
6F., No.7, Sec. 2, Xianmin Blvd., Banqiao Dist., New Taipei City 22041, Taiwan(R.O.C.)
TEL : +886-2-8969-1969     FAX : +886-2-8969-3359

Weitere ähnliche Inhalte

Ähnlich wie Refactoring

flatMap Oslo presentation slides
flatMap Oslo presentation slidesflatMap Oslo presentation slides
flatMap Oslo presentation slides
Martin Odersky
 

Ähnlich wie Refactoring (20)

Need 4 Speed FI
Need 4 Speed FINeed 4 Speed FI
Need 4 Speed FI
 
BKK16-305B ILP32 Performance on AArch64
BKK16-305B ILP32 Performance on AArch64BKK16-305B ILP32 Performance on AArch64
BKK16-305B ILP32 Performance on AArch64
 
flatMap Oslo presentation slides
flatMap Oslo presentation slidesflatMap Oslo presentation slides
flatMap Oslo presentation slides
 
Flatmap
FlatmapFlatmap
Flatmap
 
An Introduction to Enterprise Design Patterns
An Introduction to Enterprise Design PatternsAn Introduction to Enterprise Design Patterns
An Introduction to Enterprise Design Patterns
 
Refactoring - An Introduction
Refactoring - An IntroductionRefactoring - An Introduction
Refactoring - An Introduction
 
The Hacking Games - Operation System Vulnerabilities Meetup 29112022
The Hacking Games - Operation System Vulnerabilities Meetup 29112022The Hacking Games - Operation System Vulnerabilities Meetup 29112022
The Hacking Games - Operation System Vulnerabilities Meetup 29112022
 
The software design principles
The software design principlesThe software design principles
The software design principles
 
Oracle Developer Live: Deploying MySQL InnoDB Cluster on OCI with Terraform
Oracle Developer Live: Deploying MySQL InnoDB Cluster on OCI with TerraformOracle Developer Live: Deploying MySQL InnoDB Cluster on OCI with Terraform
Oracle Developer Live: Deploying MySQL InnoDB Cluster on OCI with Terraform
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoring
 
Interoperable Clouds and How to Build (or Buy) Them
Interoperable Clouds and How to Build (or Buy) ThemInteroperable Clouds and How to Build (or Buy) Them
Interoperable Clouds and How to Build (or Buy) Them
 
What Can Reverse Engineering Do For You?
What Can Reverse Engineering Do For You?What Can Reverse Engineering Do For You?
What Can Reverse Engineering Do For You?
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Beyond the 'cript practical i os reverse engineering lascon
Beyond the 'cript  practical i os reverse engineering lasconBeyond the 'cript  practical i os reverse engineering lascon
Beyond the 'cript practical i os reverse engineering lascon
 
Know your platform. 7 things every scala developer should know about jvm
Know your platform. 7 things every scala developer should know about jvmKnow your platform. 7 things every scala developer should know about jvm
Know your platform. 7 things every scala developer should know about jvm
 
Zero to flow
Zero to flowZero to flow
Zero to flow
 
SNIA : Swift Object Storage adding EC (Erasure Code)
SNIA : Swift Object Storage adding EC (Erasure Code)SNIA : Swift Object Storage adding EC (Erasure Code)
SNIA : Swift Object Storage adding EC (Erasure Code)
 
Deobfuscation and beyond (ZeroNights, 2014)
Deobfuscation and beyond (ZeroNights, 2014)Deobfuscation and beyond (ZeroNights, 2014)
Deobfuscation and beyond (ZeroNights, 2014)
 
Java Debuggers: A Peek Under the Hood [JavaOne 2016 CON1503]
Java Debuggers: A Peek Under the Hood [JavaOne 2016 CON1503]Java Debuggers: A Peek Under the Hood [JavaOne 2016 CON1503]
Java Debuggers: A Peek Under the Hood [JavaOne 2016 CON1503]
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 

Mehr von Jiayun Zhou (8)

Spring Initializr JCConf 2018
Spring Initializr JCConf 2018Spring Initializr JCConf 2018
Spring Initializr JCConf 2018
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring & Hibernate
Spring & HibernateSpring & Hibernate
Spring & Hibernate
 
Python Style Guide
Python Style GuidePython Style Guide
Python Style Guide
 
Python3
Python3Python3
Python3
 
Ionic2
Ionic2Ionic2
Ionic2
 
Akka Cluster in Java - JCConf 2015
Akka Cluster in Java - JCConf 2015Akka Cluster in Java - JCConf 2015
Akka Cluster in Java - JCConf 2015
 
Java EE 6 CDI Integrates with Spring & JSF
Java EE 6 CDI Integrates with Spring & JSFJava EE 6 CDI Integrates with Spring & JSF
Java EE 6 CDI Integrates with Spring & JSF
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

Refactoring