Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
}
like image 310
RoXX Avatar asked Sep 17 '10 14:09

RoXX


People also ask

What is refactoring code in C#?

A term coined by Martin Fowler, code refactoring allows you to change the code structure without changing or affecting what the code itself actually does. For example, changing a variable name or packaging a few lines of code into a method are code refactoring.

Is visual studio used for refactoring the code?

Visual Studio Code supports refactoring operations (refactorings) such as Extract Method and Extract Variable to improve your code base from within your editor.

What are the best practices for refactoring?

The best time for refactoring is before adding new features or updates to existing code. Doing so can help improve the product's quality. By cleaning the code base before adding new features or updates, it helps to make the product more robust and easier to use in the future.


2 Answers

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.

like image 120
Steve Townsend Avatar answered Sep 30 '22 18:09

Steve Townsend


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.

Visual Studio Refactorings

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.

like image 24
Jay Bazuzi Avatar answered Sep 30 '22 17:09

Jay Bazuzi