SlideShare a Scribd company logo
Effective Refactoring
Arnon Axelrod
Copyright © SELA Software & Education Labs, Ltd. | 14-18 Baruch Hirsch St., Bnei Brak 51202, Israel | www.selagroup.com
About me
Arnon Axelrod
https://blogs.microsoft.co.il/blogs/arnona
ArnonAxelrod
What is Refactoring?
restructuring existing
computer code, without
changing its external
behavior
Clean Code
Measuring Clean Code
Clean Code
• Readability
– Naming
– Formatting
• Method length
• Number of parameters
• Modularity and Reusability
– Law of Demeter
– Avoid static and Singletons
• Exception Handling
Law of Demeter
Method f in class C should only call methods of:
C
An object created by f
An object passed as an argument to f
An object held by an instance variable of C
Counter example:
string outputDir =
ctxt.getOptions().getScratchDir().getAbsolutePath();
The SOLID principles
• Single Responsibility
• Open/Close
• Liskov Substitution
• Interface Segregation
• Dependency Inversion
4 Rules of Simple Design / Kent Beck
• Passes all the tests
• Reveals the intent
• No duplication (DRY)
• Fewest elements (YAGNI)
(order matters)
Poka Yoke
Poka Yoke - examples
• private/public
• readonly fields and get-only properties
• Use classes instead of strings and integers
• Use most specific interfaces
– E.g. IReadOnlyCollection<T> instead of List<T>
Strong Typing
With strong typing:
With primitive types:
Refactoring / Martin Fowler
Refactoring – Best Practices
• Small Steps
• Test Often
• Write missing unit-tests
• Pair-programming
• Add before remove
• Use TODO and [Obsolete] midway
Add Before Remove
public class SomeClass
{
public SomeClass()
{
//...
}
public void Foo(int someParam)
{
// use someParam...
}
}
Initial state
Add Before Remove
public class SomeClass
{
private int _someParam;
public SomeClass(int someParam)
{
//...
_someParam = someParam;
}
public void Foo()
{
// use _someParam...
}
}
Final (desired)
state
Add Before Remove
public class SomeClass
{
public SomeClass()
{
//...
}
public void Foo(int someParam)
{
// use someParam...
}
}
Initial state
Add Before Remove
public class SomeClass
{
public SomeClass()
{
//...
}
private int _someParam;
public SomeClass(int someParam) : this()
{
_someParam = someParam;
}
public void Foo(int someParam)
{
//use someParam...
}
}
Step1 – Add
constructor
overload
Add Before Remove
public class SomeClass
{
private int _someParam;
public SomeClass(int someParam)
{
//...
_someParam = someParam;
}
public void Foo(int someParam)
{
//use someParam...
}
}
Step2 – Remove old
constructor overload
Add Before Remove
public class SomeClass
{
private int _someParam;
public SomeClass(int someParam)
{
//...
_someParam = someParam;
}
public void Foo(int someParam)
{
//use someParam...
}
public void Foo()
{
Foo(_someParam);
}
}
Step3 – Add
new overload
of Foo
Add Before Remove
public class SomeClass
{
private int _someParam;
public SomeClass(int someParam)
{
//...
_someParam = someParam;
}
public void Foo()
{
//use _someParam...
}
}
Step4 (final) –
Remove old
overload of Foo
Refactoring with Resharper
Refactoring with Resharper
Resharper Refactorings
• Rename
• Extract Method/Inline method
• Change Signature
• Introduce variable/Inline variable
• Introduce field/Inline field
• Introduce parameter/Inline parameter
• Transform parameters (to new class)
• Convert Property to Method(s)/Convert
• Convert Extension Method to Static/Convert Static to Extension Method
• Convert Interface to Abstract Class/Convert Abstract Class to Interface
• Replace Constructor with Factory method
• Move Instance Method, Move to Another Type
• Safe Delete
• And more…
Inline Method
Change Signature
Introduce Field
Extract Super Class
Pull Members Up
Extract Interface
Encapsulate Field
Convert Property to Method(s)
Move to Another Type
Code Analysis
Effective refactoring
Summary
• Clean code is essential for productivity
and quality
• Refactoring is essential to keep clean
code
• It’s important to learn and practice
Clean Code and Refactoring
• Mastering the tools will make you
hyper-productive!
Questions

