SlideShare a Scribd company logo
1 of 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!

More Related Content

Viewers also liked

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
 

Viewers also liked (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 to 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 to 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
 

Recently uploaded

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 

Recently uploaded (20)

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

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