SlideShare una empresa de Scribd logo
1 de 30
Introduction to Refactoring
About Me VorleakChy Email (vorleak.chy@gmail.com) Yoolk Inc. (http://www.yoolk.com) Blog (http://vorleakchy.com) Rails Developer .NET Developer
What do we talk about Refactoring? What?  Why?  When?  How?
Get Agile – Refactoring Practices Tools
What is Refactoring? The process of changing a software system in such a way that it does not alter the external behavior of the code, yet improves its internal structure. Fowler, et al., Refactoring, 1999.
Before After Easier to understand Clean code Better code Cheaper to modify … More Unreadable code Duplicated code Complex code Hard to modify … More
Why do we Refactor? The reality Extremely difficult to get the design “right” the first time Hard to fully understand the problem domain Hard to understand user requirements, even if the user does! Hard to know how the system will evolve in five years Original design is often inadequate System becomes brittle over time, and more difficult to change
Why do we Refactor? (Cont.) Helps us to deliver more business values faster Improve code structure and design Easier to maintain and understand Easier to modify More flexibility Easier to add new features Increased re-usability
Why do we Refactor?(Cont.) Keep development at speed To make the software easier to understand Write for people, not the compiler Understand unfamiliar code To help us find bugs Refactor while debugging to clarify the code
Readability Which code segment is easier to read? Sample 1 if (date.before(Summer_Start) || date.after(Summer_End)) 	charge = quantity * winterRate + winterServiceCharge; else 	charge = quantity * summerRate; Sample 2 if (isSummer(date)) 	charge = summerCharge(quantity); else 	charge = winterCharge(quantity);
When should werefactor? Add new features Fix bugs During code review Only refactor when refactoring. Do not add features during refactoring
When Not to Refactor Sometimes you should throw things out and start again Sometimes you are too close to a deadline
Costs of Not Refactoring Bad code usually takes more code to do the same thing often because of duplication
How do we Refactor? We looks for Code-Smells Things that we suspect are not quite right or will cause us severe pain if we do not fix
Common Code Smells Duplicated code Feature Envy Comments Long Method Long parameter list Switch Statements
Duplicated code There is obvious duplication Such as copy and paste There are unobvious duplications Such as parallel inheritance hierarchies Similar algorithms Remedy Extract Method Pull Up Field
Feature Envy A method that seems more interested in some other classes than the one it is in Remedy Move Method Extract Method
Extract Method void printOwning(double amount){ printBanner(); 	// print details System.Console.WriteLine(string.Format(“name: “, name); System.Console.WriteLine(string.Format(“amount: “, amount); } void printOwning(double amount){ printBanner(); printDetails(amount); } void printDetails(double amount){ System.Console.WriteLine(string.Format(“name: “, name); System.Console.WriteLine(string.Format(“amount: “, amount); }
Comments Comments – be suspicious! Comments represent a failure to express an idea in the code Remedy Extract Method Rename Method
Long Method Good OO code is easiest to understand and maintain with shorter methods with good names Long methods tend to be harder to read and understand Remedy Extract Method Replace Temp with Query Replace Method with Method Object Decompose Conditional
Replace Temp with Query double basePrice = _quanity* 		_itemPrice; if(basePrice> 1000) { 	return basePrice * 0.95; } else { 	return basePrice * 0.98; } if(getBasePrice() > 1000) { 	return getBasePrice() * 0.95; } else { 	return getBasePrice() * 0.98; } double getBasePrice() { 	return _quanitiy * _itemPrice; }
Long parameter list Functions should have as few parameters as possible Remedy Replace Parameter with Method Preserve Whole Object Introduce Parameter Object
Introduce Parameter Object Customer Customer amountInvoicedIn(Date start, Date end) amountRecivedIn(Date start, Date end) amountOverdueIn(Date start, Date end) amountInvoicedIn(DateRangerange) amountRecivedIn(DateRangerange) amountOverdueIn(DateRangerange)
Switch Statements Type cases are evil because they tend to be duplicated many times Remedy Replace Type Code with Subclasses Replace Type Code with State / Strategy Replace Conditional with Polymorphism Replace Parameter with Explicit Methods Introduce Null Object
Replace Parameter with Explicit Methods void setValue(String name, int value) { 	if(name.Equals(“height”)) 	{ _height = value; return; 	} 	if(name.Equals(“width”)) 	{ 	         _width = value;                  return;          } } void setHeight(intvalue) { 	_height = value; } void setWidth(intvalue) { 	_width = value; }
Refactoring Cycle Choose on appropriate Refactoring to apply Apply Refactoring Run ALL Tests (Get GREEN Bar) Reached Desired Structure? No Yes
Demo
Q & A
Essential Reading
Thank you!

Más contenido relacionado

Destacado

Method-Level Code Clone Modification using Refactoring Techniques for Clone M...
Method-Level Code Clone Modification using Refactoring Techniques for Clone M...Method-Level Code Clone Modification using Refactoring Techniques for Clone M...
Method-Level Code Clone Modification using Refactoring Techniques for Clone M...acijjournal
 
Patricia Deshane - PhD Dissertation
Patricia Deshane - PhD DissertationPatricia Deshane - PhD Dissertation
Patricia Deshane - PhD DissertationPatricia Deshane
 
E-LEARNING FOR ALL WITH INTERFACE INCORPORATING KNOWLEDGE ABOUT USER
E-LEARNING FOR ALL WITH INTERFACE INCORPORATING KNOWLEDGE ABOUT USER E-LEARNING FOR ALL WITH INTERFACE INCORPORATING KNOWLEDGE ABOUT USER
E-LEARNING FOR ALL WITH INTERFACE INCORPORATING KNOWLEDGE ABOUT USER acijjournal
 

Destacado (8)

PhD Dissertation
PhD DissertationPhD Dissertation
PhD Dissertation
 
FSE DS
FSE DSFSE DS
FSE DS
 
Method-Level Code Clone Modification using Refactoring Techniques for Clone M...
Method-Level Code Clone Modification using Refactoring Techniques for Clone M...Method-Level Code Clone Modification using Refactoring Techniques for Clone M...
Method-Level Code Clone Modification using Refactoring Techniques for Clone M...
 
ICPC
ICPCICPC
ICPC
 
Patricia Deshane - PhD Dissertation
Patricia Deshane - PhD DissertationPatricia Deshane - PhD Dissertation
Patricia Deshane - PhD Dissertation
 
ICPC Demo
ICPC DemoICPC Demo
ICPC Demo
 
PhD Proposal
PhD ProposalPhD Proposal
PhD Proposal
 
E-LEARNING FOR ALL WITH INTERFACE INCORPORATING KNOWLEDGE ABOUT USER
E-LEARNING FOR ALL WITH INTERFACE INCORPORATING KNOWLEDGE ABOUT USER E-LEARNING FOR ALL WITH INTERFACE INCORPORATING KNOWLEDGE ABOUT USER
E-LEARNING FOR ALL WITH INTERFACE INCORPORATING KNOWLEDGE ABOUT USER
 

Similar a Introduction to Refactoring

Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerIgor Crvenov
 
Agile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tddAgile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tddSrinivasa GV
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And RefactoringNaresh Jain
 
Refactoring PHP
Refactoring PHPRefactoring PHP
Refactoring PHPAdam Culp
 
Software Craftsmanship - 2
Software Craftsmanship - 2Software Craftsmanship - 2
Software Craftsmanship - 2Uri Lavi
 
Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeValerio Maggio
 
Ddc2011 효과적으로레거시코드다루기
Ddc2011 효과적으로레거시코드다루기Ddc2011 효과적으로레거시코드다루기
Ddc2011 효과적으로레거시코드다루기Myeongseok Baek
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoringkim.mens
 
Bad Smell in Codes - Part 1
Bad Smell in Codes - Part 1Bad Smell in Codes - Part 1
Bad Smell in Codes - Part 1Shaer Hassan
 
Getting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataGetting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataCory Foy
 
Refactoring and code smells
Refactoring and code smellsRefactoring and code smells
Refactoring and code smellsPaul Nguyen
 
En story of cakephp2.0
En story of cakephp2.0En story of cakephp2.0
En story of cakephp2.0Hiroki Shimizu
 
Naming Standards, Clean Code
Naming Standards, Clean CodeNaming Standards, Clean Code
Naming Standards, Clean CodeCleanestCode
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2Hammad Rajjoub
 

Similar a Introduction to Refactoring (20)

Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
SAD10 - Refactoring
SAD10 - RefactoringSAD10 - Refactoring
SAD10 - Refactoring
 
Refactoring
RefactoringRefactoring
Refactoring
 
Agile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tddAgile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tdd
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
Refactoring PHP
Refactoring PHPRefactoring PHP
Refactoring PHP
 
Software Craftsmanship - 2
Software Craftsmanship - 2Software Craftsmanship - 2
Software Craftsmanship - 2
 
Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing code
 
Refactoring
RefactoringRefactoring
Refactoring
 
Tech talks#6: Code Refactoring
Tech talks#6: Code RefactoringTech talks#6: Code Refactoring
Tech talks#6: Code Refactoring
 
Composing method
Composing methodComposing method
Composing method
 
Ddc2011 효과적으로레거시코드다루기
Ddc2011 효과적으로레거시코드다루기Ddc2011 효과적으로레거시코드다루기
Ddc2011 효과적으로레거시코드다루기
 
Refactoring
RefactoringRefactoring
Refactoring
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
Bad Smell in Codes - Part 1
Bad Smell in Codes - Part 1Bad Smell in Codes - Part 1
Bad Smell in Codes - Part 1
 
Getting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataGetting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and Data
 
Refactoring and code smells
Refactoring and code smellsRefactoring and code smells
Refactoring and code smells
 
En story of cakephp2.0
En story of cakephp2.0En story of cakephp2.0
En story of cakephp2.0
 
Naming Standards, Clean Code
Naming Standards, Clean CodeNaming Standards, Clean Code
Naming Standards, Clean Code
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
 

Último

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 

Último (20)

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 

Introduction to Refactoring

  • 2. About Me VorleakChy Email (vorleak.chy@gmail.com) Yoolk Inc. (http://www.yoolk.com) Blog (http://vorleakchy.com) Rails Developer .NET Developer
  • 3. What do we talk about Refactoring? What? Why? When? How?
  • 4. Get Agile – Refactoring Practices Tools
  • 5. What is Refactoring? The process of changing a software system in such a way that it does not alter the external behavior of the code, yet improves its internal structure. Fowler, et al., Refactoring, 1999.
  • 6. Before After Easier to understand Clean code Better code Cheaper to modify … More Unreadable code Duplicated code Complex code Hard to modify … More
  • 7. Why do we Refactor? The reality Extremely difficult to get the design “right” the first time Hard to fully understand the problem domain Hard to understand user requirements, even if the user does! Hard to know how the system will evolve in five years Original design is often inadequate System becomes brittle over time, and more difficult to change
  • 8. Why do we Refactor? (Cont.) Helps us to deliver more business values faster Improve code structure and design Easier to maintain and understand Easier to modify More flexibility Easier to add new features Increased re-usability
  • 9. Why do we Refactor?(Cont.) Keep development at speed To make the software easier to understand Write for people, not the compiler Understand unfamiliar code To help us find bugs Refactor while debugging to clarify the code
  • 10. Readability Which code segment is easier to read? Sample 1 if (date.before(Summer_Start) || date.after(Summer_End)) charge = quantity * winterRate + winterServiceCharge; else charge = quantity * summerRate; Sample 2 if (isSummer(date)) charge = summerCharge(quantity); else charge = winterCharge(quantity);
  • 11. When should werefactor? Add new features Fix bugs During code review Only refactor when refactoring. Do not add features during refactoring
  • 12. When Not to Refactor Sometimes you should throw things out and start again Sometimes you are too close to a deadline
  • 13. Costs of Not Refactoring Bad code usually takes more code to do the same thing often because of duplication
  • 14. How do we Refactor? We looks for Code-Smells Things that we suspect are not quite right or will cause us severe pain if we do not fix
  • 15. Common Code Smells Duplicated code Feature Envy Comments Long Method Long parameter list Switch Statements
  • 16. Duplicated code There is obvious duplication Such as copy and paste There are unobvious duplications Such as parallel inheritance hierarchies Similar algorithms Remedy Extract Method Pull Up Field
  • 17. Feature Envy A method that seems more interested in some other classes than the one it is in Remedy Move Method Extract Method
  • 18. Extract Method void printOwning(double amount){ printBanner(); // print details System.Console.WriteLine(string.Format(“name: “, name); System.Console.WriteLine(string.Format(“amount: “, amount); } void printOwning(double amount){ printBanner(); printDetails(amount); } void printDetails(double amount){ System.Console.WriteLine(string.Format(“name: “, name); System.Console.WriteLine(string.Format(“amount: “, amount); }
  • 19. Comments Comments – be suspicious! Comments represent a failure to express an idea in the code Remedy Extract Method Rename Method
  • 20. Long Method Good OO code is easiest to understand and maintain with shorter methods with good names Long methods tend to be harder to read and understand Remedy Extract Method Replace Temp with Query Replace Method with Method Object Decompose Conditional
  • 21. Replace Temp with Query double basePrice = _quanity* _itemPrice; if(basePrice> 1000) { return basePrice * 0.95; } else { return basePrice * 0.98; } if(getBasePrice() > 1000) { return getBasePrice() * 0.95; } else { return getBasePrice() * 0.98; } double getBasePrice() { return _quanitiy * _itemPrice; }
  • 22. Long parameter list Functions should have as few parameters as possible Remedy Replace Parameter with Method Preserve Whole Object Introduce Parameter Object
  • 23. Introduce Parameter Object Customer Customer amountInvoicedIn(Date start, Date end) amountRecivedIn(Date start, Date end) amountOverdueIn(Date start, Date end) amountInvoicedIn(DateRangerange) amountRecivedIn(DateRangerange) amountOverdueIn(DateRangerange)
  • 24. Switch Statements Type cases are evil because they tend to be duplicated many times Remedy Replace Type Code with Subclasses Replace Type Code with State / Strategy Replace Conditional with Polymorphism Replace Parameter with Explicit Methods Introduce Null Object
  • 25. Replace Parameter with Explicit Methods void setValue(String name, int value) { if(name.Equals(“height”)) { _height = value; return; } if(name.Equals(“width”)) { _width = value; return; } } void setHeight(intvalue) { _height = value; } void setWidth(intvalue) { _width = value; }
  • 26. Refactoring Cycle Choose on appropriate Refactoring to apply Apply Refactoring Run ALL Tests (Get GREEN Bar) Reached Desired Structure? No Yes
  • 27. Demo
  • 28. Q & A