1 / 20

Refactoring

Refactoring. Recap: Class Diagrams. Class diagrams represent design structure Three parts: name, attribute, operations Visibility, attribute type, multiplicity Association, association multiplicity Generalization i.e. interface impl, subclassing Composition i.e. class A contains class B

stilest
Download Presentation

Refactoring

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Refactoring

  2. Recap: Class Diagrams • Class diagrams represent design structure • Three parts: name, attribute, operations • Visibility, attribute type, multiplicity • Association, association multiplicity • Generalization i.e. interface impl, subclassing • Composition i.e. class A contains class B • Applied information hiding, DSM, layering

  3. Recap: Interaction Diagrams • Represent functionality • Two major parts: • Life line boxes (represent life time of an object) • Messages between objects (instances) • Also looked at some special case notations • Instances: parameterized instances, singleton instances, array elements • Messages: creation, destruction, conditional messages, mutually exclusive conditional messages, asynchronous messages, polymorphic messages • Abstractions: looping, iteration over collection, nesting of frames, decomposition

  4. Recap: Responsibility Assignment • Information Expert • Creator • Controller • Polymorphism • Pure Fabrication • Indirection • Protected Variations

  5. Eclipse Tutorial • Open source IDE • Available from http://www.eclipse.org • Lot of interesting & advance features • Demo: refactoring features in eclipse • Extract method • Pull up method • Push down method

  6. Refactoring • Reorganize instructions in a programs, perhaps rewriting • Key: Preserve semantics of the program • Objective: Improve structure (aka design), improve readability • Doesn’t add/remove any functionality

  7. Value Argument • Refactoring consumes resources • Doesn’t add any new feature • Impact on near term profit = 0 • Why Improve Structure?

  8. Value Argument • Readability • More readable less chances of inadvertent errors • Adding features in future will be less cumbersome • Invest today, reap later Understand-ability Cost/ feature Time 

  9. Is semantics preserved? • Informal & imprecise verification: Write test suite • Rigorous test suite development before refactoring • Test after you refactor • Question: Why do I call it imprecise verification? • Formal verification: Prove that certain transformation always preserves the semantics

  10. Refactoring that you don't see • Compiler optimizations • Refactoring for performance improvementExample code:radius = ..;while(index < Length){array[index] = 2 * pi * radius;}

  11. Loop Invariant Optimization radius = ..;perimeter = 2 * pi * radiuswhile(index < Length){array[index] = perimeter;} • Compiler optimizations go through precise verification

  12. Common Refactoring Techniques • Extract method • Pull up method • Push down method • Consolidate conditional expressions • Encapsulate Collection • … • Alphabetical list at: http://www.refactoring.com/catalog/index.html

  13. Extract Method: The Problem Statement Logic Movie Frequent Renter Point Presentation

  14. Pull Up Method/Field/Constructor • Methods with identical results on subclasses • Move the method to superclass Fowler: Refactoring, Page 322

  15. Push Down Method • Behavior on a superclass is relevant only for some of its subclasses. • Move the behavior to the subclass Fowler: Refactoring - Page 328

  16. Consolidate Conditional Expression double disabilityAmount() { if (_seniority < 2) return 0; if (_monthsDisabled > 12) return 0; if (_isPartTime) return 0; // compute the disability amount double disabilityAmount() { if (isNotEligableForDisability()) return 0; // compute the disability amount Checking Eligibility for disability Fowler: Refactoring - Page 240

  17. Encapsulate Collection • Method returns a collection • Breaks encapsulation • Allows clients to manipulate collection without objects’ knowledge • Make a read-only view, provide methods to add remove elements Fowler: Refactoring - Page 208

  18. Summary • Improve the structure of code • No value gain at the moment • Easier to add features later • Less chances of errors in maintenance tasks • Key is to preserve semantics • Imprecisely ensure that by developing tests • Also, by code inspection

  19. Exercise • Can some refactoring techniques change the design structure matrix view of the system? • If yes, which refactoring techniques change the DSM view and how?

More Related