1 / 14

Refactoring

Refactoring. http://c2.com/cgi/wiki?ExtremeNormalForm Your classes are small and your methods are small; you've said everything OnceAndOnlyOnce and you've removed the last piece of unnecessary code.

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. http://c2.com/cgi/wiki?ExtremeNormalForm • Your classes are small and your methods are small; you've said everything OnceAndOnlyOnce and you've removed the last piece of unnecessary code. • Somewhere far away an Aikido master stands in a quiet room. He is centered.. aware.. ready for anything. • Outside a sprinter shakes out her legs and settles into the block, waiting for the crack of the gun while a cool wind arches across the track. • Softly, a bell sounds.. all of your tests have passed. • Your code is in ExtremeNormalForm.. tuned to today and poised to strike at tomorrow.

  3. Refactoring • Changes made to a program that only change its organization, not its function. • Behavior preserving program transformations.

  4. Why refactoring is important • Only defense against software decay. • Often needed to fix reusability bugs. • Lets you add patterns after you have written a program; lets you transform program into framework. • Lets you worry about generality tomorrow; just get it working today. • Necessary for beautiful software.

  5. Piecemeal Growth • The way living things grow • The way software grows • More than just addition -- transformation

  6. Duplicate code • Eliminate duplication by • making new methods • making new objects • moving methods to common superclass

  7. Long methods • Each method should do one thing. • One comment for each method. • Method should fit on the screen. • Method is all on same level of abstraction. • Method should be in right class.

  8. Move code to its right class • teacher classes add: thisClass. • teacher classLoad: (teacher classLoad + 1) • teacher addClass: thisClass

  9. Large classes • More than seven or eight variables • More than fifty methods • You probably need to break up the class • Components (Strategy, Composite, Decorator) • Inheritance

  10. Long parameter lists • If you see the same set of parameters repeated in several methods, bundle them into a new object. Create accessors to get the original parameters from the object. Change the methods to use the new object instead of the parameters. • Then figure out what other funtionality needs to move to the object.

  11. Case Statements • Use polymorphism, not case statement. • Make class hierarchy, one class for each case. • Make a method for each case statement. • Make each branch of case statement be a method in a class

  12. How to refactor • Make changes as small as possible. • Test after each change. • Many small changes are easier than one big change. • Each change makes it easier to see that more changes are needed.

  13. Refactoring browser • A better browser • Automates many refactorings - renaming, moving code, splitting • Undo • Smalllint tool helps you find places that need to be refactored.

  14. When to refactor • If a new feature seems hard to implement, refactor. • If a new feature created some ugly code, refactor. • When you can’t stand to look at your code, refactor.

More Related