SlideShare a Scribd company logo
삶이 편해지는 쉬운 리팩토링(Refactoring)
유석문
http://xaxor.com/bizarre/23678-dangerou
s-work-part-4-construction-workers.html
Refactoring?!?!!
What is Refactoring?
A change made to the internal structure of software to make it
easier to understand and cheaper to modify without changing its
observable behavior. — Refactoring, Martin Fowler, page 53.
Refactoring typically involves
• Removing duplicated or dead code
• Simplifying complex code
• Clarifying unclear code
Refactoring is revision
All good writing is based upon revision.
— Jacques Barzun, Simple & Direct, 4th Edition
Revision means to re-see or see again.
How to Refactor
The secret to successful refactoring is to
take baby steps.
When refactoring, your ultimate goal is not to:
• break anything
• make things worse
• introduce new behavior
How to Refactor
• Verify that all automated tests (unit tests) pass
• Decide what code to change
• Implement one or more refactorings carefully
• Run the unit tests whenever you wish to confirm that changes ha
ve not altered system behavior
• Repeat until the refactoring is complete or revert to an earlier
state
Refactoring?!?!!
http://daily-pins.com/there-was-a-spider-its-gone-now/
A Chicken and Egg Problem?
Many people, when they first encounter the concept of unit testing and
refactoring legacy code, consider it a chicken-and-egg conundrum:
Can't do one without having the other.
Unit Testing Refactoring
Start with the Universals
Always start refactoring work with the three universals.
1. Reformat and Commit. Establish a baseline for later diffs.
2. Eliminate the Noise. A refactored file always minimizes noise and m
aximizes signal.
3. Get To Testability. We're going to need some tests. Can we even st
art to test our class?
Consolidate Conditional Expression
Clarity
"Any fool can write code that a computer can understand.
Good programmers write code that humans can understand."
— Martin Fowler, Refactoring
Extract Hierarchy
Highly Cohesive
Methods do only one thing and classes have a single, clear responsibility.
Extract Superclass
Duplication Free
A program should express each idea once and only once. Code may not
always appear to be identical and yet may be expressing the same logic
and information. Duplicate code may diverge to become out of sync
Hide Method
Encapsulated
A form of information hiding, code that is encapsulated
does not expose data or behavior that should be invisible
Inline Method
Simple
Code reflects the shortest path to a solution and incorporates
only enough complexity to handle its known responsibilities.
Simple code is easier to maintain and evolve.
Caller Creates
When you need to create a new class or method that will be invoked by a
caller, don't perform the creation where the new code will reside (e.g. in a
new file or within the class that will contain the new method).
Instead, declare the to-be-created class/method where it will be used, i.e.
in the caller code.
This will allow you to use IDE tools to automatically generate the exact co
de needed by the client, instead of retrofitting client code to the new class/
method after creation.
Automated Refactorings
Rejected Parameter
To avoid passing a parameter to code you want to extract, you must first rej
ect the parameter from the extraction.
The video on the next page presents three different ways to reject a
parameter:
1. Inline the unwanted parameter.
2. Move the unwanted parameter out of the extraction block.
3. Turn the unwanted parameter into a field so it can be referenced from t
he extracted method instead of being passed as a parameter.
Move (Caller Swap)
Move responsibilities from a caller to a parameter
by calling the parameter with the original caller as its parameter.
Encapsulated Dependency
Before moving code to a new class, first encapsulate
dependencies, like fields.
Remove Duplication by Extract Method - Before
Remove Duplication - After
Sample
Removing A Long Method Smell
Removing A Long Method Smell (Composed Method)
Removing Unnecessary hierarchy
Removing Unnecessary hierarchy
Change hierarchy
A Map holds keys and values,
whereas List and Set just
hold objects.
Duplicated Fields & Methods
Duplicated Fields & Methods in List and Set
AbstractCollection's method, addAll(...), contains
the smells Long Method, Duplicated Code , Switch St
atement and Alternative Classes With Different Interfa
ces
Duplicated Fields & Methods
Don’t Fix Bugs during Refactoring
A List should allow duplicates, yet in
AbstractCollection your addAll(...) method is
checking whether the element is already
contained before adding it.
There is a time and place to fix bugs and it is
not during refactoring.
Primitive Obsession In Map
Map suffers from the Primitive Obsession smell
because it wastes too much code manipulating
the keys and values arrays.
Scaffolding
Builders make changes to
buildings by first putting up
scaffolding to make their workea
sier and safer.
Extract Method refactoring to produce:
getValueAt(...)
setValueAt(...)
When you no longer need your scaffolding, you can remove it by
applying the Inline Method refactoring.
A Temporary Field In Map
Map has a field called indexWhereKeyFound that is a p
rime example of the Temporary Field Smell.
Thank you.

