SlideShare a Scribd company logo
Refactoring 2 TheMax
 Alfredo Morresi: www.rainbowbreeze.it
  Diego Guidi: lacorrente.blogspot.com
          staff@dotnetmarche.org
Shit happens

Anything that can go wrong will go
              wrong.
            (Arthur Bloch)
Underengineering

AKA "fast, slow, slower.... never :("
 quickly deliver 1.0 release that works well for our
  customers but with junky code
 working on 2.0 release, junky code slows you down,
  and new features are harder to implement
 as junky code multiplies, people lose faith into the
  system and the programmers
 planning next release, you realize you can't win, and
  start thinking about a total rewrite
Overengineering

AKA "the 'HelloWorld' pattern"
 code more sophisticated that it possible future
  requirements
 code hard to understand for new ones

 time wasted understanding and maintaining complex
  code so you need to write documentation
Code smells

   copy/paste (duplicate) code
   large method/class
   method-like properties
   contrived complexity (patterns everywhere!)
   inappropriate intimacy
   unused code
   dangerous if (if - else if - else if - else...)
Cunningham's metaphor

The technical language doesn't communicate
effectively with the vast majority of management.
Instead, Ward Cunningham's financial metaphor of
design debt works infinitely better.
Design debt occurs when you don't consistently do
three things.
1. Remove duplication.
2. Simplify your code.
3. Clarify you code's intent.
Cunningham's metaphor

 Few systems remain completely free of design debt.
Wired as we are, humans just don't write perfect code
the first time around. We naturally accumulate design
             debt. So the question becomes:


   "When do you pay it down?"
And so, 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 yet improves its internal
                        structure.

A refactoring is a "behavior-preserving transformation"
 or, as Martin Fowler defines it, "a change made to the
    internal structure of software to make it easier to
understand and cheaper to modify without changing its
                   observable behavior"
Why refactoring?

   Code i wrote yesterday is worse that code i'm writing
    today
   Make it easier to add new code
   Improve the design of existing code
   Gain a better understanding of code
   Make coding less annoying
   Readability / Testability / Estensibility
   bugs correction
   ...
When refactoring?

   Keeping code clean is a lot like keeping a room
    clean. Once your room becomes a mess, it becomes
    harder to clean. The worse the mess becomes, the
    less you want to clean it.
   It's best to refactor continuously, rather than in
    phases. When you see code that needs
    improvement, improve it.
   On the other hand, if your manager needs you to
    finish a feature before a demo that just got
    scheduled for tomorrow, finish the feature and
    refactor later!
When NOT refactoring?

   If it's working, don't change
   if you have a poorly factored program
   if your code isn't tested, that does what the
    customer wants and has no serious bugs, leave it
    alone!
   performance matters...
How refactoring

Refactoring recipes
 extract superclass/interface

 rename method/property/variable

 replace conditional with polymorphism

 replace constructor with factory method

 inline method

 even more: http://www.refactoring.com/catalog
How refactoring

 Only refactor when refactoring -- do not add feature
                 during refactoring.

Refactoring, or improving the design of existing code,
requires that you know what code needs improvement.
How refactoring

Each transformation (called a "Refactoring") does little,
  but a sequence of transformations can produce a
              significant restructuring.

The system is also kept fully working after each small
 refactoring, reducing the chances that a system can
get seriously broken during the restructuring process.
How refactoring



If you want to refactor, the essential
   precondition is having solid test
             (Martin Fowler)
Test-Driven Development



   Test-driven development (TDD) and continuous
 refactoring enable the efficient evolution of working
    code by turning programming into a dialogue.
Test-Driven Development

Ask: You ask a question of a system by writing a test.
Respond: You respond to the question by writing
code to pass the test.
Refine: You refine your response by consolidating
ideas, weeding out inessentials, and clarifying
ambiguities.
Repeat: You keep the dialogue going by asking the
next question.
Refactoring and pattern

 There is a natural relation between
 patterns and refactorings. Patterns
     are where you want to be;
 refactorings are ways to get there
       from somewhere else.
             (Fowler Martin)
