SlideShare a Scribd company logo
1 of 33
Refactoring
Mehdi Einali
Advanced Programming in Java
1
2
Tale of Messy code
3
Once upon a time …
A team start a project
Project got many attention and team has to
add new features in short time
Programmer with overtime task: “I will fix
this later”
4
After a while
Changes slowed down by messy code
As productivity decreases more programmer
assigned to project
New programmer with messy code results in
more messy code
5
rebellion
Eventually the team rebels.
A new tiger team is selected
Best technologies has been chosen
Now the two teams are in a race
This race can go on for a very long time
Tiger team is now under pleasure of comparison
with old low feature but working version
Messy code again and again once upon a time
6
refactoring
7
Refactoring
A disciplined way to restructure code
in order to improve code quality
without changing its behavior
A change made to the internal
structure of software to make it easier
to understand and cheaper to modify
without changing its observable behavior.
8
Refactoring
Refactoring is the process of changing a
software system
In such a way that it does not alter the
external behavior of the code
But improves its internal structure
It is a disciplined way to clean up code
It minimizes the chances of introducing bugs
When you refactor, you are improving the
design of the code after it has been written.
9
Refactoring
By continuously improving the design of
code, we make it easier and easier to
work with
Joshua Kerievsky, Refactoring to Patterns
10
Example
Duplicate Code
What are the drawbacks?
What is the solution?
Refactoring:
Finding a “Bad Smell”
Changing the code to remove the bad smell
Some well-known bad smells are reported
11
Bad Smell
A bad smell in code
Any symptom in the source code that
possibly indicates a deeper problem.
The term is coined by Kent Beck.
12
Bad Smells
Duplicated Code
Long Method
Large Class
Long Parameter List
Divergent Change
…
13
Refactoring Techniques
Extract Method
Move
Method
Variable
Class
Extract Class
Rename
Method
Variable
Class
Pull Up
Push Down
14
IDE Support
Refactoring techniques are widely
supported by IDEs
15
The Two Hats
Kent Beck's metaphor of two hats
Divide your time between two distinct
activities
adding function
refactoring
16
Why Should I Refactor?
Improves the Design of Software
Makes Software Easier to Understand
Helps You Find Bugs
Helps You Program Faster
Refactoring makes your code more
maintainable
17
When Should You Refactor?
The Rule of Three:
Refactor When You Add Function
Refactor When You Need to Fix a
Bug
Refactor As You Do a Code Review
18
Scanner s = new Scanner(System.in);
System.out.println("Rectangle Info.");
System.out.print("Enter the width: ");
int a1 = s.nextInt();
System.out.print("Enter the length: ");
int a2 = s.nextInt();
System.out.println("Rectangle Info.");
System.out.print("Enter the width: ");
int b1 = s.nextInt();
System.out.print("Enter the length: ");
int b2 = s.nextInt();
int x = a1*a2;
int y = b1*b2;
if(x == y)
System.out.println("Equal");
Find bad smells!
Refactor the Code!
19
Scanner scanner = new Scanner(System.in);
System.out.println("Rectangle Info.");
System.out.print("Enter the width: ");
int width1 = scanner.nextInt();
System.out.print("Enter the length: ");
int length1 = scanner.nextInt();
System.out.println("Rectangle Info.");
System.out.print("Enter the width: ");
int width2 = scanner.nextInt();
System.out.print("Enter the length: ");
int length2 = scanner.nextInt();
int area1 = width1*length1;
int area2 = width2*length2;
if(area1 == area2)
System.out.println("Equal");
Rename…
20
class Rectangle{
private int length , width;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
}
Extract Class…
21
Scanner scanner = new Scanner(System.in);
System.out.println("Rectangle Info.");
System.out.print("Enter the width: ");
int width = scanner.nextInt();
System.out.print("Enter the length: ");
int length = scanner.nextInt();
Rectangle rectangle1 = new Rectangle(length, width);
System.out.println("Rectangle Info.");
System.out.print("Enter the width: ");
width = scanner.nextInt();
System.out.print("Enter the length: ");
length = scanner.nextInt();
Rectangle rectangle2 = new Rectangle(length, width);
int area1 = rectangle1.getWidth()*rectangle1.getLength();
int area2 = rectangle2.getWidth()*rectangle2.getLength();
if(area1 == area2)
System.out.println("Equal");
22
class Rectangle{
...
public int area(){
return length * width;
}
}
…
int area1 = rectangle1.area();
int area2 = rectangle2.area();
Extract Method…
23
private static Rectangle readRectangle(Scanner scanner) {
int width;
int length;
System.out.println("Rectangle Info.");
System.out.print("Enter the width: ");
width = scanner.nextInt();
System.out.print("Enter the length: ");
length = scanner.nextInt();
Rectangle rectangle2 = new Rectangle(length, width);
return rectangle2;
}
Extract Method…
24
Refactored Code
Scanner scanner = new Scanner(System.in);
Rectangle rectangle1 = readRectangle(scanner);
Rectangle rectangle2 = readRectangle(scanner);
int area1 = rectangle1.area();
int area2 = rectangle2.area();
if(area1 == area2)
System.out.println("Equal");
25
Clean code
26
Clean code does one thing well
Make it hard for bugs to hide
27
Reads like well-written prose
Never obscure the designer’s intent
28
Provides one way rather than
many ways for doing one thing
29
Each routine you read turn out to
be pretty much what you expect
30
(Conclusion)Clean code is
Make if hard for bugs to hide
Clean code does one thing well
Reads like well-written prose
Never obscure the designer’s intent
Provides one way rather than many ways
for doing one thing
Each routine you read turn out to be
pretty much what you expect
31
notes
Clean Programming is some thing like martial art
Combination of technique and art.
The Art of Computer Programming by Knuth
Have different school of thoughts
Clean Programming is skill
Good learning results in good use forever
Changing bad learning is hard
Like Driving!
32
Reference
Refactoring: improving the design of
existing code, Martin Fowler, Kent
Beck,John Brant, William Opdyke, Don
Roberts
(1999)
Clean code,A handbook of agile software
craftmanship,Robert C Martin,2008,
Prentice Hall
33