More Related Content

PPS
Few minutes To better Code - Refactoring
PPTX
Being Lean Agile
ODP
Improve your development skills with Test Driven Development
PPT
Principles in Refactoring
PPTX
Bye Bye Cowboy Coder Days! (Legacy Code & TDD)
PPTX
Refactoring legacy code driven by tests - ENG
PDF
Unit testing legacy code
PDF
Test Driven Development
Few minutes To better Code - Refactoring
Being Lean Agile
Improve your development skills with Test Driven Development
Principles in Refactoring
Bye Bye Cowboy Coder Days! (Legacy Code & TDD)
Refactoring legacy code driven by tests - ENG
Unit testing legacy code
Test Driven Development

What's hot (20)

PPTX
Unit Testing in Action - C#, NUnit, and Moq
PPT
Automating C# Coding Standards using StyleCop and FxCop
PDF
Win at life with unit testing
PPTX
Working Effectively With Legacy Code
PPTX
Presenter deck icenium hol
PPTX
Bdd and spec flow
PPT
TDD And Refactoring
PDF
Eclipse and Java 8 - Eclipse Day India 2013
PDF
Object-oriented Analysis, Design & Programming
PPTX
BDD for APIs
PDF
Automated testing
PPTX
PHPUnit - Unit testing
PDF
Eclipse Tips & Tricks - EclipseCon North America 2014
PDF
Writing Testable Code
PPTX
c3ee900a-894b-4680-894c-42f77ffd4446-160308162303
PDF
Eclipse Luna - What's New!
PDF
JDT Embraces Lambda Expressions - EclipseCon North America 2014
ODP
FluentSelenium Presentation Code Camp09
PPTX
Behaviour Driven Development with SpecFlow
PPTX
A Beginer's Guide to testing in Django
Unit Testing in Action - C#, NUnit, and Moq
Automating C# Coding Standards using StyleCop and FxCop
Win at life with unit testing
Working Effectively With Legacy Code
Presenter deck icenium hol
Bdd and spec flow
TDD And Refactoring
Eclipse and Java 8 - Eclipse Day India 2013
Object-oriented Analysis, Design & Programming
BDD for APIs
Automated testing
PHPUnit - Unit testing
Eclipse Tips & Tricks - EclipseCon North America 2014
Writing Testable Code
c3ee900a-894b-4680-894c-42f77ffd4446-160308162303
Eclipse Luna - What's New!
JDT Embraces Lambda Expressions - EclipseCon North America 2014
FluentSelenium Presentation Code Camp09
Behaviour Driven Development with SpecFlow
A Beginer's Guide to testing in Django
Ad

Similar to Agile korea 2013 유석문 (20)

