bad smell (long method)

symptoms: a method contains too many lines of code.
if you feel the need to comment on something inside a method, you should take this code and put it into a new method
1.extract method with a good name
(Extract Method refactoring has the following limitations:
Refactoring does not work with multiple output values in automatic mode. You have to change your code before applying the refactoring.
Refactoring does not work for a code fragment which conditionally returns from the containing method and is not placed at the end of it.即 function 裡面有提前return)

how to extract
find the local variables or parameters first
non modified variable in the segment pass as a parameter
modified variables in the segment initialized as local variables in the new method , in the end return the variables

2.replace temp with query (remove temp)

3.introduce parameter object (remove parameters)

4.preserve whole object

5.replace method with method object

6.decompose conditional








references :

the best way to start refactoring is to build tests

#target  : clean code
clean code is obvious for other programmers
clean code doesn’t contain duplication
clean code contains a minmal number of classes and other moving parts
clean code passes all tests
clean code is easier and cheaper to maintain

#when to refactor:
rule of three (first time just get it done , second time do the same thing anyway,refactoring at the third time)
when u adding a feature
when u fixing a bug
during a code review

#how to refactor
refactoring should be done as a series of small change, each of which makes the existing code slightly better while still leaving the program in working order.
the code should become cleaner, if not it’s worthwhile to think about completely rewriting parts of the code.
new functionality shouldn’t be created during refactoring
all existing tests must pass after the refactoring