Refactoring and pattern

   Each pattern is a three-part rule, which expresses a
    relation between a certain context, a problem, and a
    solution.
   There are many ways to implement a pattern. If you
    don't know patterns, you're less likely to evolve great
    designs. Patterns capture wisdom. Reusing that
    wisdom is extremely useful, also when refactoring.
Readings

   Refactoring: Improving the Design of Existing Code
    (Martin Fowler)

   Refactoring to patterns
    (Joshua Kerievsky)

   Design Patterns: Elements of Reusable Object-
    Oriented Software
    (Gang of Four)

   The Pragmatic Programmer
    (Andrew Hunt and David Thomas)
The sage final sentence


          Any fool can write code
            that a computer can
             understand. Good
          programmers write code
              that humans can
                understand.
                   (M. F.)
Questions?

More Related Content

PDF
ScalaItaly 2015 - Your Microservice as a Function
PPT
The Smells Of Bad Design
PPT
Design Smells
PPTX
Importance of the quality of code
PPTX
Portrait of professional developer 2.0
PPTX
GOOD PROGRAMMING
PDF
WordCamp US: Clean Code
PPT
Clean Code summary
ScalaItaly 2015 - Your Microservice as a Function
The Smells Of Bad Design
Design Smells
Importance of the quality of code
Portrait of professional developer 2.0
GOOD PROGRAMMING
WordCamp US: Clean Code
Clean Code summary

What's hot (15)

PDF
Tdd 왜 배우기 어려운가
ODP
Documenting Code - Patterns and Anti-patterns - NLPW 2016
PPT
Best Practices of Software Development
PDF
Learning Curve
PDF
Six Steps to Conversation Driven Development
PDF
Feedback on Part 1 of the Software Engineering Large Practical
PDF
What's in a Name?
PPTX
Test-Driven Development
PPTX
Mark asoi ppt
PDF
Feedback on Part 1 of the CSLP
PDF
TDD for the masses
PDF
Code Craftsmanship Checklist
PDF
hints for computer system design by Butler Lampson
PDF
A Software Problem (and a maybe-solution)
PPTX
Why pay two developers to do the work of one?
Tdd 왜 배우기 어려운가
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Best Practices of Software Development
Learning Curve
Six Steps to Conversation Driven Development
Feedback on Part 1 of the Software Engineering Large Practical
What's in a Name?
Test-Driven Development
Mark asoi ppt
Feedback on Part 1 of the CSLP
TDD for the masses
Code Craftsmanship Checklist
hints for computer system design by Butler Lampson
A Software Problem (and a maybe-solution)
Why pay two developers to do the work of one?
Ad

Viewers also liked (9)

PDF
Strategie di Enterprise Mobile Security, per una gestione sicura dei disposit...
ODP
Mobile security & privacy - Paranoia in movimento
PPT
MDM is not Enough - Parmelee
PDF
Mobile Data Security: Sicurezza IT per aziende in movimento
ODP
Advanced Android Development
PDF
Slightly Advanced Android Wear ;)
PDF
Android Survival Guide - Two years of software development
PDF
L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...
PDF
Sophos Complete Security: arte e scienza della sicurezza
Strategie di Enterprise Mobile Security, per una gestione sicura dei disposit...
Mobile security & privacy - Paranoia in movimento
MDM is not Enough - Parmelee
Mobile Data Security: Sicurezza IT per aziende in movimento
Advanced Android Development
Slightly Advanced Android Wear ;)
Android Survival Guide - Two years of software development
L’innovazione a difesa della tradizione: il caso dell’Archivio Storico della ...
Sophos Complete Security: arte e scienza della sicurezza
Ad

Similar to Refactoring 2 The Max (20)