More Related Content

Similar to refactoring code by clean code rules

Analysis of bugs in Orchard CMS
Analysis of bugs in Orchard CMSAnalysis of bugs in Orchard CMS
Analysis of bugs in Orchard CMSPVS-Studio
 
MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...
MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...
MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...Dylan-Wu
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardMax Kleiner
 
Skiron - Experiments in CPU Design in D
Skiron - Experiments in CPU Design in DSkiron - Experiments in CPU Design in D
Skiron - Experiments in CPU Design in DMithun Hunsur
 
Jeremiah Yancy | Skills and techniques of the Systems Analyst
Jeremiah Yancy | Skills and techniques of the Systems AnalystJeremiah Yancy | Skills and techniques of the Systems Analyst
Jeremiah Yancy | Skills and techniques of the Systems AnalystJeremiah Yancy
 
Software reverse engineering
Software reverse engineeringSoftware reverse engineering
Software reverse engineeringParminder Singh
 
Week 4 Assignment - Software Development PlanScenario-Your team has be.docx
Week 4 Assignment - Software Development PlanScenario-Your team has be.docxWeek 4 Assignment - Software Development PlanScenario-Your team has be.docx
Week 4 Assignment - Software Development PlanScenario-Your team has be.docxestefana2345678
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Gianluca Padovani
 
Diving into VS 2015 Day2
Diving into VS 2015 Day2Diving into VS 2015 Day2
Diving into VS 2015 Day2Akhil Mittal
 
Programming Fundamentals lecture 3
Programming Fundamentals lecture 3Programming Fundamentals lecture 3
Programming Fundamentals lecture 3REHAN IJAZ
 
Multi step automated refactoring for code smell
Multi step automated refactoring for code smellMulti step automated refactoring for code smell
Multi step automated refactoring for code smelleSAT Publishing House
 
Multi step automated refactoring for code smell
Multi step automated refactoring for code smellMulti step automated refactoring for code smell
Multi step automated refactoring for code smelleSAT Journals
 
From Duke of DevOps to Queen of Chaos - Api days 2018
From Duke of DevOps to Queen of Chaos - Api days 2018From Duke of DevOps to Queen of Chaos - Api days 2018
From Duke of DevOps to Queen of Chaos - Api days 2018Christophe Rochefolle
 
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...Antonio de la Torre Fernández
 
