Are Refactorings safe?

The C# refactorings we’re building in Whidbey are designed to be reliable. 

Not all of our customers use TDD, but many will use the refactoring tools we build.  If the tools don’t work reliably, users will stop using them.

The big exception was unbuildable code.  We can’t really guarantee preserved semantics of illegal code, since the semantics are undefined!

If you’re doing TDD, this exception matters less, as you keep your code buildable pretty much all the time.  It’s a prerequisite to running the tests, right?

There are some other subtle exceptions:

  1. Reordering the parameters to a method can have an unforeseen effect if the parameters you pass have side effects.  The side effects now happen in a different order.

  2. Removing a parameter to a method will change behavior if you pass a parameter with a side effect.

Both of these are difficult to guard against.  What should we do?  Remove the refactorings because they’re unsafe?  Add cumbersome warning messages on the UI?

However, we’re definitely working hard to make Rename and ExtractMethod very reliable.  They’re the most important Refactorings we can provide, and we want you to use them a lot.

Comments (15)

  1. Darrell says:

    I would prefer warning messages to removing the functionality, especially with a "do not show this message again" option. 🙂 Why limit advanced users in catering to the less-advanced users?

  2. Olle de Zwart says:

    I agree 100% with Darrell

  3. +1 for Darrell.

    You should do the same when a user changes from code view to design view and tries to change something in the Windows Forms designer.

    If you try and do that in VS.NET 2002/3, either some or almost all of your work is lost and is unrecoverable.

    That just sucks.

  4. You mean when you edit in the area that says "Do not edit"? *wink*

  5. Steve Hall says:

    What about inserting comments that are easy to find, like the // TODO: comments generated by C++ wizards? Something like: // TODO: Inspect the following refactored statement for this potential problem: xxx xxx.

    And when these potential problems could occur, I would even want the original statement left as a comment after the above // TODO, to serve as a reference to the old code.

    Also, adding project setting options to turn on/off prompts and another one to turn on/off comments would be good to have to control this behavior.

    Also, there should probably be at least one project setting checkbox to turn on/off all unsafe refactorings. Ideally, you could have separate checkboxes for each type of refactoring and warn the user when exiting out of the project settings property page that they’ve chosen some unsafe refactoring methods.

    If you choose to implement only prompts, then maybe you could have buttons like: "Skip", "Proceed – No Comment", and "Proceed – Comment" to allow for warning comments to be left behind.

    In any case, please don’t remove unsafe refactorings, as that dilutes the entire idea of refactoring (taking some risk to improve the code). Instead, offer some assistance if it can’t be automatic (safe).

  6. Ferris Beuller says:

    WInform designer isnt reliable, resx files are a joke that cant refactor too well . I stopped supporting .resx and winform designers.

  7. Ferris Beuller says:

    Yeah and ANY code thats in a project, I will edit if I damn well see fit. If your tools cant handle it I stop using it.

  8. Frans Bouma says:

    I’m perhaps missing something but what’s TDD?

    I agree with Darrell btw.

  9. senkwe says:

    TDD == Test Driven Development. If that was a serious question, it warms my heart that you can still create excellent software (LLBLGenPro) without necessarily requiring TDD (Of course you might have done it that way, just under a diff name)

  10. Jay- Which "Do not edit" area are you talking about? I have seen nothing like that…

  11. Naginata says:

    Wow, I’m glad you posted your link Frans, if I ever clear all the red flags off my desk I’m going to have to take a look at the LLBLGenPro demo 🙂

    My feeling on refactorings is… I hate getting warnings and notifactions popping up at me, because 90% of the time, I already know that what I did was potentially dumb, and I want to do it anyway, and the 10% that I SHOULD read the warning, I never do because I’m trained to just click ok/cancel/ignore/whatever.

    I think that leaving comments around can be one way to do it, especially on unbuilt/dirty (as in edited) code. I also think that the new "color coding" features going in to whidbey could be used… maybe have a special color for code that’s been changed by the refactoring tool since the last build or something?

  12. Robert Watkins says:


    One option to consider with the refactoring is a preview mode, where the user can inspect the (possibly dangerous) refactoring and decide for themselves.