Download - Code Refactoring using rails
![Page 1: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/1.jpg)
Anand Bait
Developer
Allerin Tech Pvt Ltd
![Page 2: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/2.jpg)
Refactoring Code
Making code better, may not be faster
Better structured, better built
More readable, more understandable
Easy to work with
Easy to add new features
Easy to spot and fix bugs
Keeping code in control
Improving existing code
![Page 3: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/3.jpg)
Contents Introduction to Refactoring
Method-Level Refactoring
Class and Condition Focused Refactoring
Data Focused Refactoring
Communication and High Level Refactoring
Conclusion
![Page 4: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/4.jpg)
What ‘REFACTORING’ is? Techniques of improving code that already exists
No rules that anyone can blindly follow to improve code
Techniques of recognize issues as per consciousness
Applied without thinking will be dangerous
![Page 5: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/5.jpg)
What ‘REFACTORING’ is NOT? Not debugging
Not adding feature
Not changing observable behavior of code
Not performance improving
![Page 6: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/6.jpg)
Why to use?
![Page 7: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/7.jpg)
When to use? Write – Scan – Refactor at the end : INCORRECT
Write – Scan – Refactor regularly : CORRECT
Code reviews
How to use?
![Page 8: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/8.jpg)
Code smell Duplicated code
Long methods
Comments
Doesn’t tell what to do but tells us what to notice
![Page 9: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/9.jpg)
Method Level Refactoring Preparing to refactor
Grouping refactorings together
Using the Extract Method refactoring
Extract Method with parameters and variables
Common code smells for Extract Method
Using IDEs for the Extract Method refactoring
The Inline Method refactoring
Refactorings that remove temps
Refactorings that add temps
![Page 10: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/10.jpg)
Preparing to refactor Requirement of automated testing vs manual testing
![Page 11: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/11.jpg)
Grouping refactorings together Individual Method(name? parameters? Temp
variables? Duplicate? Too much in method?)
Context of method in class (right class? similarity with other methods class)
Communication between classes(inheritance)
![Page 12: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/12.jpg)
Extract Method refactoring Smaller and specific is better
Provides reusability
Ease of overriding while using inheritance
![Page 13: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/13.jpg)
Extract Method with parameters
Parameters should be related to the method
Extract Method with variables Temporary variables can be extracted
![Page 14: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/14.jpg)
Code smells for using Extract Method Long method
Comments(having comments vs need comments)
![Page 15: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/15.jpg)
Inline Method refactoring Removing method that adds no value
![Page 16: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/16.jpg)
Refactoring to remove temps Increase temptation to write longer methods
Change temps to small methods
![Page 17: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/17.jpg)
Refactoring to add temps Split temporary variables
Introduce explaining variables
Remove assignments to parameters
![Page 18: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/18.jpg)
Split temporary variables Don’t use same temporary variable for different
purposes
![Page 19: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/19.jpg)
Introduce explaining variables To improve readability
Introduce when a small method may not be required
![Page 20: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/20.jpg)
Remove assignments to parameters
Add temporary variables that assigns value to parameters may give undesired results
![Page 21: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/21.jpg)
Class and Condition Focused Refactoring Move Method
Extract Class and Inline Class
Making conditions easier to read
Replacing conditions with polymorphism
Replacing type code with subclasses
![Page 22: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/22.jpg)
Move Method Move method from one class to another
When a method interact with multiple classes then move it to the class with which it interacts more
Code smell: feature envy, inappropriate intimacy, shotgun surgery
![Page 23: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/23.jpg)
Extract Class and Inline Class Code smell: Large class
![Page 24: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/24.jpg)
Making conditions easier to read Decompose conditionals
Consolidate conditionals
Consolidate duplicate conditionals
![Page 25: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/25.jpg)
Replacing conditions with polymorphism
Conditions to check custom behavior
![Page 26: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/26.jpg)
Replacing type code with subclasses Data can be categorized into subclasses
![Page 27: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/27.jpg)
Data Focused Refactoring Moving and encapsulating fields
Working with data clumps
Simplifying method calls and parameter use
Pulling and pushing methods and fields
![Page 28: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/28.jpg)
Moving and encapsulating fields If a field is more related and being used by the other
class
![Page 29: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/29.jpg)
Working with data clumps Preserve whole object
(instead of parameters pass object)
Introduce parameter object
(minimize parameters list by creating a class for set of parameters)
![Page 30: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/30.jpg)
Simplifying method calls and parameter use Rename method
Remove parameter
Add parameter
Parameterize method
(same behavior multiple methods)
De-parameterize method
(same type of parameter but different behavior depending on parameter)
Separate Query from Modifier
(split method with individual focus on functioning)
![Page 31: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/31.jpg)
Pulling and pushing methods and fields
Pull up method
Pull up field
Push down method
Push down field
![Page 32: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/32.jpg)
Communication and High-Level Refactoring
Refining hierarchies
Extract superclass, Extract subclass, collapse hierarchy
Communication refactoring
Code smell: message chain, middle man
Larger scale refactoring
Convert procedural to OO
Code smell: large classes, god classes, dumb data
![Page 33: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/33.jpg)
Conclusion
![Page 34: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/34.jpg)
References http://refactoring.com
![Page 35: Code Refactoring using rails](https://reader034.vdocuments.us/reader034/viewer/2022050818/55a8987f1a28ab053e8b4778/html5/thumbnails/35.jpg)
Thank you