What the hell is your software doing at runtime?
What the hell is your software doing at runtime?What the hell is your software doing at runtime?
What the hell is your software doing at runtime?Roberto Franchini
 
S D D Program Development Tools
S D D  Program  Development  ToolsS D D  Program  Development  Tools
S D D Program Development Toolsgavhays
 

Similar to refactoring code by clean code rules (20)

Analysis of bugs in Orchard CMS
Analysis of bugs in Orchard CMSAnalysis of bugs in Orchard CMS
Analysis of bugs in Orchard CMS
 
Refactoring
RefactoringRefactoring
Refactoring
 
MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...
MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...
MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO Standard
 
Skiron - Experiments in CPU Design in D
Skiron - Experiments in CPU Design in DSkiron - Experiments in CPU Design in D
Skiron - Experiments in CPU Design in D
 
Jeremiah Yancy | Skills and techniques of the Systems Analyst
Jeremiah Yancy | Skills and techniques of the Systems AnalystJeremiah Yancy | Skills and techniques of the Systems Analyst
Jeremiah Yancy | Skills and techniques of the Systems Analyst
 
Software reverse engineering
Software reverse engineeringSoftware reverse engineering
Software reverse engineering
 
Week 4 Assignment - Software Development PlanScenario-Your team has be.docx
Week 4 Assignment - Software Development PlanScenario-Your team has be.docxWeek 4 Assignment - Software Development PlanScenario-Your team has be.docx
Week 4 Assignment - Software Development PlanScenario-Your team has be.docx
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Diving into VS 2015 Day2
Diving into VS 2015 Day2Diving into VS 2015 Day2
Diving into VS 2015 Day2
 
Programming Fundamentals lecture 3
Programming Fundamentals lecture 3Programming Fundamentals lecture 3
Programming Fundamentals lecture 3
 
Raising the Bar
Raising the BarRaising the Bar
Raising the Bar
 
ArduinoWorkshop2.pdf
ArduinoWorkshop2.pdfArduinoWorkshop2.pdf
ArduinoWorkshop2.pdf
 
Multi step automated refactoring for code smell
Multi step automated refactoring for code smellMulti step automated refactoring for code smell
Multi step automated refactoring for code smell
 
Multi step automated refactoring for code smell
Multi step automated refactoring for code smellMulti step automated refactoring for code smell
Multi step automated refactoring for code smell
 
From Duke of DevOps to Queen of Chaos - Api days 2018
From Duke of DevOps to Queen of Chaos - Api days 2018From Duke of DevOps to Queen of Chaos - Api days 2018
From Duke of DevOps to Queen of Chaos - Api days 2018
 
Javascript.ppt
Javascript.pptJavascript.ppt
Javascript.ppt
 
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
 
What the hell is your software doing at runtime?
What the hell is your software doing at runtime?What the hell is your software doing at runtime?
What the hell is your software doing at runtime?
 
S D D Program Development Tools
S D D  Program  Development  ToolsS D D  Program  Development  Tools
S D D Program Development Tools
 

More from saber tabatabaee

clean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxclean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxsaber tabatabaee
 
هاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجی
هاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجیهاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجی
هاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجیsaber tabatabaee
 
لاراول ارائه 26 سپتامبر 2021 اسکایپ
لاراول ارائه 26 سپتامبر 2021 اسکایپلاراول ارائه 26 سپتامبر 2021 اسکایپ
لاراول ارائه 26 سپتامبر 2021 اسکایپsaber tabatabaee
 
scrum master اسکرام مستر
scrum master اسکرام مسترscrum master اسکرام مستر
scrum master اسکرام مسترsaber tabatabaee
 
Scrum master - daily scrum master story
Scrum master - daily scrum master storyScrum master - daily scrum master story
Scrum master - daily scrum master storysaber tabatabaee
 
clean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionclean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionsaber tabatabaee
 
teaching data science students to write clean code
teaching data science students to write clean codeteaching data science students to write clean code
teaching data science students to write clean codesaber tabatabaee
 
Writing clean scientific software Murphy cleancoding
Writing clean scientific software Murphy cleancodingWriting clean scientific software Murphy cleancoding
Writing clean scientific software Murphy cleancodingsaber tabatabaee
 