More Related Content

PDF
Whats to come with swift generics
PPTX
Java vs kotlin
PDF
1 kotlin vs. java: some java issues addressed in kotlin
PPTX
Session 08 - OOP with Java - continued
PPTX
Kotlin For Android - Properties (part 4 of 7)
PPT
Operator overloading
PPTX
Operator overloading and type conversion in cpp
PPTX
C++ Constructor destructor
Whats to come with swift generics
Java vs kotlin
1 kotlin vs. java: some java issues addressed in kotlin
Session 08 - OOP with Java - continued
Kotlin For Android - Properties (part 4 of 7)
Operator overloading
Operator overloading and type conversion in cpp
C++ Constructor destructor

What's hot (19)

PPT
Operator Overloading
PPTX
Presentation on overloading
PDF
Lombok Features
PPT
Core java day4
PPT
Overloading
PPTX
operator overloading & type conversion in cpp over view || c++
PPT
Input and output in C++
PPT
pointers, virtual functions and polymorphisms in c++ || in cpp
PPTX
Operator overloading and type conversions
PDF
Wait for your fortune without Blocking!
PPTX
Operator Overloading & Type Conversions
PPT
Lec 26.27-operator overloading
PPSX
Writing code that writes code - Nguyen Luong
PPTX
Compile time polymorphism
PPSX
OOP with Java - Abstract Classes and Interfaces
PPTX
expression in cpp
PPT
Effective Java - Generics
PPT
C# Variables and Operators
PPTX
A Brief Conceptual Introduction to Functional Java 8 and its API
Operator Overloading
Presentation on overloading
Lombok Features
Core java day4
Overloading
operator overloading & type conversion in cpp over view || c++
Input and output in C++
pointers, virtual functions and polymorphisms in c++ || in cpp
Operator overloading and type conversions
Wait for your fortune without Blocking!
Operator Overloading & Type Conversions
Lec 26.27-operator overloading
Writing code that writes code - Nguyen Luong
Compile time polymorphism
OOP with Java - Abstract Classes and Interfaces
expression in cpp
Effective Java - Generics
C# Variables and Operators
A Brief Conceptual Introduction to Functional Java 8 and its API

Similar to Effective refactoring (20)

PDF
Breaking Dependencies to Allow Unit Testing
PDF
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
PPSX
TechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
PPTX
Constructors and Destructors
PPTX
C++ idioms.pptx
PPT
Csharp4 objects and_types
PDF
Koin Quickstart
PPTX
C++ theory
PPT
Handling Exceptions In C &amp; C++ [Part B] Ver 2
PPTX
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
PPTX
03 classes interfaces_principlesofoop
ODP
PHP Barcelona 2010 - Architecture and testability
PPTX
Java script Techniques Part I
DOCX
New microsoft office word document (2)
PPTX
CSharp presentation and software developement
DOCX
C questions
PPTX
Object-Oriented Programming with C#
PPTX
OOP with Java - Part 3
PDF
TWINS: OOP and FP - Warburton
PDF
00-intro-to-classes.pdf
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
TechkTalk #12 Grokking: Writing code that writes code – Nguyen Luong
Constructors and Destructors
C++ idioms.pptx
Csharp4 objects and_types
Koin Quickstart
C++ theory
Handling Exceptions In C &amp; C++ [Part B] Ver 2
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
03 classes interfaces_principlesofoop
PHP Barcelona 2010 - Architecture and testability
Java script Techniques Part I
New microsoft office word document (2)
CSharp presentation and software developement
C questions
Object-Oriented Programming with C#
OOP with Java - Part 3
TWINS: OOP and FP - Warburton
00-intro-to-classes.pdf

More from Arnon Axelrod (11)

