SlideShare una empresa de Scribd logo
1 de 30
Descargar para leer sin conexión
Software Refactoring


                                     Mika Mäntylä,
                             Helsinki University of Technology
                      Software Business and Engineering Institute




HELSINKI UNIVERSITY OF TECHNOLOGY
Outline
 Refactoring - Improving the created
 construction
 When to Refactor: Bad code smells & Anti-Pattern




                      Mika Mäntylä                  2
What is Software Refactoring
 Also know as Restructuring
 Fowler’s book title as definition:
    Refactoring - Improving the design of existing code
 Refactoring increases
    Software maintainability (=ease of further development
    and bug fixing)
       Evolvablity would be a better term
 Refactoring does not
    Change the observable behavior of the software
       Performance tuning is not refactoring
 Good programmers have always refactored


                           Mika Mäntylä                      3
Why Refactoring, if we have perfect design?




                    Mika Mäntylä              4
Why Refactoring?
 Software Evolution (Lehman’s Laws)
   Law 1: Change is inevitable
      Software which is used in a real-world environment must
      change or become less and less useful in that
      environment
      Does not hold for all software, e.g. Algorithms, protocols
      may remain unchanged for decades
   Law 2: Each change increases complexity
      As an evolving program changes, its structure becomes
      more complex, unless active efforts are made to avoid
      this phenomenon
   Consequence: Soon the perfect design is outdated



                           Mika Mäntylä                            5
Why Refactoring cont’d?
 Learning
    Continuous learning (of technology, domain, customer
    problem, etc) exposes new and better design
    alternatives
    Consequence: Perfect design is not possible
    In some cases even big refactorings are not enough
       Brooks: plan to throw one away; you will, anyhow
       Managers may not allow complete rewrite
 Agile software development methods
    Accept the consequences of evolution and learning
       Need of continuous refactoring
    XP embrace change

                           Mika Mäntylä                    7
Why Refactoring cont’d?
 Refactoring is a part of                    Benefits of Refactoring
 software development                        according to Fowler and
    Earlier it was considered as             Arnold
    less important maintenance                  It improves software design
    activity                                    Programs are easier to
    Software maintenance starts                 understand
    from day 1                                  It helps you finding bugs
    Design is not enough                        Increased software
                                                development speed
                                                Easier testing, auditing,
                                                documenting
                                                Reduced dependency on
                                                individuals
                                                Greater job satisfaction
                                                Extending system’s lifetime
                                                Preserving software asset
                                                value to organization


                              Mika Mäntylä                                    8
