Refactoring made easy with IntelliCode!

Mark Wilson-Thomas

Mark

Have you ever found yourself refactoring your code and making the same or similar changes in multiple locations? Maybe you thought about making a regular expression so you could search and replace, but the effort to do that was too great? Eventually you probably resigned yourself to the time-intensive, error prone task of going through the code manually.

What if your developer tools could track your edits and learn about the repeatable changes you were making? After only a couple of examples they would spot you doing something repetitive and offer to take the remaining actions for you? With Visual Studio 2019 version 16.3 Preview 3 we are happy to announce that refactorings can now be enhanced by IntelliCode. IntelliCode spots repetition quickly and suggests other places in your code where you might want to apply that same change, right in your IDE:

 

Try it now

Refactoring is a preview feature of IntelliCode, so when you get Visual Studio 2019 version 16.3 Preview 3  it will be off by default. Visit the Tools-Options page, IntelliCode General tab, Preview features area, and switch C# refactorings to “Enabled” to turn it on.

Once you change this setting, close any files you may have open, then restart Visual Studio:

How to turn on the refactorings feature in tools-options

How it works

Under the hood, IntelliCode looks at each of your edits as you type. It uses PROSE (PROgram Synthesis by Example) to synthesize generalized edit scripts that can take your code from the “before editing” state to the “after”. When IntelliCode discovers that it can apply one of these scripts elsewhere in your code (which can be based on as few as 2 examples in your code), we let you know via the Visual Studio lightbulb in the margin or when hovering the affected code, and through green “squiggles”. The lightbulb offers actions to apply the refactorings for you. The underlying technology is similar to the Excel’s Flash Fill feature and is described in this research paper. More details will be presented at the upcoming OOPSLA 2019 conference.

This isn’t just tracking text changes – IntelliCode is aware of the syntactic structure of your code. This syntactic awareness allows it to detect cases where the variable names in your refactoring examples differ but the essential structure of the change is the same:

Illustration showing how IntelliCode can detect patterns syntactically and offer suggestions

If you don’t like a suggested change you can select the ignore option on the lightbulb, and we won’t bother you about that detected pattern again unless you recreate it.

Let us know what you think!

We would love to hear about your experiences as you try this new feature. Good or bad, they will help us improve. Please raise issues and comments via Visual Studio “report a problem” .  We’re interested to hear feedback about the recommendations themselves, the performance of the feature, or any capabilities you might be missing. When sending your feedback it would be really useful if you can share details of what was detected and what sort of edits you were making; we’ll follow up.

Mark Wilson-Thomas
Mark Wilson-Thomas

Principal Program Manager, Visual Studio IntelliCode

Follow Mark   