PPTX
Refactoring, 2nd Edition
PDF
2018 01-29 - brewbox - refactoring. sempre, senza pietà
PDF
Refactoring 2TheMax (con ReSharper)
PDF
Put to the Test
PDF
Quick Intro to Clean Coding
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
PDF
agile refactoring and integration techniques.pdf
PPTX
Refactoring
PPT
Principles in Refactoring
PPT
Agile Methodologies And Extreme Programming
PPT
Agile Methodologies And Extreme Programming - Svetlin Nakov
PPT
Refactoring, A First Example
PDF
10 Ways To Improve Your Code( Neal Ford)
PDF
WordCamp Nashville: Clean Code for WordPress
PDF
10 Ways To Improve Your Code
PPTX
Agile Values, Principles and Practices
PPTX
Best pratice
PDF
Patterns And Practices For Infrastructure As Code With Examples In Python And...
PPT
Code Quality
ODP
Documenting code yapceu2016
Refactoring, 2nd Edition
2018 01-29 - brewbox - refactoring. sempre, senza pietà
Refactoring 2TheMax (con ReSharper)
Put to the Test
Quick Intro to Clean Coding
Writing Better Code - Helping Developers make Decisions.pptx
agile refactoring and integration techniques.pdf
Refactoring
Principles in Refactoring
Agile Methodologies And Extreme Programming
Agile Methodologies And Extreme Programming - Svetlin Nakov
Refactoring, A First Example
10 Ways To Improve Your Code( Neal Ford)
WordCamp Nashville: Clean Code for WordPress
10 Ways To Improve Your Code
Agile Values, Principles and Practices
Best pratice
Patterns And Practices For Infrastructure As Code With Examples In Python And...
Code Quality
Documenting code yapceu2016

More from Alfredo Morresi (9)

ODP
Testing in Android: automatici, di integrazione, TDD e scenari avanzati
ODP
Mobile platforms development overview
ODP
Nativa Android Applications development
ODP
Funambol Code Sniper - Avatargrabber
PDF
Tesi cartoni animati e modelli culturali
PDF
(in)Sicurezze delle reti wireless 802.11b
PDF
BlueSecurity: quando il dente fa male!
PDF
2(.0) passi nel mondo mobile - Alfredo Morresi
PDF
QR code - Alfredo Morresi
Testing in Android: automatici, di integrazione, TDD e scenari avanzati
Mobile platforms development overview
Nativa Android Applications development
Funambol Code Sniper - Avatargrabber
Tesi cartoni animati e modelli culturali
(in)Sicurezze delle reti wireless 802.11b
BlueSecurity: quando il dente fa male!
2(.0) passi nel mondo mobile - Alfredo Morresi
QR code - Alfredo Morresi

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
Teaching material agriculture food technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
KodekX | Application Modernization Development
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
cuic standard and advanced reporting.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Teaching material agriculture food technology
Review of recent advances in non-invasive hemoglobin estimation
Spectral efficient network and resource selection model in 5G networks
KodekX | Application Modernization Development
Network Security Unit 5.pdf for BCA BBA.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation theory and applications.pdf
MYSQL Presentation for SQL database connectivity
Dropbox Q2 2025 Financial Results & Investor Presentation
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
cuic standard and advanced reporting.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Per capita expenditure prediction using model stacking based on satellite ima...
Digital-Transformation-Roadmap-for-Companies.pptx

