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 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:
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.
Awsome! You people coding stuff like this are awsome! When I grow up, I wanna be like you guys! I’ll check this out!
Regards!
Thanks @Emiliano! Glad you approve 🙂
Please do let us know how you get on when you try it out.
Mark
Awesome, Mark!! I hope to use this feature here in my job.
That’s great to hear Victor! Would love to hear how you get on when you try it!
Mark
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...
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?Read more
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 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!
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...
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!
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.
Hi Ramesh
Thanks for your interest in the IntelliCode refactorings feature. It was introduced in Visual Studio 16.3 Preview 3 so you’ll need to use that version or higher to see it; it’s not in version 16.2.5.
You can get the new preview version at https://visualstudio.microsoft.com/vs/preview/ right now, and it installs side-by-side with any existing versions you may have.
Regards
Mark
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!
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...
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...
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...
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.
Thanks David. I agree a duplicate finder/refactorer is an interesting opportunity. I hope folks will use this feature to refactor in good ways too, not just maintain copy/paste code 🙂
Mark
What about Visual Basic .NET?
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...
Cool!! Any luck for VS code to have this feature!!?
Glad you like it Madhav hs. We’re working to perfect this feature in Visual Studio at present and would love to hear more feedback. If you had support in VS Code, which particular language(s) would you be looking for? Feel free to raise a feature request over at https://aka.ms/vsicissues with more details.
If you’d like to reproduce the video I shared in this post to try the new feature, I’ve posted the very simple sample I used at https://github.com/markw-t/refactoringsdemo for you to clone.
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.
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…