-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Inline function refactoring #27070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Related: #18459 |
The boundary of refactor seems to be difficult to determine 🤫 |
I think this is one of the most important (but underused) refactorings. For example, if you want to reorder these parameters: function apiFunction(arg1: string, arg2: string) {
// ...
}
function consumerCode() {
apiFunction("arg1", "arg2");
apiFunction("foo", "bar");
} ... you can first rename function apiFunction(arg1: string, arg2: string) {
// ...
}
function apiFunction_(arg1: string, arg2: string) {
apiFunction(arg1, arg2);
}
function consumerCode() {
apiFunction_("arg1", "arg2");
apiFunction_("foo", "bar");
} Now you can apply the local refactoring to function apiFunction(arg2: string, arg1: string) {
// ...
}
function apiFunction_(arg1: string, arg2: string) {
apiFunction(arg2, arg1);
}
function consumerCode() {
apiFunction_("arg1", "arg2");
apiFunction_("foo", "bar");
} And now you can inline function apiFunction(arg2: string, arg1: string) {
// ...
}
function consumerCode() {
apiFunction("arg2", "arg1");
apiFunction("bar", "foo");
} The inline refactoring is very complicated to implement though. |
Yes, I miss this refactoring in VSC. I would also like inline constant expression which is even easier:
becomes
In Opdyke's dissertation which gave a rigorous treatment of refactorings, he stressed that every refactoring has an equal and opposite refactoring: extract function has inline function as an inverse. VSC omits lots of the inverses, it seems. |
Looking forward to this! Coming from Uncle Bob's and Primeagen's video. This seems to be super helpful when understanding a function with many nested functions and refactoring towards better abstractions as per clean code. |
This seems to already be supported as "inline variable". Cursor on the I do miss "inline function" though. |
I think it would be acceptable to error and refuse to perform the refactoring if variable names conflict. Refactorings should be mechanical and reversible, so "inline function" followed by "extract function" should get you back where you started. I think it's OK to expect the developer to rename some variables to unblock "inline function" . Ideally some guidance - "cannot inline function because of conflicting identifier 'foo'" |
Search Terms
inline function method refactoring
Suggestion
I would like a refactoring that inlines a function from
to
Use Cases
This is a very common refactoring and thus widely used while cleaning up code.
Examples
In the above code sample, block 1, line 1: selecting foo, the user should be able to inline this function to every occurence and optionally delete the function definition.
In the above code sample, block 1, line 2: selecting foo, the user should be able to inline the function to this occurence, and, if it's the only one, optionally delete the function definition.
I am not sure how the option can be handled in vscode. Eclipse brings up a pop-up with the two options, but afaik vs code tries to be minimalist.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: