SlideShare a Scribd company logo
1 of 27
Download to read offline
CLEANER CODE
REFACTORING PRACTICE
REFACTORING PRACTICES: CLEANER CODE
TABLE OF CONTENTS
▸ Motivation
▸ What is CLEAN CODE?
▸ Refactoring Practices
▸ Practice Scope
▸ Practice1: NumberAnalysis.java
▸ Practice2: TicTacToe.java
▸ Lessons Learned
2
MOTIVATION
▸ For deeper understanding of OOP and java
▸ Curious about more advanced coding styles
I searched proper references and chose the book!
Clean Code
A Handbook of Agile Software Craftsmanship
by Robert Cecil Martin
3REFACTORING PRACTICES: CLEANER CODE
WHAT IS CLEAN CODE? (1/2)
▸ What is defined as clean code differs from each individual
but the base is shown below:
CLEAN CODE
straightforward
easy to read
easy to understand
easy to maintain
good performance
lower dependency
higher cohesiveness
meaningful name
…
easy to enhance
…
4REFACTORING PRACTICES: CLEANER CODE
WHAT IS CLEAN CODE? (2/2)
▸ Martin explains the concept of clean code in a simple
manner, just like how the code should be.
▸ Clean code starts from variable and method names that
describes its function, each method having only one
function. This way, even the most complex class can be
easily read when put all together.
▸ Any code can be converted into clean code with a little bit
of refactoring.
5REFACTORING PRACTICES: CLEANER CODE
REFACTORING
PRACTICES
PRACTICE SCOPE
▸ Easily-read statements
▸ Intention-revealing naming that follows Java’s naming
conventions
▸ Simple methods
▸ Single responsibility for each Class
▸ Revealing OO-concepts in codes
7REFACTORING PRACTICES: CLEANER CODE
PRACTICE 1
ANALYSING NUMBERS
PRACTICE 1
ANALYSING NUMBERS
▸ Program Purpose: AP Computer Science homework
intended to practice a wide array of Java concepts
▸ Requirements:
1. It should be written in the form of a console program.
2. Various kinds of operations should be provided.
3. Operation selection should be repetitive.
9
PRACTICE 1
ANALYSING NUMBERS
▸ Example of Program Execution
10
Input from keyboard
Operation choice
Operation choice
PRACTICE 1
CLASS DIAGRAM OF INITIAL CODE
11
A list of numbers to be calculated
An array for making histogram
An array for obtaining modulus
Displaying repetitive prompts
PRACTICE 1
MAIN() METHOD
Keep storing numbers
from keyboard
until the value is zero
Make a choice and
display the result
PRACTICE 1
PROBLEMS (1/2)
13
Vague Naming
Method names
not following Java naming convention
Wrong visibility
PRACTICE 1
PROBLEMS (2/2)
14
Hard to understand
Hard to read
Indistinct identifier
PRACTICE 1
REFACTORED
▸ Class Diagram
15
Class name, variables and methods
renamed
Methods extracted
Visibility corrected
Constant added
PRACTICE 1
REFACTORED
▸ main() and public method run()
16
Test code with high readability
by extracting a method
and correcting visibility.
Now,
fillNumberArray() and run() are visible
in main() method and
run() uses the private methods inside.
PRACTICE 2
TIC-TAC-TOE
PRACTICE 2
TIC-TAC-TOE
▸ Requirements
1. Display the game board and the respected crosses
and noughts with symbols.
2. Game continues until a player wins or ties.
18
PRACTICE 2
TIC-TAC-TOE
▸ Example of Program Execution
19
Initial game board
Pick a row
and column
in turn
Game End
and
display a board
PRACTICE 2
CLASS DIAGRAM OF INITIAL IMPLEMENTATION
20
A constructor initialising and print
the game board
A method that checks to see if a player has won or tied
An array that contains the game board
PRACTICE 2
PROBLEMS (1/2)
21
Unclear variable name
Different functions contained
in one method
Method name
breaking naming convention
PRACTICE 2
PROBLEMS (2/2)
22
Bug latent
I found the bug that already-chosen cell can be replaced
in the refactoring process.
PRACTICE 2
CLASS DIAGRAM OF CODE REFACTORED
23
Constant variables created
The board and the parameters renamed Methods split so each has one function
Class dissection
PRACTICE 2
HOWEVER, I THOUGHT..
24
TicTacToe.java still seems heavy.
Local variables might not be necessary.
Ways to eliminate error-prone code?
Is the class dissection right?
…
PRACTICE 2
REFACTORING ONCE AGAIN
25
+ Three valid states
+ Clearer relations among classes
+ More understandable classes
and its role
Now,
ü it became more
understandable and
maintainable structure.
ü I made reusable classes.
ü Unintentionally I fixed a bug.
LESSONS LEARNED (1/2)
▸ I had a chance to review bad code smells in my code.
▸ In the refactored code,
▸ methods and classes are much easier to read.
▸ the sub methods are easier to maintain.
▸ code becomes easier to read and understand without
the need of comments.
▸ code becomes maintainable for future bugs and/or
tweaks.
26REFACTORING PRACTICES: CLEANER CODE
LESSONS LEARNED (2/2)
▸ To keep coming back to the code and revise it makes
better code.
▸ Refactoring can be those important and meaningful
practice.
▸ Keep in mind “The Boy Scout Rule”
27REFACTORING PRACTICES: CLEANER CODE

