160 likes | 339 Views
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/ ) Handout from Fowler’s Refactoring book
E N D
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/) Handout from Fowler’s Refactoring book Optional: Refactoring: Benefits and Disadvantages…”. Web article.
Overview • Refactoring: what and why • Reminders: TDD and Unit Testing • Tool support • (Later: lab exercise)
What Is Refactoring? • Definition: • The process of changing software in a way that the external behavior is not alteredbut 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
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
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).
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
Reminder slides • Remember unit tests? TDD?
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
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
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
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.
Refactoring and Design Smells • Fowler, design smells, and catalog of refactorings • The book • http://www.refactoring.com/ • List of smells on the web:http://wiki.java.net/bin/view/People/SmellsToRefactorings
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
Reminder • Refactorings are often small changes • This are low-level design or code level mods
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?
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