PDF
Refactoring: Improve the design of existing code
PDF
Refactoring
PPT
Refactoring Tips by Martin Fowler
PPT
Code Refactoring
PDF
Refactoring
PPTX
Code Refactoring
PPTX
Code smells
PDF
Bad Smell In Codes 1
PDF
Refactoring 2TheMax (con ReSharper)
PDF
Code refactoring workshop (in Javascript)
PPTX
Code smells and remedies
PDF
Bade Smells in Code
PPTX
Refactoring Chapter 6,7.pptx
PPT
Code Refactoring - 3.0
PDF
Refactoring Fest
PDF
Introduction to Refactoring
PDF
Refactoring
PDF
Refactoring, Therapeutic Attitude to Programming.
PPTX
31 days Refactoring
PDF
Refactoring
Refactoring: Improve the design of existing code
Refactoring
Refactoring Tips by Martin Fowler
Code Refactoring
Refactoring
Code Refactoring
Code smells
Bad Smell In Codes 1
Refactoring 2TheMax (con ReSharper)
Code refactoring workshop (in Javascript)
Code smells and remedies
Bade Smells in Code
Refactoring Chapter 6,7.pptx
Code Refactoring - 3.0
Refactoring Fest
Introduction to Refactoring
Refactoring
Refactoring, Therapeutic Attitude to Programming.
31 days Refactoring
Refactoring
Ad

More from Sangcheol Hwang (14)

PDF
Tech planet 2015 Docker 클라우드 구축 프로젝트 - d4
PDF
DevOps with Docker
PPTX
사티어 빙산의사소통 방법론
PDF
DevOpsday 2013 SiliconValley
PDF
Velocity 2013 Conference
PDF
Tdd retro agile_korea_게시용
PDF
협업도구 및 주요 Agile practices 적용사례 v1.0
PDF
Lean startupinpractice
PDF
Agile planning & estimating joshua partogi
PDF
Lab80 unicorn ranch-agilekorea2012
PDF
애자일하게스펙작성하기
PDF
애자일에대한오해와진실
PPT
칸반게임소개 20100810
PPT
애자일 아키텍트를 위한 툴 박스
Tech planet 2015 Docker 클라우드 구축 프로젝트 - d4
DevOps with Docker
사티어 빙산의사소통 방법론
DevOpsday 2013 SiliconValley
Velocity 2013 Conference
Tdd retro agile_korea_게시용
협업도구 및 주요 Agile practices 적용사례 v1.0
Lean startupinpractice
Agile planning & estimating joshua partogi
Lab80 unicorn ranch-agilekorea2012
애자일하게스펙작성하기
애자일에대한오해와진실
칸반게임소개 20100810
애자일 아키텍트를 위한 툴 박스

Recently uploaded (20)

PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
How AI Agents Improve Data Accuracy and Consistency in Due Diligence.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Sensors and Actuators in IoT Systems using pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
creating-agentic-ai-solutions-leveraging-aws.pdf
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
PPTX
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
PDF
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
PPTX
Web Security: Login Bypass, SQLi, CSRF & XSS.pptx
PPTX
CroxyProxy Instagram Access id login.pptx
PPTX
Belt and Road Supply Chain Finance Blockchain Solution
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Automating ArcGIS Content Discovery with FME: A Real World Use Case
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
How AI Agents Improve Data Accuracy and Consistency in Due Diligence.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Sensors and Actuators in IoT Systems using pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
creating-agentic-ai-solutions-leveraging-aws.pdf
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
Enable Enterprise-Ready Security on IBM i Systems.pdf
Web Security: Login Bypass, SQLi, CSRF & XSS.pptx
CroxyProxy Instagram Access id login.pptx
Belt and Road Supply Chain Finance Blockchain Solution
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...

