是否有.NET(或至少是C#)的任何安全重构工具? [英] Is there any safe refactoring tool for .net (or at least c#)?

查看:191
本文介绍了是否有.NET(或至少是C#)的任何安全重构工具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近看了迈克尔羽毛的书与遗留代码有效运作并在那里他提到一个方法来测试的自动重构工具的安全点就来了。



我的问题是:是否有任何安全重构工具的.NET平台;这意味着工具只允许真正的重构和例如不允许内变量温度重构在下面的示例中的变量,或者至少表明一个警告。我改变的逻辑

 类节目
{
私有静态诠释_x;

静态无效的主要()
{
INT TEMP =测试();
表示(INT I = 0; I&小于10 ++ⅰ)
{
Console.WriteLine(临时);
}
Console.ReadKey();
}


私有静态诠释测试()
{
返回++ _ X;
}
}



我测试过的重构工具这个例子 ReSharper的的CodeRush +重构亲与最新版本,并都未能通过测试,并允许重构为:

 类节目
{
私有静态诠释_x;

静态无效的主要()
{
的for(int i = 0;我小于10; ++ I)
{
Console.WriteLine (测试());
}
Console.ReadKey();
}

私有静态诠释测试()
{
返回++ _ X;
}
}


解决方案

< STRONG>为了与自动化重构真正的安全是很辛苦



当我们在Visual C#中首次引入的重构,我们问自己这个问题:尽我们的重构需要完全把事情做好的时候,还是应该让他们为在某些情况下的错误呢?



是正确的所有的时间将需要大量的程序员的努力,这意味着我们不仅会在框中得到一些重构。这也将使重构慢,因为他们会花的验证了很多时间。



允许他们犯错误会使他们没用对于没任何球队吨有很大的自动化测试覆盖率。 TDD团队有很好的测试,但只有一个Visual Studio用户群的一部分。我们不想做,我们必须告诉人们不要使用的功能!



TDD的团队会很快发现错误,但他们学习一样快不信任我们的重构工具。他们会毫不犹豫地使用他们,并寻求其他解决方案的大部分时间(查找和替换,而不是重命名的)。



此外,由于C#的团队,我们是在一个很好的位置,使高保真重构。我们有一个独特的优势,用C#语言的设计者和编译器团队只是在大厅里。我们知道,我们应该发挥我们的优势。



所以,我们决定,而不是大量的说是不可靠的重构,使更少的高品质的重构。今天,有6





回想起来,我希望我们只是做重命名,提取方法,并引入局部变量。这些最后两个几乎相同,实现明智的。 3参数重构(有曾经是一个第七,促进局部变量参数,但它是在VS2010切)是一个大量的工作得到正确,而且很可能是不值得的。



我的建议是为做TDD ,给你一个测试的大集合,因此您可以放心地重构,无论您使用的工具或手工做的。


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;
    }
}

解决方案

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.

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.

这篇关于是否有.NET(或至少是C#)的任何安全重构工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