October 7, 2017
Manual Refactoring : Inlining Variables & Methods
My fourth post on manual refactoring is about inlining code. Essentially, this means replacing a reference to something with the implementation of the thing itself.
Fot example, I can simplify this Python code by inlining the result variable.
To do this, simply copy the expression assigned to the variable in its declaration and paste it over the reference(s) to the variable.
RUN THE TESTS!
Then delete the original declaration.
NOW RUN THE TESTS!
We can also inline the method sum_previous to simplify things further.
If I were to paste the body of this method in place where it's currently being called, the code will break because the variable being passed in as a parameter value is called index, but the method parameter is called . So first I need to rename the parameter.
AND RUN THE TESTS!
Next, paste the body of the method we want to inline where it's being called.
THEN RUN THE TESTS!
And now that sum_previous isn't being called from anywhere, we can delete it.
DON'T FORGET TO RUN THE TESTS!
Posted 1 year, 10 months ago on October 7, 2017