Agile korea 2013 유석문

  • 1. 삶이 편해지는 쉬운 리팩토링(Refactoring) 유석문
  • 3. What is Refactoring? A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior. — Refactoring, Martin Fowler, page 53. Refactoring typically involves • Removing duplicated or dead code • Simplifying complex code • Clarifying unclear code
  • 4. Refactoring is revision All good writing is based upon revision. — Jacques Barzun, Simple & Direct, 4th Edition Revision means to re-see or see again.
  • 5. How to Refactor The secret to successful refactoring is to take baby steps. When refactoring, your ultimate goal is not to: • break anything • make things worse • introduce new behavior
  • 6. How to Refactor • Verify that all automated tests (unit tests) pass • Decide what code to change • Implement one or more refactorings carefully • Run the unit tests whenever you wish to confirm that changes ha ve not altered system behavior • Repeat until the refactoring is complete or revert to an earlier state
  • 8. A Chicken and Egg Problem? Many people, when they first encounter the concept of unit testing and refactoring legacy code, consider it a chicken-and-egg conundrum: Can't do one without having the other. Unit Testing Refactoring
  • 9. Start with the Universals Always start refactoring work with the three universals. 1. Reformat and Commit. Establish a baseline for later diffs. 2. Eliminate the Noise. A refactored file always minimizes noise and m aximizes signal. 3. Get To Testability. We're going to need some tests. Can we even st art to test our class?
  • 10. Consolidate Conditional Expression Clarity "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." — Martin Fowler, Refactoring
  • 11. Extract Hierarchy Highly Cohesive Methods do only one thing and classes have a single, clear responsibility.
  • 12. Extract Superclass Duplication Free A program should express each idea once and only once. Code may not always appear to be identical and yet may be expressing the same logic and information. Duplicate code may diverge to become out of sync
  • 13. Hide Method Encapsulated A form of information hiding, code that is encapsulated does not expose data or behavior that should be invisible
  • 14. Inline Method Simple Code reflects the shortest path to a solution and incorporates only enough complexity to handle its known responsibilities. Simple code is easier to maintain and evolve.
  • 15. Caller Creates When you need to create a new class or method that will be invoked by a caller, don't perform the creation where the new code will reside (e.g. in a new file or within the class that will contain the new method). Instead, declare the to-be-created class/method where it will be used, i.e. in the caller code. This will allow you to use IDE tools to automatically generate the exact co de needed by the client, instead of retrofitting client code to the new class/ method after creation.
  • 17. Rejected Parameter To avoid passing a parameter to code you want to extract, you must first rej ect the parameter from the extraction. The video on the next page presents three different ways to reject a parameter: 1. Inline the unwanted parameter. 2. Move the unwanted parameter out of the extraction block. 3. Turn the unwanted parameter into a field so it can be referenced from t he extracted method instead of being passed as a parameter.
  • 18. Move (Caller Swap) Move responsibilities from a caller to a parameter by calling the parameter with the original caller as its parameter.
  • 19. Encapsulated Dependency Before moving code to a new class, first encapsulate dependencies, like fields.
  • 20. Remove Duplication by Extract Method - Before
  • 23. Removing A Long Method Smell
  • 24. Removing A Long Method Smell (Composed Method)
  • 27. Change hierarchy A Map holds keys and values, whereas List and Set just hold objects.
  • 28. Duplicated Fields & Methods Duplicated Fields & Methods in List and Set AbstractCollection's method, addAll(...), contains the smells Long Method, Duplicated Code , Switch St atement and Alternative Classes With Different Interfa ces
  • 30. Don’t Fix Bugs during Refactoring A List should allow duplicates, yet in AbstractCollection your addAll(...) method is checking whether the element is already contained before adding it. There is a time and place to fix bugs and it is not during refactoring.
  • 31. Primitive Obsession In Map Map suffers from the Primitive Obsession smell because it wastes too much code manipulating the keys and values arrays.
  • 32. Scaffolding Builders make changes to buildings by first putting up scaffolding to make their workea sier and safer. Extract Method refactoring to produce: getValueAt(...) setValueAt(...) When you no longer need your scaffolding, you can remove it by applying the Inline Method refactoring.
  • 33. A Temporary Field In Map Map has a field called indexWhereKeyFound that is a p rime example of the Temporary Field Smell.