More Related Content

What's hot

Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaMOHIT AGARWAL
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-javaDeepak Singh
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classesAnup Burange
 
Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceOum Saokosal
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentSuresh Mohta
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaEdureka!
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interfacemanish kumar
 
Object Oriended Programming with Java
Object Oriended Programming with JavaObject Oriended Programming with Java
Object Oriended Programming with JavaJakir Hossain
 
Access Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and EncapsulationAccess Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and EncapsulationAbid Kohistani
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces Tuan Ngo
 
Oop r&s may 2019 (2)
Oop r&s may 2019 (2)Oop r&s may 2019 (2)
Oop r&s may 2019 (2)ktuonlinenotes
 

What's hot (20)

Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
 
Oop r&s may 2019
Oop r&s may 2019Oop r&s may 2019
Oop r&s may 2019
 
Xamarin: C# Methods
Xamarin: C# MethodsXamarin: C# Methods
Xamarin: C# Methods
 
Java interface
Java interfaceJava interface
Java interface
 
Delphi qa
Delphi qaDelphi qa
Delphi qa
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Java interface
Java interfaceJava interface
Java interface
 
Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and Interface
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | Edureka
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
Object Oriended Programming with Java
Object Oriended Programming with JavaObject Oriended Programming with Java
Object Oriended Programming with Java
 
Access Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and EncapsulationAccess Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and Encapsulation
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
 
Oop r&s may 2019 (2)
Oop r&s may 2019 (2)Oop r&s may 2019 (2)
Oop r&s may 2019 (2)
 
Interface
InterfaceInterface
Interface
 

Viewers also liked

Some OOP paradigms & SOLID
Some OOP paradigms & SOLIDSome OOP paradigms & SOLID
Some OOP paradigms & SOLIDJulio Martinez
 
Introduction to Clean Code
Introduction to Clean CodeIntroduction to Clean Code
Introduction to Clean CodeJulio Martinez
 
Introduction to Docker
Introduction to DockerIntroduction to Docker
Introduction to DockerJulio Martinez
 
Introduction to hexagonal architecture
Introduction to hexagonal architectureIntroduction to hexagonal architecture
Introduction to hexagonal architectureManel Sellés
 
Conclusion of the Seminary UPC 2017
Conclusion of the Seminary UPC 2017Conclusion of the Seminary UPC 2017
Conclusion of the Seminary UPC 2017Julio Martinez
 
(DVO401) Deep Dive into Blue/Green Deployments on AWS
(DVO401) Deep Dive into Blue/Green Deployments on AWS(DVO401) Deep Dive into Blue/Green Deployments on AWS
(DVO401) Deep Dive into Blue/Green Deployments on AWSAmazon Web Services
 
