1Refactoringhttp://c2.com/cgi/wiki?ExtremeNormalFormYour classes are small and your methods are small; you’ve saideverything OnceAndOnlyOnce and you’ve removed the lastpiece 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 archesacross the track.Softly, a bell sounds.. all of your tests have passed.Your code is in ExtremeNormalForm.. tuned to today and poisedto strike at tomorrow.2RefactoringChanges made to a program that only changeits organization, not its function.Behavior preserving program transformations.Why refactoring is importantOnly defense against software decay.Often needed to fix reusability bugs.Lets you add patterns after you have written aprogram; lets you transform program intoframework.Lets you worry about generality tomorrow;just get it working today.Necessary for beautiful software.3Piecemeal GrowthThe way living things growThe way software growsMore than just addition -- transformationDuplicate codeEliminate duplication by● making new methods● making new objects● moving methods to common superclass4Long methodsEach 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.Move code to its right classteacher classes add: thisClass.teacher classLoad: (teacher classLoad + 1)teacher addClass: thisClass5Large classesMore than seven or eight variablesMore than fifty methods● You probably need to break up the classComponents (Strategy, Composite, Decorator)InheritanceLong parameter listsIf you see the same set of parameters repeatedin several methods, bundle them into a newobject. Create accessors to get the originalparameters from the object. Change themethods to use the new object instead of theparameters.Then figure out what other funtionality needsto move to the object.6Case StatementsUse 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 amethod in a classHow to refactorMake changes as small as possible.Test after each change.Many small changes are easier than one bigchange.Each change makes it easier to see that morechanges are needed.7Refactoring browserA better browserAutomates many refactorings - renaming,moving code, splittingUndoSmalllint tool helps you find places that needto be refactored.When to refactorIf 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
View Full Document