Presentation is loading. Please wait.

Presentation is loading. Please wait.

Refactoring - 1 CS494: Intro. to Refactoring Readings: –Refactoring for everyone: How and why to use Eclipse's automated refactoring features. By David.

Similar presentations


Presentation on theme: "Refactoring - 1 CS494: Intro. to Refactoring Readings: –Refactoring for everyone: How and why to use Eclipse's automated refactoring features. By David."— Presentation transcript:

1 Refactoring - 1 CS494: Intro. to Refactoring Readings: –Refactoring for everyone: How and why to use Eclipse's automated refactoring features. By David Gallardo (http://www-106.ibm.com/developerworks/opensource/library/os-ecref/)http://www-106.ibm.com/developerworks/opensource/library/os-ecref/ –Handout from Fowler’s Refactoring book –Optional: Refactoring: Benefits and Disadvantages…”. Web article.Web article

2 Refactoring - 2 Overview Refactoring: what and why –Reminders: TDD and Unit Testing Tool support (Later: lab exercise)

3 Refactoring - 3 What Is Refactoring? Definition: –The process of changing software in a way that the external behavior is not altered but the internal structure is improved. –Or, an instance of such a change. (E.g. “I carried out a refactoring….”) Cleaning up code. Improving design. But after the code works. –Fights against software entropy

4 Refactoring - 4 Why Refactor? Continuous improvement of design –“Tidying up.” Avoids decay. –Needed if “make it work” is first priority –Best design choices may not evident at first Makes software easier to understand –Refactoring done to clean up hacks –Done after reflection. –May be done to remove ambiguity, misleading code, a hack, etc. –If you can change it successfully, you understand it

5 Refactoring - 5 Why Refactor? (2) Helps you find bugs –A change to improve structure may reveal a flaw in old implementation Helps you developing working code more quickly –Counter-intuitive! –Short cycles of add-functionality then improve-design. (Kent Beck’s “two hats” idea).

6 Refactoring - 6 But Hold On… Possible objections…. –Touching the design is asking for trouble! –Once it’s working, why bother? –After I think it’s working, don’t I have to re- verify the design changes again? What we need to make refactoring successful is –Unit tests

7 Refactoring - 7 Reminder slides Remember unit tests? TDD?

8 Refactoring - 8 Unit Testing in TDD Motto: “Clean code that works.” (Ron Jeffries) Unit testing has “broader goals” that just insuring quality –Improve developers lives (coping, confidence) –Support design flexibility and change –Allow iterative development with working code early

9 Refactoring - 9 Unit Testing Benefits Developers can work in a predictable way of developing code Programmers write their own unit tests Get rapid response for testing small changes Build many highly-cohesive loosely- coupled modules to make unit testing easier

10 Refactoring - 10 Red/Green/Refactor The TDD mantra of how to code: –Red: write a little test that doesn’t work, perhaps even doesn’t compile –Green: Write code to make the test work quickly (perhaps not the best code) –Refactor: Eliminate duplication and other problems that you did to just make the test work

11 Refactoring - 11 When Should Refactor? Remember: afterwards! (After what?) “The Rule of Three” –First, just do it. Second time, do it badly but tolerate it. Third time, do it well: refactor. Some guidelines: –When you need to add a function. Existing design makes this hard. Improve it. –When you need to fix a bug. –As part of a code review.

12 Refactoring - 12 Refactoring and Design Smells Fowler, design smells, and catalog of refactorings –The book –http://www.refactoring.com/http://www.refactoring.com/ –List of smells on the web: http://wiki.java.net/bin/view/People/SmellsTo Refactorings http://wiki.java.net/bin/view/People/SmellsTo Refactorings

13 Refactoring - 13 Tool Support for Refactoring Refactorings: standard re-structurings to solve common problems –Can do them by hand –Put might be tricky IDEs and separate tools support this –IntelliJ, ReSharper, JRefactory, C# Refactory –Eclipse, JBuilder, next release of Visual Studio

14 Refactoring - 14 Reminder Refactorings are often small changes –This are low-level design or code level mods

15 Refactoring - 15 Why tools? Ponder these: In Java, you decide to change a class’s name. What needs to changed? In Java, you want to change the name of a method. What needs to be changed?

16 Refactoring - 16 Java Refactorings in Eclipse See help info in Eclipse. And Gallardo article. Renaming resources –classes, methods, fields –moving to another package –inner and anonymous classes Class hierarchy modifications –includes make an interface from a class Code level mods –make a code segment a function


Download ppt "Refactoring - 1 CS494: Intro. to Refactoring Readings: –Refactoring for everyone: How and why to use Eclipse's automated refactoring features. By David."

Similar presentations


Ads by Google