การใช้เทคโนโลยีสารสนเทศนำเสนองาน
การใช้เทคโนโลยีสารสนเทศนำเสนองานการใช้เทคโนโลยีสารสนเทศนำเสนองาน
การใช้เทคโนโลยีสารสนเทศนำเสนองานtanachot1898
 
Portfolio_Josh_White_Spread_Print_compressed2
Portfolio_Josh_White_Spread_Print_compressed2Portfolio_Josh_White_Spread_Print_compressed2
Portfolio_Josh_White_Spread_Print_compressed2Josh White
 
Azhar_JavaDeveloper_2.8yrsExp
Azhar_JavaDeveloper_2.8yrsExpAzhar_JavaDeveloper_2.8yrsExp
Azhar_JavaDeveloper_2.8yrsExpAzhar Glb
 
Development pro forma
Development pro formaDevelopment pro forma
Development pro formaBHuyton10
 
土地貸款借款利率說明
土地貸款借款利率說明土地貸款借款利率說明
土地貸款借款利率說明Lianying Win
 
Profile J.Edwards 2015
Profile J.Edwards 2015Profile J.Edwards 2015
Profile J.Edwards 2015Jeff Edwards
 
Harga maika etnik 2016
Harga maika etnik 2016Harga maika etnik 2016
Harga maika etnik 2016Dawang Sirita
 

Viewers also liked (20)

Java 7 New Features
Java 7 New FeaturesJava 7 New Features
Java 7 New Features
 
Some OOP paradigms & SOLID
Some OOP paradigms & SOLIDSome OOP paradigms & SOLID
Some OOP paradigms & SOLID
 
Introduction to Clean Code
Introduction to Clean CodeIntroduction to Clean Code
Introduction to Clean Code
 
Introduction to Docker
Introduction to DockerIntroduction to Docker
Introduction to Docker
 
Introduction to hexagonal architecture
Introduction to hexagonal architectureIntroduction to hexagonal architecture
Introduction to hexagonal architecture
 
Conclusion of the Seminary UPC 2017
Conclusion of the Seminary UPC 2017Conclusion of the Seminary UPC 2017
Conclusion of the Seminary UPC 2017
 
55 New Features in Java 7
55 New Features in Java 755 New Features in Java 7
55 New Features in Java 7
 
(DVO401) Deep Dive into Blue/Green Deployments on AWS
(DVO401) Deep Dive into Blue/Green Deployments on AWS(DVO401) Deep Dive into Blue/Green Deployments on AWS
(DVO401) Deep Dive into Blue/Green Deployments on AWS
 
Nurul arganindya
Nurul arganindyaNurul arganindya
Nurul arganindya
 
Navidad
NavidadNavidad
Navidad
 
การใช้เทคโนโลยีสารสนเทศนำเสนองาน
การใช้เทคโนโลยีสารสนเทศนำเสนองานการใช้เทคโนโลยีสารสนเทศนำเสนองาน
การใช้เทคโนโลยีสารสนเทศนำเสนองาน
 
Portfolio_Josh_White_Spread_Print_compressed2
Portfolio_Josh_White_Spread_Print_compressed2Portfolio_Josh_White_Spread_Print_compressed2
Portfolio_Josh_White_Spread_Print_compressed2
 
Romance debunked
Romance debunkedRomance debunked
Romance debunked
 
Azhar_JavaDeveloper_2.8yrsExp
Azhar_JavaDeveloper_2.8yrsExpAzhar_JavaDeveloper_2.8yrsExp
Azhar_JavaDeveloper_2.8yrsExp
 
Internet nuevo.
Internet  nuevo.Internet  nuevo.
Internet nuevo.
 
Development pro forma
Development pro formaDevelopment pro forma
Development pro forma
 
土地貸款借款利率說明
土地貸款借款利率說明土地貸款借款利率說明
土地貸款借款利率說明
 
Faisal Abbas cv
Faisal Abbas cvFaisal Abbas cv
Faisal Abbas cv
 