29 comments

  • Avatar
    Andi Emma Davies

    Impressive! Why are these expressed as compiler warnings though? Seems a bit of a strange choice, given there’s already a mechanism for suggested refactors that doesn’t use warnings (I think, though I could be getting mixed-up), and these new refactor suggestion warnings will cause build failures when the “treat warnings as errors” flag is enabled.

    • Mark Wilson-Thomas
      Mark Wilson-Thomas

      Thanks a lot for your feedback Andi, and for trying out the new refactorings feature. We realize that mixing these suggested actions with the “warnings” level of feedback in the editor can be problematic. We did experiment internally with using the “suggestion/info” level – which you can see at 11:41 in my demo at https://aka.ms/vsic/blog/build2019live, the three little grey dots in the editor.  We found that nobody was discovering the suggested action at this level, and we’re experimenting with the higher visibility of the “warning” level in the current version. However we recognise this can cause confusion with real compiler warnings, so we are considering what the right level for these suggested actions should be.  We’re considering adding a new level for suggested actions that would have a stronger visual treatment than the “suggestion” level. I’d be very interested to talk with you further about this – drop me an email at mwthomas at microsoft dot com and we can continue the conversation?

      • Avatar
        Andi Emma Davies

        Hi Mark. My pleasure! That sounds like a good middle-ground, if it’s practical to implement, and doesn’t add too much complexity. Sure, I’ll email you now.

  • Avatar
    Frank Hileman

    It sounds nice in principle, as long as we can look at and understand the generated script. I always thought complex regular expressions (the only real alternative) were too hard to remember how to construct, given how infrequently one needs them.

    • Mark Wilson-Thomas
      Mark Wilson-Thomas

      Thanks for the suggestion Frank. We certainly want to be a lot easier than constructing a complex regular expression is! Can you give me more insight into why you might want to see the transform script/program yourself rather than just taking/not taking the suggestions raised – are you imagining using the script somewhere else? Would love to hear more about that…

    • Mark Wilson-Thomas
      Mark Wilson-Thomas

      Hi Ondřej .

      Thanks for the question.

      We’ll add more languages to the refactorings feature as we perfect it – it’s a preview right now and we have some work to do to refine it. However please go ahead and register your support for adding refactorings for VB.NET over at https://aka.ms/vsicissues where you’ll see our feature request backlog and can upvote ones you support. 

      I should say that (if you didn’t already know), we do have a preview of IntelliCode completions for VB.NET available in Visual Studio 2019 version 16.3 Preview 2 and higher – just go to the Tools-Options-IntelliCode page, look under Preview features and turn on “Visual Basic argument completion” for argument completion suggestions, and “Visual Basic base model” for methods and properties suggestions.

      Thanks

      Mark

  • Avatar
    Kim Homann

    On the one hand, this feature scares me. But that is probably just me. On the other hand, and this is even worse, this feature encourages keeping copy&paste code instead of cleaning it up. It even encourages producing more copy&paste code bc it becomes easier to maintain. Bad idea!

    • Avatar
      David Lowndes

      You’re not alone. I also feel that having a feature that makes it easier to maintain copy/paste code is perhaps not the best step.
      If it identified the code duplication and suggested pulling it into a function to eliminate the duplication, I think that would be preferable.

    • Mark Wilson-Thomas
      Mark Wilson-Thomas

      Hi Kim – thanks for the comment. 

      It’s true that you could use this tool to help you maintain your cut and paste coding habits and keep leaving code smells like repeated code in your codebase. However as the video shows, it can also be used to make it easier to remove that smelly code and replace/refactor it by abstracting into more maintainable functions.  That said, as David says below, there’s an interesting opportunity around finding duplicates and recommending refactorings too.

      Mark

      • Avatar
        Paul Evans

        When I saw the demo of it, I was seeing it as a way of eliminating the copy and paste code. Once the developer identified that they had copy and paste code and did the first few changes, they are getting warnings that there are other places to remove that code smell.I am not in a position to look at the new feature at the moment, having to use non-preview software at work.If I did one of those FToC changes, then adjusted something else, then did another FToC change (and possibly another something else and an FToC change) would intelliCode still pick up that I was doing those changes and recommend the other changes, or do all the refactorings have to be done at the same time? If it can have other bits happen inbetween, is there a limit to that?Having IntelliCode detect code smell and recommend creating functions to deal with it would be nice, as long as you can tell it “No – I really do want to do it this way, let me know about other places to do this but not here”

        • Mark Wilson-Thomas
          Mark Wilson-Thomas

          Hi Paul

          That’s exactly what our intention was: to make it easier to make repeated changes across your code to clean it up, refactor it etc. To answer your question about whether the repeated changes have to be done at exactly the same time – the answer is no.  We keep track a certain number of editing “sessions” with the algorithm (in order to keep memory consumption of the tracker reasonable), but we believe the threshold is set high enough for us to catch most cases where you are doing similar things that will be detected and recommend the other locations for change.  When you do get a chance to try this, if you find that we’re missing any places where you’d expect us to find the repetition but we don’t, please let us know.

          On your point of detecting repetition and needing to be able to suppress – this is definitely something we will need to think about carefully. Thanks for the feedback!

          Mark

  • Avatar
    Ramesh Kunwar

    Hi Mark, I cannot find this feature on Visual Studio Professional 2019 version 16.2.5? Is it removed or moved? Looks like removed as searching won’t return it.

  • Avatar
    Phillip Mortari

    I kind liked the idea as a first step to help identifying possible portions of code that requires attention/refactoring. There’s any plans or ideas to expand that to something like a “hey – you have these similar pieces on your code, what about refactoring?” thing? I guess that would be something awesome to have it handy!

    • Mark Wilson-Thomas
      Mark Wilson-Thomas

      Hi Philip

      Thanks for reading and for your comments. We hope what we’ve built so far will help you with the case where you’ve *already found* a couple of places which could use a certain refactoring and figured out what to do, but just need help finding the other target locations. The problem of finding repeated patterns in your code AND then identifying them as good or bad is an even tougher one, but we are certainly interested in it. Are you imagining something that would scan your code for patterns and let you know what they are to decide what to do next, or did you have something else in mind? I’d love to talk further – drop me a line on mwthomas at microsoft dot com?

      Thanks

      Mark

      • Avatar
        Phillip Mortari

        Hey Mark – Thanks on the quick response, and sure that helped! Actually this post was my trigger to update my preview.
        I’ll put a few ideas on the paper and definitely I’m sending it to you.

        Thank you!

  • Avatar
    Yann Duran

    Hey Mark! This sounds like it’d be great for detecting duplicate code, on the fly as it were. And also for repetitive refactoring of code that may be inherited. I’ve definitely wished for some better way of doing repetitive code refactoring tasks since macros were taken away (yes, I know there’s an extension, but it’s not the same as it used to be). The storage mechanism should have been C# (or maybe VB, or whatever language was being used), not Javascript. It’s been a while since I’ve looked at it, so hopefully I haven’t got that wrong.
    I’m wondering though, where’s the processing happening. Is it all local, or is there a cloud service out there somewhere that does all the heavy lifting?
    In particular you said “We keep track (of) a certain number of editing ‘sessions’“. My question would be WHERE would those sessions be stored? Locally, or in the cloud?
    I agree with @Andi Emma Davies that compiler warnings doesn’t seem to be a good fit. I do like the sound of your suggestion to add a new suggested refactoring type (no pun intended lol) and therefore be able to make the suggestions more discoverable. But on the other hand, the refactoring suggestion mechanism does already exist and is a perfect fit for the new feature that you guys have created.
    Further to what @Paul Evens was saying, maybe when the cursor is in a method, IntelliCode could scan the rest of the code tree (in the background of course) to see if there was any other similar or exact copies of the code?

    • Mark Wilson-Thomas
      Mark Wilson-Thomas

      Hi @Yann!

      Thanks for your feedback. I’m glad you can see some good use for this feature!

      To take your questions one by one:

      1. “where’s the processing happening. Is it all local, or is there a cloud service out there somewhere that does all the heavy lifting?
      In particular you said “We keep track (of) a certain number of editing ‘sessions’“. My question would be WHERE would those sessions be stored? Locally, or in the cloud?”

      All the processing for IntelliCode refactorings is entirely local. Sessions are stored in memory on the local machine, and culled when you reach the “session limit” – and an editing “session” is not the same as a Visual Studio session, it’s a much smaller atom of work. There’s no cloud processing of your code in this feature. We do send simple telemetry about whether the suggestions are accepted or rejected, and whether we learn any suggestions, but these are counts only, no code is sent to the cloud by the refactorings feature. You can turn even this telemetry collection off if required – see this documentation. Let me know if you have other questions about this. I’d be interested to hear how you would feel if a future feature did require more cloud processing of your code – drop me a line mwthomas at microsoft dot com if you would like to discuss.

      2. Warnings->Messages/Suggestions we definitely agree with this feedback and have turned down the severity to “Message” in the next preview release of Visual Studio 2019. We will help discovery by raising an alert that lets you know to look in the Messages in the Error List when you first generate a suggestion, but thereafter we will be subtle. We are hoping not to have to create a whole new category as we want to avoid cluttering the IDE with more concepts if we can, so we are leaning toward your comment of using the existing refactoring mechanism.

      3. Duplicate code scanning if you change a piece of code in a couple of places we will already help you find the duplicates, but I can see the usefulness of a dupe-detector without changes too.

      Thanks again for your feedback; please do let us know about any successes or failures you have with IntelliCode refactorings if you get chance to try them for yourself. As well as filing reports on “Report a Problem” in Visual Studio, you can send me mail on mwthomas at microsoft dot com or tweet me @MarkPavWT.

      Regards
      Mark

Leave a comment