SlideShare a Scribd company logo
Refactoring: Improving
the design of existing code
Chapter 6. Composing Methods
Kent Beck and Martin Fowler
Composing Methods
• A large part of the author’s refactoring is composing methods to package
code properly.
• Almost all the time the problems come from methods that are too long.
• Long methods are troublesome because they often contain lots of
information, which gets buried by the complex logic that usually gets
dragged in.
Composing Methods
• Extract Method
• Inline Method
• InlineTemp
• ReplaceTemp with Query
• Introduce ExplainingVariable
• SplitTemporaryVariable
• Remove Assignments to Parameters
• Replace Method with Method Object
• Substitute Algorithm
Extract Method
• Is the key refactoring, which takes a clump of code that is too long or needs
a comment to understand its purpose and turns it into its own method.
• Short, well-named methods, increases the chances that other methods can
use a method when the method is finely grained and allows the higher-level
methods to read more like a series of comments.
• If extracting improves clarity, do it, even if the name is longer than the code
you have extracted.
Extract Method
Extract Method
Inline Method
• When you've done multiple extractions and realize some of the resulting
methods are no longer pulling their weight
• If you need to reorganize the way you've broken down methods.
• When someone is using too much indirection and it seems that every
method does simple delegation to another method, and I get lost in all the
delegation. In these cases some of the indirection is worthwhile, but not all
of it.
Inline Method
Inline Method
InlineTemp
• Most of the time is used as part of ReplaceTemp with Query, so the real
motivation is there.
• The only time InlineTemp is used on its own is when you find a temp that is
assigned the value of a method call. Often this temp isn't doing any harm
and you can safely leave it there.
• If the temp is getting in the way of other refactorings, such as Extract
Method, it's time to inline it.
InlineTemp
ReplaceTemp with Query
• The problem with temps is that they are temporary and local.
• By replacing the temp with a query method, any method in the class can get at the
information.That helps a lot in coming up with cleaner code for the class.
• Often is a vital step before Extract Method. Local variables make it difficult to
extract, so replace as many variables as you can with queries.
• If the temp is used to collect a result (such as summing over a loop), you need to
copy some logic into the query method.
ReplaceTemp with Query
ReplaceTemp with Query
Introduce ExplainingVariable
• Put the result of the expression, or parts of the expression, in a temporary variable
with a name that explains the purpose.
• Is particularly valuable with conditional logic in which it is useful to take each clause
of a condition and explain what the condition means with a well-named temp.
• Another case is a long algorithm, in which each step in the computation can be
explained with a temp.
• Use Extract Method if you can.When local variables make it difficult to use Extract
Method is when you may use Introduce ExplainingVariable.
Introduce ExplainingVariable
Introduce ExplainingVariable
SplitTemporaryVariable
• You have a temporary variable assigned to more than once, but is not a loop
variable nor a collecting temporary variable.
• If they are set more than once is a sign that they have more than one
responsibility within the method.
• Any variable with more than one responsibility should be replaced with a
temp for each responsibility.
• Using a temp for two different things is very confusing for the reader.
SplitTemporaryVariable
SplitTemporaryVariable
Remove Assignments to Parameters
• If the code assigns to a parameter. Use a temporary variable instead.
• With pass by value, any change to the parameter is not reflected in the
calling routine.Those who have used pass by reference will probably find
this confusing..
• It is much clearer if you use only the parameter to represent what has been
passed in, because that is a consistent usage.
Remove Assignments to Parameters
Replace Method with Method Object
• If you have a long method that uses local variables in such a way that you
cannot apply Extract Method.
• Turn the method into its own object so that all the local variables become
fields on that object.You can then use Extract Method on this new object to
create additional methods that break down the original method.
Substitute Algorithm
• If you find a clearer way to do something, you should replace the complicated way
with the clearer way.
• Refactoring can break down something complex into simpler pieces, but
sometimes you just reach the point at which you have to remove the whole
algorithm and replace it with something simpler.
• This occurs as you learn more about the problem and realize that there's an easier
way to do it. It also happens if you start using a library that supplies features that
duplicate your code.
• When you have to take this step, make sure you have decomposed the method as
much as you can. Substituting a large, complex algorithm is very difficult; only by
making it simple can you make the substitution tractable.
Substitute Algorithm
Substitute Algorithm
Andrés Callejas González
Universidad Pontificia Bolivariana

More Related Content

PPTX
Moq presentation
PPTX
scope of python
PPTX
Qtp actions-the-basics
PPTX
2CPP12 - Method Overriding
PPTX
2CPP11 - Method Overloading
PPTX
Unit testing using jasmine in Javascript
PPT
How to test models using php unit testing framework?
PPTX
ASM Core API - methods (part 1)
Moq presentation
scope of python
Qtp actions-the-basics
2CPP12 - Method Overriding
2CPP11 - Method Overloading
Unit testing using jasmine in Javascript
How to test models using php unit testing framework?
ASM Core API - methods (part 1)