R. herves. clean code (theme)2
R. herves. clean code (theme)2R. herves. clean code (theme)2
R. herves. clean code (theme)2saber tabatabaee
 
sharepoint 2007 presentation in crcis
sharepoint 2007 presentation in crcis sharepoint 2007 presentation in crcis
sharepoint 2007 presentation in crcis saber tabatabaee
 
Linux DVD 03 Learnkey linux+ setup
Linux DVD 03 Learnkey linux+ setupLinux DVD 03 Learnkey linux+ setup
Linux DVD 03 Learnkey linux+ setupsaber tabatabaee
 

More from saber tabatabaee (18)

clean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxclean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
 
هاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجی
هاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجیهاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجی
هاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجی
 
لاراول ارائه 26 سپتامبر 2021 اسکایپ
لاراول ارائه 26 سپتامبر 2021 اسکایپلاراول ارائه 26 سپتامبر 2021 اسکایپ
لاراول ارائه 26 سپتامبر 2021 اسکایپ
 
scrum master اسکرام مستر
scrum master اسکرام مسترscrum master اسکرام مستر
scrum master اسکرام مستر
 
L5 swagger
L5 swaggerL5 swagger
L5 swagger
 
Crm or xrm
Crm or xrmCrm or xrm
Crm or xrm
 
Scrum master - daily scrum master story
Scrum master - daily scrum master storyScrum master - daily scrum master story
Scrum master - daily scrum master story
 
Online exam ismc
Online exam ismcOnline exam ismc
Online exam ismc
 
clean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionclean code book summary - uncle bob - English version
clean code book summary - uncle bob - English version
 
teaching data science students to write clean code
teaching data science students to write clean codeteaching data science students to write clean code
teaching data science students to write clean code
 
Writing clean scientific software Murphy cleancoding
Writing clean scientific software Murphy cleancodingWriting clean scientific software Murphy cleancoding
Writing clean scientific software Murphy cleancoding
 
R. herves. clean code (theme)2
R. herves. clean code (theme)2R. herves. clean code (theme)2
R. herves. clean code (theme)2
 
Clean code chpt_1
Clean code chpt_1Clean code chpt_1
Clean code chpt_1
 
Code quality
Code qualityCode quality
Code quality
 
clean code - uncle bob
clean code - uncle bobclean code - uncle bob
clean code - uncle bob
 
sharepoint 2007 presentation in crcis
sharepoint 2007 presentation in crcis sharepoint 2007 presentation in crcis
sharepoint 2007 presentation in crcis
 
Linux DVD 03 Learnkey linux+ setup
Linux DVD 03 Learnkey linux+ setupLinux DVD 03 Learnkey linux+ setup
Linux DVD 03 Learnkey linux+ setup
 
linux+ learnkey DVD 2
linux+ learnkey DVD 2 linux+ learnkey DVD 2
linux+ learnkey DVD 2
 

Recently uploaded

Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 

Recently uploaded (20)

Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 