Profile J.Edwards 2015
Profile J.Edwards 2015Profile J.Edwards 2015
Profile J.Edwards 2015
 
Harga maika etnik 2016
Harga maika etnik 2016Harga maika etnik 2016
Harga maika etnik 2016
 

Similar to Refactoring Practice: Cleaner Code

Agile korea 2013 유석문
Agile korea 2013 유석문Agile korea 2013 유석문
Agile korea 2013 유석문Sangcheol Hwang
 
Code sense
Code senseCode sense
Code sensenasirj
 
Bad Code Smells
Bad Code SmellsBad Code Smells
Bad Code Smellskim.mens
 
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
 
Implementation
ImplementationImplementation
Implementationadil raja
 
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
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)Jay Patel
 
Perfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standardsPerfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standardsPerfomatix Solutions
 
Mit4021–%20 c# and .net
Mit4021–%20 c# and .netMit4021–%20 c# and .net
Mit4021–%20 c# and .netsmumbahelp
 
Journey's diary developing a framework using tdd
Journey's diary   developing a framework using tddJourney's diary   developing a framework using tdd
Journey's diary developing a framework using tddeduardomg23
 
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
 
Programming in Java: Why Object-Orientation?
Programming in Java: Why Object-Orientation?Programming in Java: Why Object-Orientation?
Programming in Java: Why Object-Orientation?Martin Chapman
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringEyob Lube
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And RefactoringNaresh Jain
 
Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basicsvamshimahi
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerIgor Crvenov
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaHamad Odhabi
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoringkim.mens
 

Similar to Refactoring Practice: Cleaner Code (20)

Agile korea 2013 유석문
Agile korea 2013 유석문Agile korea 2013 유석문
Agile korea 2013 유석문
 
Code sense
Code senseCode sense
Code sense
 
Bad Code Smells
Bad Code SmellsBad Code Smells
Bad Code Smells
 
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
 
Implementation
ImplementationImplementation
Implementation
 
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
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
 
Perfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standardsPerfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standards
 
Mit4021–%20 c# and .net
Mit4021–%20 c# and .netMit4021–%20 c# and .net
Mit4021–%20 c# and .net
 
Journey's diary developing a framework using tdd
Journey's diary   developing a framework using tddJourney's diary   developing a framework using tdd
Journey's diary developing a framework using tdd
 
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 - ...
 
Programming in Java: Why Object-Orientation?
Programming in Java: Why Object-Orientation?Programming in Java: Why Object-Orientation?
Programming in Java: Why Object-Orientation?
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoring
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basics
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
C# interview questions
C# interview questionsC# interview questions
C# interview questions
 

Recently uploaded

An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxPurva Nikam
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 

Recently uploaded (20)

An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptx
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 