What's hot (20)

PPTX
Function
PPTX
Unit Testing with Python
PPT
1.qtp basics
PPTX
Tdd guide
ODP
Python unit testing
PPTX
Presenter deck icenium hol
PPTX
Unit testing and mocking in Python - PyCon 2018 - Kenya
PPTX
Workshop: Unit Testing in Python
ODP
Refactoring: Improving the design of existing code
PPTX
Function Parameters
PPTX
Reading init param
PPTX
Actions in QTP
PPTX
Unit testing with NUnit
PPTX
Break through e2e-testing
DOCX
Comp 220 ilab 5 of 7
PPT
Intro To Scala
PPT
Qtp Training Deepti 1 Of 4187
PPT
lazy evaluation
PDF
Basics of QTP Framework
PPT
Testing – With Mock Objects
Function
Unit Testing with Python
1.qtp basics
Tdd guide
Python unit testing
Presenter deck icenium hol
Unit testing and mocking in Python - PyCon 2018 - Kenya
Workshop: Unit Testing in Python
Refactoring: Improving the design of existing code
Function Parameters
Reading init param
Actions in QTP
Unit testing with NUnit
Break through e2e-testing
Comp 220 ilab 5 of 7
Intro To Scala
Qtp Training Deepti 1 Of 4187
lazy evaluation
Basics of QTP Framework
Testing – With Mock Objects
Ad

Viewers also liked (20)

PPTX
Continuos Delivery
PPTX
Chapter 5 refactoring
PPTX
Capitulo 7 moving features between objects
PPTX
Construir tests
PPTX
PPTX
Continuous delivery
PPTX
Refactoring: improving the desing of existing code Cap 1
PPTX
Implementing a testing strategy
PPTX
PPTX
Expo 2 parametros
PPT
Continuos Delivery Commit stage
PPTX
Making method calls_simpler
DOCX
Narrative structures
PPTX
Briefing On Influences
PDF
Jasmine Lockwood - Resume
PPTX
Infra / Cont delivery - 3rd party automation
PDF
Gabelli conference investor deck
PPTX
Estudo comparativo entr bancos RDBMS, NoSQL e NewSQL
PDF
Achieving Continuous Delivery: An Automation Story
Continuos Delivery
Chapter 5 refactoring
Capitulo 7 moving features between objects
Construir tests
Continuous delivery
Refactoring: improving the desing of existing code Cap 1
Implementing a testing strategy
Expo 2 parametros
Continuos Delivery Commit stage
Making method calls_simpler
Narrative structures
Briefing On Influences
Jasmine Lockwood - Resume
Infra / Cont delivery - 3rd party automation
Gabelli conference investor deck
Estudo comparativo entr bancos RDBMS, NoSQL e NewSQL
Achieving Continuous Delivery: An Automation Story
Ad

Similar to Refactoring: Improving the design of existing code. Chapter 6. (20)

PPTX
31 days Refactoring
PDF
Bad Smell In Codes 1
PPTX
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
PPTX
Design patterns and refactoring series 1
PDF
Refactoring: Improve the design of existing code
PPTX
Design p atterns
ODP
Refactoring Techniques
PDF
Week 7 Java Programming Methods For I.T students.pdf
PPTX
Code smells
PDF
Working With Concurrency In Java 8
PPTX
Clean Code
PPTX
Functional-style control flow in F#
PPTX
Lesson 3 Functions OOP Lessons For CS AND IT
PPTX
Unit Testng with PHP Unit - A Step by Step Training
KEY
Learning from "Effective Scala"
PDF
Programming with C++
PPT
METHODS IN JAVA.ppt
PPTX
Agile korea 2013 유석문
PPTX
Algorithm in data structure bca .pptx
PDF
2019 Levenshtein Transformer
31 days Refactoring
Bad Smell In Codes 1
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Design patterns and refactoring series 1
Refactoring: Improve the design of existing code
Design p atterns
Refactoring Techniques
Week 7 Java Programming Methods For I.T students.pdf
Code smells
Working With Concurrency In Java 8
Clean Code
Functional-style control flow in F#
Lesson 3 Functions OOP Lessons For CS AND IT
Unit Testng with PHP Unit - A Step by Step Training
Learning from "Effective Scala"
Programming with C++
METHODS IN JAVA.ppt
Agile korea 2013 유석문
Algorithm in data structure bca .pptx
2019 Levenshtein Transformer

Recently uploaded (20)

PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Introduction to Artificial Intelligence
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
ai tools demonstartion for schools and inter college
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
history of c programming in notes for students .pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
AI in Product Development-omnex systems
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Nekopoi APK 2025 free lastest update
PDF
System and Network Administration Chapter 2
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Operating system designcfffgfgggggggvggggggggg
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Introduction to Artificial Intelligence
2025 Textile ERP Trends: SAP, Odoo & Oracle
How Creative Agencies Leverage Project Management Software.pdf
Digital Strategies for Manufacturing Companies
Navsoft: AI-Powered Business Solutions & Custom Software Development
ai tools demonstartion for schools and inter college
Softaken Excel to vCard Converter Software.pdf
history of c programming in notes for students .pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
AI in Product Development-omnex systems
How to Migrate SBCGlobal Email to Yahoo Easily
Odoo Companies in India – Driving Business Transformation.pdf
Understanding Forklifts - TECH EHS Solution
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Nekopoi APK 2025 free lastest update
System and Network Administration Chapter 2
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...

Refactoring: Improving the design of existing code. Chapter 6.

  • 1. Refactoring: Improving the design of existing code Chapter 6. Composing Methods Kent Beck and Martin Fowler
  • 2. Composing Methods • A large part of the author’s refactoring is composing methods to package code properly. • Almost all the time the problems come from methods that are too long. • Long methods are troublesome because they often contain lots of information, which gets buried by the complex logic that usually gets dragged in.
  • 3. Composing Methods • Extract Method • Inline Method • InlineTemp • ReplaceTemp with Query • Introduce ExplainingVariable • SplitTemporaryVariable • Remove Assignments to Parameters • Replace Method with Method Object • Substitute Algorithm
  • 4. Extract Method • Is the key refactoring, which takes a clump of code that is too long or needs a comment to understand its purpose and turns it into its own method. • Short, well-named methods, increases the chances that other methods can use a method when the method is finely grained and allows the higher-level methods to read more like a series of comments. • If extracting improves clarity, do it, even if the name is longer than the code you have extracted.
  • 7. Inline Method • When you've done multiple extractions and realize some of the resulting methods are no longer pulling their weight • If you need to reorganize the way you've broken down methods. • When someone is using too much indirection and it seems that every method does simple delegation to another method, and I get lost in all the delegation. In these cases some of the indirection is worthwhile, but not all of it.
  • 10. InlineTemp • Most of the time is used as part of ReplaceTemp with Query, so the real motivation is there. • The only time InlineTemp is used on its own is when you find a temp that is assigned the value of a method call. Often this temp isn't doing any harm and you can safely leave it there. • If the temp is getting in the way of other refactorings, such as Extract Method, it's time to inline it.
  • 12. ReplaceTemp with Query • The problem with temps is that they are temporary and local. • By replacing the temp with a query method, any method in the class can get at the information.That helps a lot in coming up with cleaner code for the class. • Often is a vital step before Extract Method. Local variables make it difficult to extract, so replace as many variables as you can with queries. • If the temp is used to collect a result (such as summing over a loop), you need to copy some logic into the query method.
  • 15. Introduce ExplainingVariable • Put the result of the expression, or parts of the expression, in a temporary variable with a name that explains the purpose. • Is particularly valuable with conditional logic in which it is useful to take each clause of a condition and explain what the condition means with a well-named temp. • Another case is a long algorithm, in which each step in the computation can be explained with a temp. • Use Extract Method if you can.When local variables make it difficult to use Extract Method is when you may use Introduce ExplainingVariable.
  • 18. SplitTemporaryVariable • You have a temporary variable assigned to more than once, but is not a loop variable nor a collecting temporary variable. • If they are set more than once is a sign that they have more than one responsibility within the method. • Any variable with more than one responsibility should be replaced with a temp for each responsibility. • Using a temp for two different things is very confusing for the reader.
  • 21. Remove Assignments to Parameters • If the code assigns to a parameter. Use a temporary variable instead. • With pass by value, any change to the parameter is not reflected in the calling routine.Those who have used pass by reference will probably find this confusing.. • It is much clearer if you use only the parameter to represent what has been passed in, because that is a consistent usage.
  • 22. Remove Assignments to Parameters
  • 23. Replace Method with Method Object • If you have a long method that uses local variables in such a way that you cannot apply Extract Method. • Turn the method into its own object so that all the local variables become fields on that object.You can then use Extract Method on this new object to create additional methods that break down the original method.
  • 24. Substitute Algorithm • If you find a clearer way to do something, you should replace the complicated way with the clearer way. • Refactoring can break down something complex into simpler pieces, but sometimes you just reach the point at which you have to remove the whole algorithm and replace it with something simpler. • This occurs as you learn more about the problem and realize that there's an easier way to do it. It also happens if you start using a library that supplies features that duplicate your code. • When you have to take this step, make sure you have decomposed the method as much as you can. Substituting a large, complex algorithm is very difficult; only by making it simple can you make the substitution tractable.
  • 27. Andrés Callejas González Universidad Pontificia Bolivariana