Is there any Visual Studio Add-In that can do the remove method refactoring?
Suppose you have the following method:
Result DoSomething(parameters)
{
return ComputeResult(parameters);
}
Or the variant where Result is void.
The purpose of the refactoring is to replace all the calls to DoSomething with calls to ComputeResult or the expression that uses the parameters if ComputeResult is not a method call.
If I understand the question, then Resharper calls this 'inline method' - Ctrl - R + I
I would do it the simpliest way:
rename ComputeResult method to ComputeResultX
rename DoSomething method to ComputeResult
remove DoSomething method (which is now ComputeResult)
rename ComputeResultX method back to ComputeResult
Maybe VS will show some conflict because of the last rename, but ignore it.
By "rename" I mean: overwrite the name of the method and after it use the dropdown (Shift+Alt+F10) and select "rename". It will replace all occurences with the new name.
There are a few products available to add extra refactoring options to Visual Studio 2005 & 2008, a few of the better ones are Refactor! Pro and Resharper.
As far as remove method, there is a description in the canonical Refactoring book about how to do this incrementally.
Personally, I follow a pattern something along these lines (assume that compiling and running unit tests occurs between each step):
Create the new method
Remove the body of the old method, change it to call the new method
Search for all references to the old method (right click the method name and select "Find all Reference"), change them to calls to the new method
Mark the old method as [Obsolete] (calls to it will now show up as warnings during the build)
Delete the old method
When it comes to refactoring like that, try out ReSharper.
Just right click on the method name, click "Find usages", and refactor until it cannot find any references.
And as dlamblin mentioned, the newest version of ReSharper has the possibility to inline a method. That should do just what you need.
ReSharper is definitely the VS 2008 plug in to have for refactoring. However it does not do this form of refactoring in one step; you will have to Refactor->rename DoSomething to ComputeResult and ignore the conflict with the real ComputeResult. Then delete the definition which was DoSomething. It's almost one step.
However maybe it can do it one step. If I read that correctly.
You can also right click the method name and click "Find all References" in Visual Studio.
I personally would just do a CTRL + SHIFT + H to Find & Replace
Related
I was wondering, does visual studio have a feature such that I gave it a 2 method names and it then works out if at somewhere along the call stack the first method is called by the second (statically, without having to debug).
e.g.
Say I have a method FireBullet, and I want to see if IsOutsideWestBoundary can be invoked at some point
FireBullet()->HitTest()->CheckBoundaries()->IsOutsiteWestBoundary()
You can see that FireBullet can eventually cause IsOutsideWestBoundary to be invoked at some point.
I understand this can potentially become a very large problem, especially with deep call stacks and multiple methods called at each level, but still, for relatively small call stacks depths, this could be very useful.
Surely something like this must exist?
Thanks
Thomas
The Visual Studio extension NDepend can do that. It lets write code rules and code queries through C# LINQ queries. The following LINQ code query, executed live in Visual Studio, answers your need:
from m in Methods
where m.Name == "FireBullet()"
let depth0 = m.DepthOfIsUsing("MyNamespace.Program.IsOutsiteWestBoundary()")
where depth0 >= 0 orderby depth0
select new { m, depth0 }
Notice that the code query result also provides the depth of call. It can be stored in your NDepend project, and it can be transformed into a rule by adding the prefix warnif count > 0.
This query gets inspired from the query generated by NDepend when right clicking a method and Select Methods ... that call me (directly or indirectly).
If you click the button Export to Graph you get such call graph (more info on this here):
A 14-day full featured trial is available here.
Disclaimer: I work for NDepend.
Is there a way to perform the opposite of the "Extract Method" refactor in Visual Studio?
I have a legacy codebase that has ~50 very short, private functions that are only used once each, and I have been tasked with inlining them.
If an automatic inline refactoring isn't possible, is it possible to reduce the amount of time it takes to inline these function calls? My current workflow is:
Copy the code in the function.
Find where it is called.
Replace the function call with the copied code.
Replace local variable names from the function.
Delete the function.
The refactoring you are looking for is called "Inline Method".
While Visual Studio does not provide this refactoring out of the box, you can get access to it (and many other useful refactorings) by installing the Jetbrains ReSharper extension for Visual Studio.
With the extension installed, all you need to do is click on the method declaration or method call, and invoke the 'Inline Method' refactoring. This will automatically inline all occurrences of the method and delete it.
You might consider collecting the functions into a header file and decorating them with inline. I realize this is not exactly an answer to what you asked, but may be a better solution to your problem because this way the compiler would perform the inlining (if it sees fit).
It depends on the situation, but keeping the function definitions might lead to clearer code, thus might be of value even if called only once.
This is also quicker and less error prone than manual "inline method" refactorings.
When one steps into the debug function with multiple input parameters, it is a quite a time waster to enter and leave a copy constructor of each input parameter. Most often than not, these copy constructors are trivial (increase the reference counter), and what a developer really want is to "step right into function", and skip debugging through of input parameters creation,
be it a call to a copy ctor, or a call to a function which creates the instance of parameter object.
Is there a way in a Visual Studio to configure the debugging in a such way, as to avoid the debugging of the construction/copy construction
of the parameters and to go right to the function's body?
alt-shift-F11 in Visual Studio 10 or higher brings up a list of all the functions that will be called and you can select the function you would like to go directly to. I find this incredibly useful, but unsure if you are using VS10 or an earlier version.
Related to this question: Create a new jquery chained method, how do I do an advanced find and replace in Visual Studio (or even Notepad++ or something else) to find:
Foo($("selector"));
And replace with:
$("selector").foo();
This is because I've created a new jQuery chained method:
$("#SaveButton").click(function () {
$("#SubTotal").foo();
$("#TaxTotal").foo();
$("#Total").foo();
});
To replace this
$("#SaveButton").click(function () {
Foo($("#SubTotal"));
Foo($("#TaxTotal"));
Foo($("#Total"));
});
And now I need to find and replace all instances of the old method call with the new chained method.
Use regular expressions in the Find options of Find/Replace dialog.
Hope that helps.
NB: I run screaming madly at the mere notion of having a stab at your particular expression but it is really just a transform of a number of matches so should be doable.
Is it possible to get Visual Studio to do mathematical expression evaluation/reduction?
For example if I type '-0.005 + -0.345' how do I get Visual Studio to reduce that (i.e. replace it with the reduction)? Do I have to write a macro? If so, are there any pre-existing macros to do this type of expression reduction?
Just to be clear, I want to be able to highlight an expression and have it replaced with the reduced result. Many are suggesting the immediate window but I fail to see how that will suffice?
Edit I should point out that this is while editing not running or debugging. The immediate window is of little to no use. I also consider this a language neutral question. I would certainly be interested in seeing alternative macros to the one I had posted.
Edit Going Once... Going Twice... (i.e. any other suggestions before I consider accepting my own answer?)
Thank you for the above answers.
There probably are better ways, but here's a quick and dirty macro that does what I need.
References to the System.Data and System.XML namespaces need to be added.
Highlight the expression you want to evaluate and run the macro (it uses the calculated column in the DataTable to evaluate the expression.) It will replace the expression with the reduced result.
Edit - Updated code below. It worked extremely well for reducing a large number of expressions. As pointed out by others there is the immediate window but this will not work for editing purposes. This macro is a language independent solution for basic expressions "(), +, -, *, /".
Sub Eval()
Dim ts As EnvDTE.TextSelection = DTE.ActiveDocument.Selection
Using dt As New DataTable()
dt.Columns.Add("Expression", GetType(Double), ts.Text)
dt.Rows.Add(dt.NewRow)
ts.Text = CDbl(dt.Rows(0).Item("Expression"))
End Using
End Sub
Visual Studio by default will not do any mathematical expression evaluation / reduction. I'm not sure if you can get support for that via items like ReSharper, but if it is available it will be in an add-in.
Also, it would be helpful to know the language you are working in?
Some languages may be helpful in this area. F# for instance makes it easy to evaluate expressions in the IDE via the interactive window and will display out the result. This could easily be added back into your code but it doesn't appear to be exactly what you're looking for.
Here's an answer: Yes, it is possible using the following steps. (While technically performing what you're asking for, I'm not sure it will be extremely useful. :-)
Set a breakpoint in your program that's likely to get hit when you debug the program.
Then, run your program under the Visual Studio debugger.
When the breakpoint is hit, open the Watch window.
In the Watch window, add a new watch by clicking in the Name column.
Enter your expression '-0.005 + -0.345' (without the quotes) then hit [Enter].
... You should see the Value column get populated with -0.35.
Of course, that isn't in the context of the editor window ... which is, presumably, where you'd want to perform the reduction. So again, not very useful, I imagine. An add-in is the likely way to do that in the editor window.
You could just go to the immediate window and type "?<yourExpression>"