Refactoring Practice: Cleaner Code

  • 2. REFACTORING PRACTICES: CLEANER CODE TABLE OF CONTENTS ▸ Motivation ▸ What is CLEAN CODE? ▸ Refactoring Practices ▸ Practice Scope ▸ Practice1: NumberAnalysis.java ▸ Practice2: TicTacToe.java ▸ Lessons Learned 2
  • 3. MOTIVATION ▸ For deeper understanding of OOP and java ▸ Curious about more advanced coding styles I searched proper references and chose the book! Clean Code A Handbook of Agile Software Craftsmanship by Robert Cecil Martin 3REFACTORING PRACTICES: CLEANER CODE
  • 4. WHAT IS CLEAN CODE? (1/2) ▸ What is defined as clean code differs from each individual but the base is shown below: CLEAN CODE straightforward easy to read easy to understand easy to maintain good performance lower dependency higher cohesiveness meaningful name … easy to enhance … 4REFACTORING PRACTICES: CLEANER CODE
  • 5. WHAT IS CLEAN CODE? (2/2) ▸ Martin explains the concept of clean code in a simple manner, just like how the code should be. ▸ Clean code starts from variable and method names that describes its function, each method having only one function. This way, even the most complex class can be easily read when put all together. ▸ Any code can be converted into clean code with a little bit of refactoring. 5REFACTORING PRACTICES: CLEANER CODE
  • 7. PRACTICE SCOPE ▸ Easily-read statements ▸ Intention-revealing naming that follows Java’s naming conventions ▸ Simple methods ▸ Single responsibility for each Class ▸ Revealing OO-concepts in codes 7REFACTORING PRACTICES: CLEANER CODE
  • 9. PRACTICE 1 ANALYSING NUMBERS ▸ Program Purpose: AP Computer Science homework intended to practice a wide array of Java concepts ▸ Requirements: 1. It should be written in the form of a console program. 2. Various kinds of operations should be provided. 3. Operation selection should be repetitive. 9
  • 10. PRACTICE 1 ANALYSING NUMBERS ▸ Example of Program Execution 10 Input from keyboard Operation choice Operation choice
  • 11. PRACTICE 1 CLASS DIAGRAM OF INITIAL CODE 11 A list of numbers to be calculated An array for making histogram An array for obtaining modulus Displaying repetitive prompts
  • 12. PRACTICE 1 MAIN() METHOD Keep storing numbers from keyboard until the value is zero Make a choice and display the result
  • 13. PRACTICE 1 PROBLEMS (1/2) 13 Vague Naming Method names not following Java naming convention Wrong visibility
  • 14. PRACTICE 1 PROBLEMS (2/2) 14 Hard to understand Hard to read Indistinct identifier
  • 15. PRACTICE 1 REFACTORED ▸ Class Diagram 15 Class name, variables and methods renamed Methods extracted Visibility corrected Constant added
  • 16. PRACTICE 1 REFACTORED ▸ main() and public method run() 16 Test code with high readability by extracting a method and correcting visibility. Now, fillNumberArray() and run() are visible in main() method and run() uses the private methods inside.
  • 18. PRACTICE 2 TIC-TAC-TOE ▸ Requirements 1. Display the game board and the respected crosses and noughts with symbols. 2. Game continues until a player wins or ties. 18
  • 19. PRACTICE 2 TIC-TAC-TOE ▸ Example of Program Execution 19 Initial game board Pick a row and column in turn Game End and display a board
  • 20. PRACTICE 2 CLASS DIAGRAM OF INITIAL IMPLEMENTATION 20 A constructor initialising and print the game board A method that checks to see if a player has won or tied An array that contains the game board
  • 21. PRACTICE 2 PROBLEMS (1/2) 21 Unclear variable name Different functions contained in one method Method name breaking naming convention
  • 22. PRACTICE 2 PROBLEMS (2/2) 22 Bug latent I found the bug that already-chosen cell can be replaced in the refactoring process.
  • 23. PRACTICE 2 CLASS DIAGRAM OF CODE REFACTORED 23 Constant variables created The board and the parameters renamed Methods split so each has one function Class dissection
  • 24. PRACTICE 2 HOWEVER, I THOUGHT.. 24 TicTacToe.java still seems heavy. Local variables might not be necessary. Ways to eliminate error-prone code? Is the class dissection right? …
  • 25. PRACTICE 2 REFACTORING ONCE AGAIN 25 + Three valid states + Clearer relations among classes + More understandable classes and its role Now, ü it became more understandable and maintainable structure. ü I made reusable classes. ü Unintentionally I fixed a bug.
  • 26. LESSONS LEARNED (1/2) ▸ I had a chance to review bad code smells in my code. ▸ In the refactored code, ▸ methods and classes are much easier to read. ▸ the sub methods are easier to maintain. ▸ code becomes easier to read and understand without the need of comments. ▸ code becomes maintainable for future bugs and/or tweaks. 26REFACTORING PRACTICES: CLEANER CODE
  • 27. LESSONS LEARNED (2/2) ▸ To keep coming back to the code and revise it makes better code. ▸ Refactoring can be those important and meaningful practice. ▸ Keep in mind “The Boy Scout Rule” 27REFACTORING PRACTICES: CLEANER CODE