开发者

Is there any safe refactoring tool for .net (or at least c#)?

I recently read Michael C. Feathers' book Working effectively with legacy code and came across the point where he mentioned a way to test the safety of automatic refactoring tools.

My question is: Are there any safe refactoring tools for the .net platform?; that means tools which only allow real refactorings and e.g开发者_如何学运维. don't allow the inline variable refactoring on the temp variable in the following example, or at least show a warning that I am changing logic.

class Program
{
    private static int _x;

    static void Main()
    {
        int temp = Test();
        for (int i = 0; i < 10; ++i)
        {
            Console.WriteLine(temp);
        }
        Console.ReadKey();
    }


    private static int Test()
    {
        return ++_x;
    }
}

I've tested this example on the refactoring tools Resharper and Coderush + Refactor pro with the latest versions and both failed the test and allowed the refactoring to:

class Program
{
    private static int _x;

    static void Main()
    {
        for (int i = 0; i < 10; ++i)
        {
            Console.WriteLine(Test());
        }
        Console.ReadKey();
    }

    private static int Test()
    {
        return ++_x;
    }
}


Refactoring is inherently risky. Relying solely on a tool to make your code safe is unwise imo.

We use Resharper but not without the safety net of comprehensive unit tests. I am not aware of any better C# tool in this space.


To be really safe with automated refactorings is very hard.

When we first introduced refactorings in Visual C#, we asked ourselves this question: do our refactorings need to get things completely right all the time, or should we allow them to make mistakes in certain cases?

To be right all the time would require a lot of programmer effort, meaning we'd only get a few refactorings in the box. It would also make the refactorings slow, as they'd spend a lot of time in validation.

Allowing them to make mistakes would make them useless for any teams that didn't have great automated test coverage. TDD teams have good tests, but that's only one portion of the Visual Studio user base. We didn't want to make features that we had to tell people not to use!

TDD teams would catch mistakes quickly, but they would learn just as quickly not to trust our refactoring tools. They'd hesitate to use them, and seek other solutions much of the time (find-and-replace instead of Rename).

Also, as the C# team, we were in a good position to make high-fidelity refactorings. We had a unique advantage, with the C# language designers and compiler team just down the hall. We knew we should play to our strengths.

So, we decided to make fewer high-quality refactorings, instead of lots of refactorings that weren't as reliable. Today there are 6.

Is there any safe refactoring tool for .net (or at least c#)?

Looking back, I wish we had only done Rename, Extract Method, and Introduce Local Variable. Those last two are almost the same, implementation-wise. The 3 Parameter refactorings (there used to be a 7th, Promote Local Variable to Parameter, but it was cut in VS2010) were a ton of work to get right, and probably weren't worth it.

My recommendation is to do TDD, giving you a great collection of tests so you can refactor safely, whether you use a tool or do it by hand.


I disagree that your "test" shows failure.

YOU changed the logic, not the tool. You changed the code such that a method would be called repeatedly instead of once.

The tools merely did what you told them to do.


"Safe" is rather subjective....

While those two tools made not be considered "safe" based on this test in your mind both of those tools are extremely useful. No tool is perfect. If there is a situation where they do something you don't like either avoid doing it or create a work around.


I think Safe Refactor can be a tool for your case. Even though it's for Java, its concepts may apply to other OO languages.

http://www.dsc.ufcg.edu.br/~spg/saferefactor/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