What is the quickest way to determine what code will be affected by a refactoring?
-
13-04-2021 - |
Question
I'm using Eclipse and Java, but the answer doesn't need to be specific to those. I'm sure platform specific answers will get plenty of upvotes if they're far quicker than non-platform specific ones.
Right now I'm using
grep "classname"
find . -regex .*\.java
> uses_classname.txt
to find code that might be affected by a change to the class. If there are only a few lines returned by this, I can look at it manually as it is. Otherwise, I can grep again to find the specific methods I'm modifying. Is there a better way to do this?
La solution
[Just being a bit of a librarian here:]
Finding references to a Java method/class:
How can I find all the methods that call a given method in Java?
References to Java annotations:
Finding references to 'derived' methods in Eclipse:
Eclipse Specific: Is it possible to find references of use of derived methods in a class?
How to do it with VIM:
Is it possible to find usage of java classes or methods in VIM?
And with Emacs:
How can I find the references of a class, method, variable in Emacs with Etags?
Finding (the number of) references to a method in C#:
How do you programmatically identify the number of references to a method with C#
Nice to read: how to find methods that are not used in C#/.NET:
Is there a tool for finding unreferenced functions (dead, obsolete code) in a C# app?
On refactoring of PHP code:
Finding references in Smalltalk:
Finding references in (Apple) Xcode seems to be a popular question:
Find method references in Xcode
Autres conseils
A good solution is to use JUnit and practice test-driven development. After refactoring, re-running your test cases will tell you whether any existing functionality has been damaged. Moreover, since you are actually testing the code rather than grep'ing over method names, you can get full backtrace, making debugging much easier. A good introduction to JUnit can be found here.