But not when I Extract Method.
Consider these two rules:
- I trust #3 and #4 on the "definitions of Refactoring" list.
- When two methods in the same class are textually identical, they are semantically identical.
- Extract Method at each site, giving the new methods nearly-identical names ("Foo", "Foo2" is fine).
- Use a text diffing tool to compare the two methods, including the signature.
- Use automated refactoring tools to normalize (eliminate the differences). For example, rename a parameter in one method to match the other.
- When the two methods are textually identical, except for their names, forward one to the other.
- Inline the forwarding method.
Note that you don't have to understand what the code does to make this work. You just have to see the duplication and strive to eliminate it.
I like to do fully-automated, highly-reliable refactorings instead of manual edits, where possible. Because it makes me confident that I'm not breaking anything, I can do that without test coverage, which is key to recovering legacy code.
No comments:
Post a Comment