Refactoring 2 The Max

  • 1. Refactoring 2 TheMax Alfredo Morresi: www.rainbowbreeze.it Diego Guidi: lacorrente.blogspot.com staff@dotnetmarche.org
  • 2. Shit happens Anything that can go wrong will go wrong. (Arthur Bloch)
  • 3. Underengineering AKA "fast, slow, slower.... never :("  quickly deliver 1.0 release that works well for our customers but with junky code  working on 2.0 release, junky code slows you down, and new features are harder to implement  as junky code multiplies, people lose faith into the system and the programmers  planning next release, you realize you can't win, and start thinking about a total rewrite
  • 4. Overengineering AKA "the 'HelloWorld' pattern"  code more sophisticated that it possible future requirements  code hard to understand for new ones  time wasted understanding and maintaining complex code so you need to write documentation
  • 5. Code smells  copy/paste (duplicate) code  large method/class  method-like properties  contrived complexity (patterns everywhere!)  inappropriate intimacy  unused code  dangerous if (if - else if - else if - else...)
  • 6. Cunningham's metaphor The technical language doesn't communicate effectively with the vast majority of management. Instead, Ward Cunningham's financial metaphor of design debt works infinitely better. Design debt occurs when you don't consistently do three things. 1. Remove duplication. 2. Simplify your code. 3. Clarify you code's intent.
  • 7. Cunningham's metaphor Few systems remain completely free of design debt. Wired as we are, humans just don't write perfect code the first time around. We naturally accumulate design debt. So the question becomes: "When do you pay it down?"
  • 8. And so, 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 yet improves its internal structure. A refactoring is a "behavior-preserving transformation" or, as Martin Fowler defines it, "a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior"
  • 9. Why refactoring?  Code i wrote yesterday is worse that code i'm writing today  Make it easier to add new code  Improve the design of existing code  Gain a better understanding of code  Make coding less annoying  Readability / Testability / Estensibility  bugs correction  ...
  • 10. When refactoring?  Keeping code clean is a lot like keeping a room clean. Once your room becomes a mess, it becomes harder to clean. The worse the mess becomes, the less you want to clean it.  It's best to refactor continuously, rather than in phases. When you see code that needs improvement, improve it.  On the other hand, if your manager needs you to finish a feature before a demo that just got scheduled for tomorrow, finish the feature and refactor later!
  • 11. When NOT refactoring?  If it's working, don't change  if you have a poorly factored program  if your code isn't tested, that does what the customer wants and has no serious bugs, leave it alone!  performance matters...
  • 12. How refactoring Refactoring recipes  extract superclass/interface  rename method/property/variable  replace conditional with polymorphism  replace constructor with factory method  inline method  even more: http://www.refactoring.com/catalog
  • 13. How refactoring Only refactor when refactoring -- do not add feature during refactoring. Refactoring, or improving the design of existing code, requires that you know what code needs improvement.
  • 14. How refactoring Each transformation (called a "Refactoring") does little, but a sequence of transformations can produce a significant restructuring. The system is also kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring process.
  • 15. How refactoring If you want to refactor, the essential precondition is having solid test (Martin Fowler)
  • 16. Test-Driven Development Test-driven development (TDD) and continuous refactoring enable the efficient evolution of working code by turning programming into a dialogue.
  • 17. Test-Driven Development Ask: You ask a question of a system by writing a test. Respond: You respond to the question by writing code to pass the test. Refine: You refine your response by consolidating ideas, weeding out inessentials, and clarifying ambiguities. Repeat: You keep the dialogue going by asking the next question.
  • 18. Refactoring and pattern There is a natural relation between patterns and refactorings. Patterns are where you want to be; refactorings are ways to get there from somewhere else. (Fowler Martin)
  • 19. Refactoring and pattern  Each pattern is a three-part rule, which expresses a relation between a certain context, a problem, and a solution.  There are many ways to implement a pattern. If you don't know patterns, you're less likely to evolve great designs. Patterns capture wisdom. Reusing that wisdom is extremely useful, also when refactoring.
  • 20. Readings  Refactoring: Improving the Design of Existing Code (Martin Fowler)  Refactoring to patterns (Joshua Kerievsky)  Design Patterns: Elements of Reusable Object- Oriented Software (Gang of Four)  The Pragmatic Programmer (Andrew Hunt and David Thomas)
  • 21. The sage final sentence Any fool can write code that a computer can understand. Good programmers write code that humans can understand. (M. F.)