Administrivia. Programming Language Fall Example. Evolving Software. Project 3 coming out Midterm October 28. Refactoring October 14, 2004

Similar documents
Evolving Software. CMSC 433 Programming Language Technologies and Paradigms Spring Example. Some Motivations for This Refactoring

Credit where Credit is Due. Lecture 25: Refactoring. Goals for this lecture. Last Lecture

Patterns in Software Engineering

Refactoring. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 27 11/29/11. University of Colorado, 2011

Refactoring. Chen Tang March 3, 2004

void printowing(double amount) { printbanner(); printdetails(); void printdetails(double amount) {

Refactoring. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 27 04/19/11. University of Colorado, 2011

CSE 403 Lecture 21. Refactoring and Code Maintenance. Reading: Code Complete, Ch. 24, by S. McConnell Refactoring, by Fowler/Beck/Brant/Opdyke

The Benefits And Reasons for Doing Refactoring

Refactorings. Refactoring. Refactoring Strategy. Demonstration: Refactoring and Reverse Engineering. Conclusion

McCa!"s Triangle of Quality

CISC327 - Software Quality Assurance

Refactoring. Paul Jackson. School of Informatics University of Edinburgh

Refactoring. George Dinwiddie idia Computing, LLC

Software Design COSC 4353/6353 D R. R A J S I N G H

MSO Refactoring. Hans Philippi. October 2, Refactoring 1 / 49

What? reorganising its internal structure

Designing with patterns - Refactoring. What is Refactoring?

COURSE 11 DESIGN PATTERNS

Refactoring. Refactoring Techniques

Refactoring, 2nd Ed. A love story. Michael Hunger

AntiPatterns. EEC 421/521: Software Engineering. AntiPatterns: Structure. AntiPatterns: Motivation

Lecture Notes. Polymorphism. Polymorphism. If Polymorphism is Not Available. To be placed at:

Software LEIC. Lecture 23

Code Smells & Refactoring

Refactoring 101. By: Adam Culp

Understading Refactorings

Refactoring. Section (JIA s) OTHER SOURCES

Code Smells & Refactoring

Maintainability and Agile development. Author: Mika Mäntylä

Keywords Code clean up, code standard, maintainability, extendibility, software refactoring, bad smell code.

Implementing evolution: Refactoring

CSCD01 Engineering Large Software Systems. Refactoring. Joe Bettridge. Winter With thanks to Anya Tafliovich (Based on Refactoring by M.

11/2/09. Code Critique. What goal are we designing to? What is the typical fix for code smells? Refactoring Liskov Substitution Principle

Software Engineering I (02161)

CSC 408F/CSC2105F Lecture Notes

Refactoring. Herez Moise Kattan NUSP: João S. Brito Júnior NUSP:

Clean

Exploring Software Refactoring Decisions in a Lean Startup

Small changes to code to improve it

CS247. Today s topics. Design matters. What is refactoring?

Hugbúnaðarverkefni 2 - Static Analysis

Refactoring. Software Engineering, DVGC18 Faculty of Economic Sciences, Communication and IT Eivind Nordby

Badge#8: Geek Refactoring

Kerievsky_book.fm Page 355 Thursday, July 8, :12 PM. Index

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

Expanding Our Horizons. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 9 09/25/2011

A Study of Bad Smells in Code

Composing Methods. Extract Method - Code that can be grouped - Meaningful name for method

How We Refactor, and How We Know It

CISC327 - So*ware Quality Assurance

Refactoring Exercise

Agile Architecture. The Why, the What and the How

Reengineering II. Transforming the System

The compiler is spewing error messages.

Objectives: On completion of this project the student should be able to:

Type Checking in COOL (II) Lecture 10

Microservice Splitting the Monolith. Software Engineering II Sharif University of Technology MohammadAmin Fazli

Automating Big Refactorings for Componentization and the Move to SOA

Code Refactoring. CS356 Object-Oriented Design and Programming November 21, 2014

Refactoring, Part 2. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 27 12/01/09. University of Colorado, 2009

Refactoring. Refactoring. Refactoring. Refactoring. Refactoring. Refactoring. Lesson Five: Conditionals

JUnit 3.8.1, 64. keep it simple stupid (KISS), 48

Does Your Code Measure Up?

OOD Smells and Principles

Implementing evolution: Refactoring

SOFTWARE ENGINEERING SOFTWARE EVOLUTION. Saulius Ragaišis.

Refactoring. So#ware Quality Quality Audit and Cer4fica4on. Master in Computer Engineering. Roberto García

Design Patterns Reid Holmes

Information Expert (or Expert)

Refactoring. Thierry Sans. with slides from Anya Tafliovich

3 Continuous Integration 3. Automated system finding bugs is better than people

Errors and Exceptions

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

15-498: Distributed Systems Project #1: Design and Implementation of a RMI Facility for Java

PROGETTAZIONE DI SISTEMI ORIENTATI AGLI OGGETTI (OOD)

CSE 70 Final Exam Fall 2009

Stage 11 Array Practice With. Zip Code Encoding

Course December Adrian Iftene

Supplementary Notes for Software Reengineering - July 8, 2011

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Refactoring. Improving the Design of Existing Code. Second Edition. Martin Fowler with contributions by Kent Beck

Making Inheritance Work: C++ Issues

Making Inheritance Work: C++ Issues

7. Software Development

Object-Oriented Concepts and Design Principles

Refactoring with Eclipse

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 00. WELCOME TO OBJECTVILLE. Speaking the Language of OO

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

AgileBill Krebs. Agile3d Academy. Enterprise Open Distributed. Agile Quality. Years 30 Books 240. Certs 8. Badges 6. O, Rq, Pm, Qa, Ns, Agile 01

Software Engineering /48

Principles of Object-Oriented Design

Refactoring Without Ropes

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

Be careful when deciding whether to represent data as integers or floats, and be sure that you consider all possible behaviors in computation.

Appendix A - Glossary(of OO software term s)

Program development plan

Reverse Engineering and Refactoring Related Concept in Software Engineering

Chapter 6 Introduction to Defining Classes

COURSE 2 DESIGN PATTERNS

Transcription:

CMSC 433 Programming Language Fall 2004 Project 3 coming out Midterm October 28 Administrivia Refactoring October 14, 2004 Lots of material taken from Fowler, Refactoring: Improving the Design of Existing Code 1 2 Evolving Software Example Problem The requirements of real software often change in ways that cannot be handled by the current design Moreover, trying to anticipate changes in the initial implementation can be difficult and costly Solution Redesign as requirements change Refactor code to accommodate new design (p204) Replace Magic Number with Symbolic Constant double potentialenergy(double m, double h) { return m * 9.81 * h; becomes... static final double G = 9.81; double potentialenergy(double m, double h) { return m * G * h; 3 4

Some Motivations for This Refactoring Conventional Wisdom: The Design is Fixed Magic numbers have special values But why they have those values is not obvious So we like to give them a name Magic numbers may be used multiple times Easy to make errors May make a typo when putting in a number May need to change a number later (more digits of G) Software process looks like this: Step 1: Design, design, design Step 2: Build your system Once you re on step 2, don t change the design! You might break something in the code You need to update your design documents You need to communicate your new design with everyone else 5 6 What if the Design is Broken? Refactoring Philosophy You re kind of stuck Design changes are very expensive When you re cleaning up the code, you re not adding features Result: An inappropriate design Makes code harder to change Makes code harder to understand and maintain Very expensive in the long run 7 It s hard to get the design right the first time So let s not even pretend Step 1: Make a reasonable design that should work, but... Plan for changes As implementers discover better designs As your clients change the requirements (!) But how can we ensure changes are safe? 8

Refactoring Philosophy (cont d) Two Hats Make all changes small and methodical Follow mechanical patterns (which could be automated in some cases) called refactorings, which are semantics-preserving Retest the system after each change By rerunning all of your unit tests If something breaks, you know what caused it Notice: we need fully automated tests for this case We re going to be running them a lot 9 Refactoring hat You are updating the design of your code, but not changing what it does. You can thus rerun existing tests to make sure the change works. Bug-fixing/feature-adding hat You are modifying the functionality of the code. May switch hats frequently But know when you are using which hat, to be sure that you are reaching your end goal. 10 Principles of Refactoring Principles of Refactoring In general, each refactoring aims to Decompose large objects into smaller ones Distribute responsibility Like design patterns Adds composition and delegation (read: indirection) In some sense, refactorings are ways of applying design patterns to existing code 11 Refactoring improves design Fights against code decay as people make changes Refactoring makes code easier to understand Simplifies complicated code, eliminates duplication Refactoring helps you find bugs In order to make refactorings, you need to clarify your understanding of the code. Makes bugs easier to spot. Refactoring helps you program faster Good design = rapid development 12

When to Refactor When to Refactor: An Analogy The Rule of Three Three strikes and you refactor The third time you duplicate something, refactor Refactor before you add a feature Make it easier for you to add the feature Refactor when you have a bug Simplify the code as you re looking for the bug (Could be dangerous...) Refactor before you do code reviews...if you d be embarrassed to show someone the code 13 Unfinished refactoring is like going into debt Debt is fine as long as you can meet the interest payments (extra maintenance costs) If there is too much debt, you will be overwhelmed [Ward Cunningham] 14 Barriers to Refactoring Barriers to Refactoring (cont d) May introduce errors Mitigated by testing Clean first, then add new functionality Cultural issues Producing negative lines of code We pay you to add new features, not to improve the code! If it ain t broke, don t fix it Tight coupling with implementations E.g., databases that rely on schema details Public interfaces If others rely on your API, you can t easily change it I.e., you can t refactor if you don t control code callers Designs that are hard to refactor It might be hard to see a path from the current design to the new design You may be better off starting from scratch 15 16

What Code Needs to be Refactored? Feature Envy Bad code exhibits certain characteristics that can be addressed with refactoring These are called smells Different smells suggest different refactorings A method seems more interested in a class other than the one it is actually in E.g., invoking lots of get methods Move Method Move method from one class to another Extract Method Pull out code in one method into a separate method 17 18 Move Method Should other methods also be moved? What about sub- and superclasses? What about access control (public, protected)? 19 void printowning(double amt) { printbanner(); System.out.println( name + name); System.out.println( amount + amt); Extract Method void printdetails(double amt) { Are you ever going to reuse this new method? Local variable scopes? Extra cost of method invocation? System.out.println( name + name); System.out.println( amount + amt); void printowning(double amt) { printbanner(); printdetails(amt); 20

Long Method Replace Temp with Query A method is too long. Long methods are harder to understand than lots of short ones. Can decompose with Extract Method Replace Temp with Query Remove code that assigns a method call to a temporary, and replace references to that temporary with the call Replace Method with Method Object Use the command pattern to build a closure 21 double baseprice = num * price; if (baseprice > 1000) return baseprice * 0.95; else return baseprice * 0.98; double baseprice() { return num * price; if (baseprice() > 1000) return baseprice() * 0.95; else return baseprice() * 0.98; Local variables make it hard to use some refactorings, e.g., Extract Method What about performance? 22 Switch Statements Replace Type Code with State/Strategy Usually not necessary in delegation-based OO programming Replace Type Code with State/Strategy Define a class hierarchy, a subclass for each type code Replace Conditional with Polymorphism Call method on state object to perform the check; switching is based on dynamic dispatch 23 24

Replace Conditional with Polymorphism double getspeed() { switch (kind) { case EUROPEAN: return getbasespeed(); case AFRICAN: return getbasespeed()-loadfactor()*numberofcoconuts; case NORWEGIAN_BLUE: return (isnailed)? 0 : getbasespeed(voltage); throw new RuntimeException( Should be unreachable ); 25 Duplicated Code The same expression used in different places in the same class Use Extract Method to pull it out into a method The same expression in two subclasses sharing the same superclass Extract Method in each, then PullUp method into parent Duplicated code in two unrelated classes Extract Class - Break a class that does too many things into smaller classes 26 Pull Up Method Extract Class Might do other refactorings if methods don t quite match What if doesn t appear in all subclasses? How do we decide what goes in new class? Do fields still need to be accessed in orig class? 27 28

Long Parameter List Replace Parameter with Method Lots of parameters occlude understanding Replace Parameter with Method Remove method parameters and instead use some other way to get the parameter value (e.g., method call) Introduce Parameter Object Group parameters that go together into a container object double baseprice = num * price; double discount = getdiscount(); double finalprice = discountedprice(baseprice, discount); double baseprice = num * price; double finalprice = discountedprice(baseprice); discountedprice can call getdiscount() itself 29 30 Introduce Parameter Object Divergent Change One class is commonly changed in different ways for different reasons To add a new database, change these three methods To add a new financial currency, change these four Suggests maybe this shouldn t be one object Apply Extract Class to group together variations 31 32

Shotgun Surgery Every time I make change X, I have to make lots of little changes to different classes Opposite of Divergent Change Move Method Move Field Switch field from one class to another Inline Class A class isn t doing very much, so inline its features into its users (reverse of Extract Class) Other Bad Smells Data Clumps Objects seem to be associated, but aren t grouped together Primitive Obsession Reluctance to use objects instead of primitives Parallel Inheritence Hierarchies Similar to Shotgun Surgery; every time we add a subclass in one place, we need to add a corresponding subclass to another 33 34 Other Bad Smells (cont d) Other Bad Smells (cont d) Lazy Class A class just isn t useful any more Speculative Generality Oh, I think we need the ability to do this kind of thing someday. Temporary Field Instance variable only used in some cases. Confusing to figure out why it s not being set everywhere. Message Chains Long sequences of gets or temporaries; means client is tied to deep relationships among other classes Middle Man Too much delegation. If a class delegates lots of its functionality to another class, do you need it? Inappropriate Intimacy Classes rely on too many details of each other 35 36

Other Bad Smells (cont d) Other Bad Smells (cont d) Alternative Classes with Different Interfaces Methods do the same thing but have different interfaces Incomplete Library Class Library code doesn t do everything you d like Data Class Classes that act as structs, with no computation Refused Bequest Subclass doesn t use features of superclass Comments! If code is heavily commented, either It s very tricky code (e.g., a hard algorithm), or The design is bad, and you re trying to explain it When you feel the need to write a comment, first try to refactor the code so that any comment becomes superfluous. 37 38 Refactoring with Tools More information Many refactorings can be performed automatically This reduces the possibility of making a silly mistake Eclipse provides support for refactoring in Java http://www.eclipse.org Textbook: Refactoring by M. Fowler Catalog of refactorings: http://www.refactoring.com/catalog/index.html Refactoring to patterns http://industriallogic.com/xp/refactoring/ 39 40

This document was created with Win2PDF available at http://www.daneprairie.com. The unregistered version of Win2PDF is for evaluation or non-commercial use only.