refactoring code by clean code rules

  • 3. 3 Once upon a time … A team start a project Project got many attention and team has to add new features in short time Programmer with overtime task: “I will fix this later”
  • 4. 4 After a while Changes slowed down by messy code As productivity decreases more programmer assigned to project New programmer with messy code results in more messy code
  • 5. 5 rebellion Eventually the team rebels. A new tiger team is selected Best technologies has been chosen Now the two teams are in a race This race can go on for a very long time Tiger team is now under pleasure of comparison with old low feature but working version Messy code again and again once upon a time
  • 7. 7 Refactoring A disciplined way to restructure code in order to improve code quality without changing its behavior A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.
  • 8. 8 Refactoring Refactoring is the process of changing a software system In such a way that it does not alter the external behavior of the code But improves its internal structure It is a disciplined way to clean up code It minimizes the chances of introducing bugs When you refactor, you are improving the design of the code after it has been written.
  • 9. 9 Refactoring By continuously improving the design of code, we make it easier and easier to work with Joshua Kerievsky, Refactoring to Patterns
  • 10. 10 Example Duplicate Code What are the drawbacks? What is the solution? Refactoring: Finding a “Bad Smell” Changing the code to remove the bad smell Some well-known bad smells are reported
  • 11. 11 Bad Smell A bad smell in code Any symptom in the source code that possibly indicates a deeper problem. The term is coined by Kent Beck.
  • 12. 12 Bad Smells Duplicated Code Long Method Large Class Long Parameter List Divergent Change …
  • 13. 13 Refactoring Techniques Extract Method Move Method Variable Class Extract Class Rename Method Variable Class Pull Up Push Down
  • 14. 14 IDE Support Refactoring techniques are widely supported by IDEs
  • 15. 15 The Two Hats Kent Beck's metaphor of two hats Divide your time between two distinct activities adding function refactoring
  • 16. 16 Why Should I Refactor? Improves the Design of Software Makes Software Easier to Understand Helps You Find Bugs Helps You Program Faster Refactoring makes your code more maintainable
  • 17. 17 When Should You Refactor? The Rule of Three: Refactor When You Add Function Refactor When You Need to Fix a Bug Refactor As You Do a Code Review
  • 18. 18 Scanner s = new Scanner(System.in); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int a1 = s.nextInt(); System.out.print("Enter the length: "); int a2 = s.nextInt(); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int b1 = s.nextInt(); System.out.print("Enter the length: "); int b2 = s.nextInt(); int x = a1*a2; int y = b1*b2; if(x == y) System.out.println("Equal"); Find bad smells! Refactor the Code!
  • 19. 19 Scanner scanner = new Scanner(System.in); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int width1 = scanner.nextInt(); System.out.print("Enter the length: "); int length1 = scanner.nextInt(); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int width2 = scanner.nextInt(); System.out.print("Enter the length: "); int length2 = scanner.nextInt(); int area1 = width1*length1; int area2 = width2*length2; if(area1 == area2) System.out.println("Equal"); Rename…
  • 20. 20 class Rectangle{ private int length , width; public int getLength() { return length; } public void setLength(int length) { this.length = length; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public Rectangle(int length, int width) { this.length = length; this.width = width; } } Extract Class…
  • 21. 21 Scanner scanner = new Scanner(System.in); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int width = scanner.nextInt(); System.out.print("Enter the length: "); int length = scanner.nextInt(); Rectangle rectangle1 = new Rectangle(length, width); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); width = scanner.nextInt(); System.out.print("Enter the length: "); length = scanner.nextInt(); Rectangle rectangle2 = new Rectangle(length, width); int area1 = rectangle1.getWidth()*rectangle1.getLength(); int area2 = rectangle2.getWidth()*rectangle2.getLength(); if(area1 == area2) System.out.println("Equal");
  • 22. 22 class Rectangle{ ... public int area(){ return length * width; } } … int area1 = rectangle1.area(); int area2 = rectangle2.area(); Extract Method…
  • 23. 23 private static Rectangle readRectangle(Scanner scanner) { int width; int length; System.out.println("Rectangle Info."); System.out.print("Enter the width: "); width = scanner.nextInt(); System.out.print("Enter the length: "); length = scanner.nextInt(); Rectangle rectangle2 = new Rectangle(length, width); return rectangle2; } Extract Method…
  • 24. 24 Refactored Code Scanner scanner = new Scanner(System.in); Rectangle rectangle1 = readRectangle(scanner); Rectangle rectangle2 = readRectangle(scanner); int area1 = rectangle1.area(); int area2 = rectangle2.area(); if(area1 == area2) System.out.println("Equal");
  • 26. 26 Clean code does one thing well Make it hard for bugs to hide
  • 27. 27 Reads like well-written prose Never obscure the designer’s intent
  • 28. 28 Provides one way rather than many ways for doing one thing
  • 29. 29 Each routine you read turn out to be pretty much what you expect
  • 30. 30 (Conclusion)Clean code is Make if hard for bugs to hide Clean code does one thing well Reads like well-written prose Never obscure the designer’s intent Provides one way rather than many ways for doing one thing Each routine you read turn out to be pretty much what you expect
  • 31. 31 notes Clean Programming is some thing like martial art Combination of technique and art. The Art of Computer Programming by Knuth Have different school of thoughts Clean Programming is skill Good learning results in good use forever Changing bad learning is hard Like Driving!
  • 32. 32 Reference Refactoring: improving the design of existing code, Martin Fowler, Kent Beck,John Brant, William Opdyke, Don Roberts (1999) Clean code,A handbook of agile software craftmanship,Robert C Martin,2008, Prentice Hall
  • 33. 33