Refactoring Example 1/4 – Extract Method
void printSalerySheet() {
    printCompanyLogo();
    //Calculate salery
    int hoursWorked = getHours();
    int salery = hoursWorked * getWage();
    System.out.println (”Salery: " + salery);
}


-------------------------------------------------------
void printSalery() {
    printCompanyLogo();
    System.out.println (”Salery: " + calculateSalery());
}
int calculateSalery(){
    int hoursWorked = getHours();
    int salery = hoursWorked * getWage();
    return salery;
                                         Mika Mäntylä      9
}
Refactoring Example 2/4 –Null Object
Car car = null;                                   Car car = new NullCar();
...                                               ...
if (car == null)                                  engine = car.getEngine();
      engine = Engine.getBasic();                 ...
else                                              Class NullCar{...
      engine = car.getEngine();                       Engine getEngine(){
                                                            return Engine.getBasic()
                                                        }
                                                  }
                                       Car


                                  +getEngine()




                                     NullCar


                                  +getEngine()

                                       Mika Mäntylä                                    10
Refactoring Example 3/4 – Switch statement
Class Engine{ …
int cylinderCount(){
    switch (type){
         case FAMILY_CAR_ENGINE:
                  return getBaseSylinderNumber();
         case EXECUTIVE_CAR_ENGINE:
                  return getBaseSylinderNumber() * 2;
         case RACE_CAR_ENGINE:
                  return getRaceCarCylinderNumber();
    }
}




                                   Mika Mäntylä         11
Refactoring Example 4/4 – Inheritance




                   Mika Mäntylä         12
Refactoring Example 4/4 – Inheritance




                   Mika Mäntylä         13
Refactoring Example 4/4 – Inheritance




                   Mika Mäntylä         14
Practical notes on refactoring
  When to refactor
     Integrate refactoring with normal software development, e.g.
         Refactor when adding a feature - First refactor then add a feature
     Make a time box for software refactoring
         Have a refactoring day / week / iteration, e.g. refactor one day per week
           —   Microsoft 20% tax for refactoring
  What do I tell my manager
     Mostly problem when manager is not technically oriented
     Short answer:
         Don’t
     Long answer:
         Present the benefits of refactoring (Fowler & Arnold slide 7)
  What should I have before staring refactoring
     Refactoring tools
         Tools enable automated and safe refactorings
         Has increased considerably during past years
         A few years back, changing a variable/method/class name was painful
     Unit tests
         Manual refactorings can break the system

                                        Mika Mäntylä                                 15
Terms of ”Refactoring domain”
 Refactoring
    Controlled way to improve the software’s structure without
    changing its observable behavior
    Refactoring should be continuous activity performed by all
    software developers
 Re-engineering
    Examination and alteration of a system to reconstitute it in a
    new form
    Consists of Reverse-engineering and Forward engineering
    Large system modifications are referred as re-engineering
 Reverse-engineering
    Analyze system to identify system components and their
    relationship
    Create representation of system in higher abstraction level

                            Mika Mäntylä                             16
Summary: Refactoring
 Perfect design is impossible
   Software evolution
   Continuous learning and better design alternatives
 Refactoring is needed to keep the code evolvable
 Refactoring is no excuse to omit software design
 Examples of Refactorings
   Look more from Fowler’s Book on Refactoring




                        Mika Mäntylä                    17
Outline
 Refactoring - Improving the created construction
 When to Refactor: Bad code smells & Anti-
 Patterns




                       Mika Mäntylä                 18
Anti-Patterns and Bad Code Smells
 Describe sour constructs and other common
 software development mistakes




                      Mika Mäntylä           19
Pattern History
 Design Patterns
    Represent reusable designs
       Strategy, Command, Singleton, etc
    History
       Based on pattern languages which focused on traditional architecture
       (1977)
       Rediscovered by software people 1987 and published by GoF in 1994
    Motivation
       Capsulate years of industry experience on software development
 Anti-Patterns
    Represent frequently occurring undesirable pattern
    History
       Have been around longer than Design Patterns
       Fred Brooks and The Mythical Man-Month 1979
       Term Anti-Pattern appeared soon after the book by GoF in 1994
    Motivation
       Learning from mistakes

                                 Mika Mäntylä                                 20
Anti-Patterns & Bad Code Smells
 Anti-Pattern cover wider                    Bad code smells
 range of topics                               Are in fact development
    Development                                level anti-patterns
       Software Development
    Architectural
       Systems Architecture
    Managerial
       Organization and Process




                              Mika Mäntylä                               21
Bad Code Smells - Taxonomy
 Bloaters
   When something is too large, e.g.:
      Long Method
      Large Class
      Long Parameter List,
   These smells likely grow little bit a time
      Hopefully nobody designs e.g. Long Methods




                             Mika Mäntylä          22
Bad Code Smells - Taxonomy

 Object-Orientation abusers
   Object Oriented concept not fully understood and
   utilized, e.g.:
      Switch statements
      Alternative Classes with Different Interfaces
   C-programmer in a Java context




                           Mika Mäntylä               23
Switch statement
Class Engine{ …
int cylinderCount(){
    switch (type){
         case FAMILY_CAR_ENGINE:
                  return getBaseSylinderNumber();
         case EXECUTIVE_CAR_ENGINE:
                  return getBaseSylinderNumber() * 2;
         case RACE_CAR_ENGINE:
                  return 8;
                                                             Engine
    }
}                                                     +cylinderCount() : int




                              FamilyCarEngine         ExecutiveCarEngine         RaceCarEngine


                          +cylinderCount() : int      +cylinderCount() : int   +cylinderCount() : int

                                       Mika Mäntylä                                                     24
Alternative Classes with Different Interfaces
                                                                          RaceCar


                                                                  1     +cylinders()
                         Engine
               -typeCode : int                                           FamilyCar
               -raceCar : RaceCar
               -familyCar : FamilyCar
               -executiveCar : ExecutiveCar    **                 1   +cylinderCount()


                                                                       ExecutiveCar


                                                              1       +cylinderNumber()




PetrolEngine          DieselEngine            AlcoholEngine




                                              Mika Mäntylä                             25
Mika Mäntylä   26
Bad Code Smells - Taxonomy
 Change Preventers
   These smells make changing the system unnecessarily
   difficult, e.g.
      Shotgun surgery
       —   Change the database, e.g., from Oracle to SQLServer
           requires changes to several classes
   Violate principle one external change should effect to
   one class only




                             Mika Mäntylä                        27
Bad Code Smells - Taxonomy
 Dispensables
   All code needs effort to understand and maintain
   If code is not used or redundant it needs to be
   removed, e.g.:
      Dead code
      Speculative Generality
      Duplicate Code




                          Mika Mäntylä                28
Duplicate Code
 What others say
    “Once And Only Once” (Cunningham & Cunningham)
    “Don’t Repeat Yourself” (Hunt & Thomas)
    “Number one in the stink parade” (Fowler & Beck)
    In other words
       “Ohjelmoi vain sellaista mitä ei ole vielä ohjelmoitu”
 Syntactic duplication
    Fragments of code that are identical or near identical
    Tools can offer great help, e.g. Simian
 Semantic duplication
    Fragments of code that have identical intent but differ at
    code level
       E.g. bubble sort and quick sort
    No tool help available

                               Mika Mäntylä                      29
Bad Code Smells - Taxonomy
 Couplers
   Low coupling between objects/classes is one the
   desirable goals of OO software, e.g.
      Feature Envy
      Message Chains
       —    a.getB().getC().getD().doOperation
   Too much delegation (= minimizing coupling) can be
   bad as well, e.g.
      MiddleMan




                              Mika Mäntylä              30
Summary: Bad code smells & Anti-Patterns
 Present frequently occurring problems in the code
 and design
 Anti-patterns can also be found in higher levels, e.g.
 software processes, management
 Taxonomy of code smells
    Bloaters
    Object-Orientation abusers
    Change Preventers
    Dispensables
    Couplers




                         Mika Mäntylä                     31

Más contenido relacionado

Similar a high performance mysql

Agile software development
Agile software developmentAgile software development
Agile software developmentRajesh Piryani
 
Periodic Table of Agile Principles and Practices
Periodic Table of Agile Principles and PracticesPeriodic Table of Agile Principles and Practices
Periodic Table of Agile Principles and PracticesJérôme Kehrli
 
Extreme Programming: An Introduction to XP Practices
Extreme Programming: An Introduction to XP PracticesExtreme Programming: An Introduction to XP Practices
Extreme Programming: An Introduction to XP PracticesDavid Hanson
 
Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...
Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...
Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...ghodgkinson
 
Agile Development Ultimate Slides
Agile Development Ultimate SlidesAgile Development Ultimate Slides
Agile Development Ultimate Slidesgilashikwa
 
extreme Programming
extreme Programmingextreme Programming
extreme ProgrammingBilal Shah
 
Lecture - 20-23.pptx
Lecture - 20-23.pptxLecture - 20-23.pptx
Lecture - 20-23.pptxFarHana74914
 
Extreme Programming Talk Wise Consulting Www.Talkwiseconsulting
Extreme  Programming    Talk Wise  Consulting   Www.TalkwiseconsultingExtreme  Programming    Talk Wise  Consulting   Www.Talkwiseconsulting
Extreme Programming Talk Wise Consulting Www.Talkwiseconsultingtalkwiseone
 
Extreme programming talk wise consulting - www.talkwiseconsulting
Extreme programming   talk wise consulting - www.talkwiseconsultingExtreme programming   talk wise consulting - www.talkwiseconsulting
Extreme programming talk wise consulting - www.talkwiseconsultingtalkwiseone
 
Continuous Integration
Continuous IntegrationContinuous Integration
Continuous Integrationmantripooja
 
Agile Manifesto & XP
Agile Manifesto & XPAgile Manifesto & XP
Agile Manifesto & XPSemen Arslan
 
W1_S2_SEIntro_SDLC.pdf
W1_S2_SEIntro_SDLC.pdfW1_S2_SEIntro_SDLC.pdf
W1_S2_SEIntro_SDLC.pdfssuserf56658
 
Xp(Xtreme Programming) presentation
Xp(Xtreme Programming) presentationXp(Xtreme Programming) presentation
Xp(Xtreme Programming) presentationMuaazZubairi
 
Pega robotics best practices building solutions (1)
Pega robotics best practices   building solutions (1)Pega robotics best practices   building solutions (1)
Pega robotics best practices building solutions (1)KPMG US
 
Pega Robotics Training @Phno: whatsapp @8142976573.
Pega Robotics Training @Phno: whatsapp @8142976573.Pega Robotics Training @Phno: whatsapp @8142976573.
Pega Robotics Training @Phno: whatsapp @8142976573.Santhoo Vardan
 

Similar a high performance mysql (20)

Agile Engineering Practices
Agile Engineering PracticesAgile Engineering Practices
Agile Engineering Practices
 
Agile software development
Agile software developmentAgile software development
Agile software development
 
Periodic Table of Agile Principles and Practices
Periodic Table of Agile Principles and PracticesPeriodic Table of Agile Principles and Practices
Periodic Table of Agile Principles and Practices
 
Software Reuse
Software ReuseSoftware Reuse
Software Reuse
 
Software Reengineering
Software ReengineeringSoftware Reengineering
Software Reengineering
 
Extreme Programming: An Introduction to XP Practices
Extreme Programming: An Introduction to XP PracticesExtreme Programming: An Introduction to XP Practices
Extreme Programming: An Introduction to XP Practices
 
Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...
Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...
Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...
 
Agile Development Ultimate Slides
Agile Development Ultimate SlidesAgile Development Ultimate Slides
Agile Development Ultimate Slides
 
extreme Programming
extreme Programmingextreme Programming
extreme Programming
 
Lecture - 20-23.pptx
Lecture - 20-23.pptxLecture - 20-23.pptx
Lecture - 20-23.pptx
 
Extreme Programming Talk Wise Consulting Www.Talkwiseconsulting
Extreme  Programming    Talk Wise  Consulting   Www.TalkwiseconsultingExtreme  Programming    Talk Wise  Consulting   Www.Talkwiseconsulting
Extreme Programming Talk Wise Consulting Www.Talkwiseconsulting
 
Extreme programming talk wise consulting - www.talkwiseconsulting
Extreme programming   talk wise consulting - www.talkwiseconsultingExtreme programming   talk wise consulting - www.talkwiseconsulting
Extreme programming talk wise consulting - www.talkwiseconsulting
 
Continuous Integration
Continuous IntegrationContinuous Integration
Continuous Integration
 
Agiel sw development
Agiel sw developmentAgiel sw development
Agiel sw development
 
Agile Manifesto & XP
Agile Manifesto & XPAgile Manifesto & XP
Agile Manifesto & XP
 
W1_S2_SEIntro_SDLC.pdf
W1_S2_SEIntro_SDLC.pdfW1_S2_SEIntro_SDLC.pdf
W1_S2_SEIntro_SDLC.pdf
 
Xp(Xtreme Programming) presentation
Xp(Xtreme Programming) presentationXp(Xtreme Programming) presentation
Xp(Xtreme Programming) presentation
 
Pega robotics best practices building solutions (1)
Pega robotics best practices   building solutions (1)Pega robotics best practices   building solutions (1)
Pega robotics best practices building solutions (1)
 
Pega Robotics Training @Phno: whatsapp @8142976573.
Pega Robotics Training @Phno: whatsapp @8142976573.Pega Robotics Training @Phno: whatsapp @8142976573.
Pega Robotics Training @Phno: whatsapp @8142976573.
 
Refactoring 2 The Max
Refactoring 2 The MaxRefactoring 2 The Max
Refactoring 2 The Max
 

Más de liufabin 66688

人大2010新生住宿指南
人大2010新生住宿指南人大2010新生住宿指南
人大2010新生住宿指南liufabin 66688
 
中山大学2010新生住宿指南
中山大学2010新生住宿指南中山大学2010新生住宿指南
中山大学2010新生住宿指南liufabin 66688
 
武汉大学2010新生住宿指南
武汉大学2010新生住宿指南武汉大学2010新生住宿指南
武汉大学2010新生住宿指南liufabin 66688
 
天津大学2010新生住宿指南
天津大学2010新生住宿指南天津大学2010新生住宿指南
天津大学2010新生住宿指南liufabin 66688
 
清华2010新生住宿指南
清华2010新生住宿指南清华2010新生住宿指南
清华2010新生住宿指南liufabin 66688
 
南京大学2010新生住宿指南
南京大学2010新生住宿指南南京大学2010新生住宿指南
南京大学2010新生住宿指南liufabin 66688
 
复旦2010新生住宿指南
复旦2010新生住宿指南复旦2010新生住宿指南
复旦2010新生住宿指南liufabin 66688
 
北京师范大学2010新生住宿指南
北京师范大学2010新生住宿指南北京师范大学2010新生住宿指南
北京师范大学2010新生住宿指南liufabin 66688
 
北大2010新生住宿指南
北大2010新生住宿指南北大2010新生住宿指南
北大2010新生住宿指南liufabin 66688
 
Mysql Replication Excerpt 5.1 En
Mysql Replication Excerpt 5.1 EnMysql Replication Excerpt 5.1 En
Mysql Replication Excerpt 5.1 Enliufabin 66688
 
Drupal Con My Sql Ha 2008 08 29
Drupal Con My Sql Ha 2008 08 29Drupal Con My Sql Ha 2008 08 29
Drupal Con My Sql Ha 2008 08 29liufabin 66688
 
Application Partitioning Wp
Application Partitioning WpApplication Partitioning Wp
Application Partitioning Wpliufabin 66688
 
090507.New Replication Features(2)
090507.New Replication Features(2)090507.New Replication Features(2)
090507.New Replication Features(2)liufabin 66688
 
High Performance Mysql
High Performance MysqlHigh Performance Mysql
High Performance Mysqlliufabin 66688
 
Refactoring And Unit Testing
Refactoring And Unit TestingRefactoring And Unit Testing
Refactoring And Unit Testingliufabin 66688
 
Refactoring Simple Example
Refactoring Simple ExampleRefactoring Simple Example
Refactoring Simple Exampleliufabin 66688
 

Más de liufabin 66688 (20)

人大2010新生住宿指南
人大2010新生住宿指南人大2010新生住宿指南
人大2010新生住宿指南
 
中山大学2010新生住宿指南
中山大学2010新生住宿指南中山大学2010新生住宿指南
中山大学2010新生住宿指南
 
武汉大学2010新生住宿指南
武汉大学2010新生住宿指南武汉大学2010新生住宿指南
武汉大学2010新生住宿指南
 
天津大学2010新生住宿指南
天津大学2010新生住宿指南天津大学2010新生住宿指南
天津大学2010新生住宿指南
 
清华2010新生住宿指南
清华2010新生住宿指南清华2010新生住宿指南
清华2010新生住宿指南
 
南京大学2010新生住宿指南
南京大学2010新生住宿指南南京大学2010新生住宿指南
南京大学2010新生住宿指南
 
复旦2010新生住宿指南
复旦2010新生住宿指南复旦2010新生住宿指南
复旦2010新生住宿指南
 
北京师范大学2010新生住宿指南
北京师范大学2010新生住宿指南北京师范大学2010新生住宿指南
北京师范大学2010新生住宿指南
 
北大2010新生住宿指南
北大2010新生住宿指南北大2010新生住宿指南
北大2010新生住宿指南
 
Optimzing mysql
Optimzing mysqlOptimzing mysql
Optimzing mysql
 
Mysql Replication Excerpt 5.1 En
Mysql Replication Excerpt 5.1 EnMysql Replication Excerpt 5.1 En
Mysql Replication Excerpt 5.1 En
 
Drupal Con My Sql Ha 2008 08 29
Drupal Con My Sql Ha 2008 08 29Drupal Con My Sql Ha 2008 08 29
Drupal Con My Sql Ha 2008 08 29
 
Application Partitioning Wp
Application Partitioning WpApplication Partitioning Wp
Application Partitioning Wp
 
090507.New Replication Features(2)
090507.New Replication Features(2)090507.New Replication Features(2)
090507.New Replication Features(2)
 
Mysql Replication
Mysql ReplicationMysql Replication
Mysql Replication
 
High Performance Mysql
High Performance MysqlHigh Performance Mysql
High Performance Mysql
 
Lecture28
Lecture28Lecture28
Lecture28
 
Refactoring And Unit Testing
Refactoring And Unit TestingRefactoring And Unit Testing
Refactoring And Unit Testing
 
Refactoring Simple Example
Refactoring Simple ExampleRefactoring Simple Example
Refactoring Simple Example
 
Refactoring Example
Refactoring ExampleRefactoring Example
Refactoring Example
 

high performance mysql

  • 1. Software Refactoring Mika Mäntylä, Helsinki University of Technology Software Business and Engineering Institute HELSINKI UNIVERSITY OF TECHNOLOGY
  • 2. Outline Refactoring - Improving the created construction When to Refactor: Bad code smells & Anti-Pattern Mika Mäntylä 2
  • 3. What is Software Refactoring Also know as Restructuring Fowler’s book title as definition: Refactoring - Improving the design of existing code Refactoring increases Software maintainability (=ease of further development and bug fixing) Evolvablity would be a better term Refactoring does not Change the observable behavior of the software Performance tuning is not refactoring Good programmers have always refactored Mika Mäntylä 3
  • 4. Why Refactoring, if we have perfect design? Mika Mäntylä 4
  • 5. Why Refactoring? Software Evolution (Lehman’s Laws) Law 1: Change is inevitable Software which is used in a real-world environment must change or become less and less useful in that environment Does not hold for all software, e.g. Algorithms, protocols may remain unchanged for decades Law 2: Each change increases complexity As an evolving program changes, its structure becomes more complex, unless active efforts are made to avoid this phenomenon Consequence: Soon the perfect design is outdated Mika Mäntylä 5
  • 6. Why Refactoring cont’d? Learning Continuous learning (of technology, domain, customer problem, etc) exposes new and better design alternatives Consequence: Perfect design is not possible In some cases even big refactorings are not enough Brooks: plan to throw one away; you will, anyhow Managers may not allow complete rewrite Agile software development methods Accept the consequences of evolution and learning Need of continuous refactoring XP embrace change Mika Mäntylä 7
  • 7. Why Refactoring cont’d? Refactoring is a part of Benefits of Refactoring software development according to Fowler and Earlier it was considered as Arnold less important maintenance It improves software design activity Programs are easier to Software maintenance starts understand from day 1 It helps you finding bugs Design is not enough Increased software development speed Easier testing, auditing, documenting Reduced dependency on individuals Greater job satisfaction Extending system’s lifetime Preserving software asset value to organization Mika Mäntylä 8
  • 8. Refactoring Example 1/4 – Extract Method void printSalerySheet() { printCompanyLogo(); //Calculate salery int hoursWorked = getHours(); int salery = hoursWorked * getWage(); System.out.println (”Salery: " + salery); } ------------------------------------------------------- void printSalery() { printCompanyLogo(); System.out.println (”Salery: " + calculateSalery()); } int calculateSalery(){ int hoursWorked = getHours(); int salery = hoursWorked * getWage(); return salery; Mika Mäntylä 9 }
  • 9. Refactoring Example 2/4 –Null Object Car car = null; Car car = new NullCar(); ... ... if (car == null) engine = car.getEngine(); engine = Engine.getBasic(); ... else Class NullCar{... engine = car.getEngine(); Engine getEngine(){ return Engine.getBasic() } } Car +getEngine() NullCar +getEngine() Mika Mäntylä 10
  • 10. Refactoring Example 3/4 – Switch statement Class Engine{ … int cylinderCount(){ switch (type){ case FAMILY_CAR_ENGINE: return getBaseSylinderNumber(); case EXECUTIVE_CAR_ENGINE: return getBaseSylinderNumber() * 2; case RACE_CAR_ENGINE: return getRaceCarCylinderNumber(); } } Mika Mäntylä 11
  • 11. Refactoring Example 4/4 – Inheritance Mika Mäntylä 12
  • 12. Refactoring Example 4/4 – Inheritance Mika Mäntylä 13
  • 13. Refactoring Example 4/4 – Inheritance Mika Mäntylä 14
  • 14. Practical notes on refactoring When to refactor Integrate refactoring with normal software development, e.g. Refactor when adding a feature - First refactor then add a feature Make a time box for software refactoring Have a refactoring day / week / iteration, e.g. refactor one day per week — Microsoft 20% tax for refactoring What do I tell my manager Mostly problem when manager is not technically oriented Short answer: Don’t Long answer: Present the benefits of refactoring (Fowler & Arnold slide 7) What should I have before staring refactoring Refactoring tools Tools enable automated and safe refactorings Has increased considerably during past years A few years back, changing a variable/method/class name was painful Unit tests Manual refactorings can break the system Mika Mäntylä 15
  • 15. Terms of ”Refactoring domain” Refactoring Controlled way to improve the software’s structure without changing its observable behavior Refactoring should be continuous activity performed by all software developers Re-engineering Examination and alteration of a system to reconstitute it in a new form Consists of Reverse-engineering and Forward engineering Large system modifications are referred as re-engineering Reverse-engineering Analyze system to identify system components and their relationship Create representation of system in higher abstraction level Mika Mäntylä 16
  • 16. Summary: Refactoring Perfect design is impossible Software evolution Continuous learning and better design alternatives Refactoring is needed to keep the code evolvable Refactoring is no excuse to omit software design Examples of Refactorings Look more from Fowler’s Book on Refactoring Mika Mäntylä 17
  • 17. Outline Refactoring - Improving the created construction When to Refactor: Bad code smells & Anti- Patterns Mika Mäntylä 18
  • 18. Anti-Patterns and Bad Code Smells Describe sour constructs and other common software development mistakes Mika Mäntylä 19
  • 19. Pattern History Design Patterns Represent reusable designs Strategy, Command, Singleton, etc History Based on pattern languages which focused on traditional architecture (1977) Rediscovered by software people 1987 and published by GoF in 1994 Motivation Capsulate years of industry experience on software development Anti-Patterns Represent frequently occurring undesirable pattern History Have been around longer than Design Patterns Fred Brooks and The Mythical Man-Month 1979 Term Anti-Pattern appeared soon after the book by GoF in 1994 Motivation Learning from mistakes Mika Mäntylä 20
  • 20. Anti-Patterns & Bad Code Smells Anti-Pattern cover wider Bad code smells range of topics Are in fact development Development level anti-patterns Software Development Architectural Systems Architecture Managerial Organization and Process Mika Mäntylä 21
  • 21. Bad Code Smells - Taxonomy Bloaters When something is too large, e.g.: Long Method Large Class Long Parameter List, These smells likely grow little bit a time Hopefully nobody designs e.g. Long Methods Mika Mäntylä 22
  • 22. Bad Code Smells - Taxonomy Object-Orientation abusers Object Oriented concept not fully understood and utilized, e.g.: Switch statements Alternative Classes with Different Interfaces C-programmer in a Java context Mika Mäntylä 23
  • 23. Switch statement Class Engine{ … int cylinderCount(){ switch (type){ case FAMILY_CAR_ENGINE: return getBaseSylinderNumber(); case EXECUTIVE_CAR_ENGINE: return getBaseSylinderNumber() * 2; case RACE_CAR_ENGINE: return 8; Engine } } +cylinderCount() : int FamilyCarEngine ExecutiveCarEngine RaceCarEngine +cylinderCount() : int +cylinderCount() : int +cylinderCount() : int Mika Mäntylä 24
  • 24. Alternative Classes with Different Interfaces RaceCar 1 +cylinders() Engine -typeCode : int FamilyCar -raceCar : RaceCar -familyCar : FamilyCar -executiveCar : ExecutiveCar ** 1 +cylinderCount() ExecutiveCar 1 +cylinderNumber() PetrolEngine DieselEngine AlcoholEngine Mika Mäntylä 25
  • 26. Bad Code Smells - Taxonomy Change Preventers These smells make changing the system unnecessarily difficult, e.g. Shotgun surgery — Change the database, e.g., from Oracle to SQLServer requires changes to several classes Violate principle one external change should effect to one class only Mika Mäntylä 27
  • 27. Bad Code Smells - Taxonomy Dispensables All code needs effort to understand and maintain If code is not used or redundant it needs to be removed, e.g.: Dead code Speculative Generality Duplicate Code Mika Mäntylä 28
  • 28. Duplicate Code What others say “Once And Only Once” (Cunningham & Cunningham) “Don’t Repeat Yourself” (Hunt & Thomas) “Number one in the stink parade” (Fowler & Beck) In other words “Ohjelmoi vain sellaista mitä ei ole vielä ohjelmoitu” Syntactic duplication Fragments of code that are identical or near identical Tools can offer great help, e.g. Simian Semantic duplication Fragments of code that have identical intent but differ at code level E.g. bubble sort and quick sort No tool help available Mika Mäntylä 29
  • 29. Bad Code Smells - Taxonomy Couplers Low coupling between objects/classes is one the desirable goals of OO software, e.g. Feature Envy Message Chains — a.getB().getC().getD().doOperation Too much delegation (= minimizing coupling) can be bad as well, e.g. MiddleMan Mika Mäntylä 30
  • 30. Summary: Bad code smells & Anti-Patterns Present frequently occurring problems in the code and design Anti-patterns can also be found in higher levels, e.g. software processes, management Taxonomy of code smells Bloaters Object-Orientation abusers Change Preventers Dispensables Couplers Mika Mäntylä 31