PPTX
Defect free development - QS Tag2019
PPTX
ATDD open house
PPTX
Test Automation Maturity Model (Israel Test Automation meetup 12/11/2018)
PPTX
Competitive code
PPTX
Beyond pageobjects
PPTX
Test automation and architecture
PPTX
Unit Testing, TDD and ATDD
PPTX
Software quality - no more bugs!
PPTX
C# in depth
PPTX
ATDD with SpecFlow
PPTX
Automation at Philips Healthcare
Defect free development - QS Tag2019
ATDD open house
Test Automation Maturity Model (Israel Test Automation meetup 12/11/2018)
Competitive code
Beyond pageobjects
Test automation and architecture
Unit Testing, TDD and ATDD
Software quality - no more bugs!
C# in depth
ATDD with SpecFlow
Automation at Philips Healthcare

Recently uploaded (20)

PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
top salesforce developer skills in 2025.pdf
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
System and Network Administraation Chapter 3
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
Nekopoi APK 2025 free lastest update
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Cost to Outsource Software Development in 2025
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
assetexplorer- product-overview - presentation
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
medical staffing services at VALiNTRY
Wondershare Filmora 15 Crack With Activation Key [2025
top salesforce developer skills in 2025.pdf
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Digital Systems & Binary Numbers (comprehensive )
System and Network Administraation Chapter 3
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Designing Intelligence for the Shop Floor.pdf
wealthsignaloriginal-com-DS-text-... (1).pdf
Digital Strategies for Manufacturing Companies
Nekopoi APK 2025 free lastest update
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Cost to Outsource Software Development in 2025
Computer Software and OS of computer science of grade 11.pptx
assetexplorer- product-overview - presentation
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
medical staffing services at VALiNTRY

Effective refactoring

Editor's Notes

  • #3: באתי מהכיוון של Clean code ו-Unit tests לתחום הבדיקות האוטומטיות. היום אני רוצה לחזור כי לדעתי שם נמצאות הבעיות האמיתיות...
  • #4: The goal: to improve maintainability It’s not: A complete rewrite Bug fixing Changing a technology (to some extent) Behavior is contextual What are the boundaries of the refactored system? Is “performance” considered behavior Is CPU and memory consumption behavior? Is Reflection should be preserved? Exceptions (backward compatibility) Does the bits should be preserved…? We should know our presumptions
  • #6: But really: Amount of time spent on debugging and understanding of code Makes it easier to prove correctness and avoid bugs Makes it easy and safe to make changes
  • #7: רשימה חלקית, ממש על קצה המזלג, של הכללים שנכללים תחת הקטגוריה של Clean code
  • #9: Many of these principles rely on Polymorphism which is based on the concept of callback functions. This is a concept that developers must master
  • #11: בא לידי ביטוי בארכיטקטורה, בקוד ובתהליכי העבודה (למשל strongly typed)
  • #17: להחליף תמונה TDD provides a safety net Caution: If tests are bad, refactoring can be more difficult than without! On the other hand, TDD drives for good tests Small steps, climbers rule
  • #18: להחליף תמונה TDD provides a safety net Caution: If tests are bad, refactoring can be more difficult than without! On the other hand, TDD drives for good tests Small steps, climbers rule
  • #19: להחליף תמונה TDD provides a safety net Caution: If tests are bad, refactoring can be more difficult than without! On the other hand, TDD drives for good tests Small steps, climbers rule
  • #20: להחליף תמונה TDD provides a safety net Caution: If tests are bad, refactoring can be more difficult than without! On the other hand, TDD drives for good tests Small steps, climbers rule
  • #21: להחליף תמונה TDD provides a safety net Caution: If tests are bad, refactoring can be more difficult than without! On the other hand, TDD drives for good tests Small steps, climbers rule
  • #22: להחליף תמונה TDD provides a safety net Caution: If tests are bad, refactoring can be more difficult than without! On the other hand, TDD drives for good tests Small steps, climbers rule
  • #35: Value Tracking Call Tracking Type Hierarchy