Why is Visual C++ lacking refactor functionality?

C++Visual StudioVisual Studio-2008Visual C++

C++ Problem Overview


When programming in C++ in Visual Studio 2008, why is there no functionality like that seen in the refactor menu when using C#?

I use Rename constantly and you really miss it when it's not there. I'm sure you can get plugins that offer this, but why isn't it integrated in to the IDE when using C++? Is this due to some gotcha in the way that C++ must be parsed?

C++ Solutions


Solution 1 - C++

The syntax and semantics of C++ make it incredibly difficult to correctly implement refactoring functionality. It's possible to implement something relatively simple to cover 90% of the cases, but in the remaining 10% of cases that simple solution will horribly break your code by changing things you never wanted to change.

Read http://yosefk.com/c++fqa/defective.html#defect-8 for a brief discussion of the difficulties that any refactoring code in C++ has to deal with.

Microsoft has evidently decided to punt on this particular feature for C++, leaving it up to third-party developers to do what they can.

Solution 2 - C++

I'm not sure why it is like this, but third-party tools exist that help. For example, right now I'm evaluating Visual Assist X (by Whole Tomato). We're also using Visual Studio 2005.

Solution 3 - C++

devexpress provides Add-in Refactor! for C++ for VS2005 and VS2008.

Solution 4 - C++

Don't feel hard-done-by, it isn't available in VB.Net either :)

C++ is a HARD language to parse compared with C# (VB too unless you've "Option Explicit" and "Option Strict" switched on, it's difficult to tell exactly what any line of code is doing out of a MUCH larger context).

At a guess it could have something to do with the "difficulty" of providing it.

P.S. I marked my answer as community wiki because I know it's not providing any useful information.

Solution 5 - C++

Eclipse does few c++ refactorings including 'rename'. Check out this question here on StackOverflow.

It is also possible to use Microsoft compiler with Eclipse. Check out here.

Try Eclipse and see if it fits for you.

Solution 6 - C++

There is a lot of fud and confusion around this issue. This amazing youtube video should clear up why C++ refactoring is hard: https://www.youtube.com/watch?v=mVbDzTM21BQ

tl;dr Google refactors their entire 100 million line C++ codebase by using a compiler (Clang + LLVM) that allows access to its intermediate format.

Bottom line, third parties are screwed here, there is no realistic way for them to refactor VS C++ unless MS outputs intermediate results the same way. If you think of it from the programming problem perspective this is obvious: in order to refactor VS C++ you have to be able to compile C++ the exact same way VS does with the same bugs, limitations, flaws, hacks, shortcuts, workarounds, etc. The usual suspects like Coderush and Resharper do not have the budget for that kind of insanity although apparently they are trying but it has been years...

http://www.jetbrains.com/resharper-cpp/

Update 2016: Resharper now does a decent job at C++ refactor. Limitations are purely for large / gigantic projects.

Solution 7 - C++

MS has finally done this: https://channel9.msdn.com/Shows/C9-GoingNative/GoingNative-33-C-Refactoring-in-Visual-Studio-2015#time=04m37s

They have started doing this about 10 years ago, I remember watching ms channel9 long ago.

Solution 8 - C++

I've been using Visual Assist X with visual studio for about one year and a half. It's an incredible tool that helps you a lot with ordinary C++ code, but it doesn't perform very well on templated code. For instance, you if have a sophisticated policy-based template design, it won't know how to rename your variables, and the project won't compile anymore.

Solution 9 - C++

Solution 10 - C++

I'd like to point out that Qt Creator (a C++ IDE which is compatible with VC++ libraries and build system) provides symbol renaming that works very well:

> You can rename symbols in all files in a project. When you rename a class, you can also change filenames that match the class name.

Qt Creator - Refactoring: Renaming Symbols

Qt Creator's rename functionality gives you a list of the symbol references it found and an opportunity to exclude any of them before performing the replace. So if it gets a symbol reference wrong, you can exclude it.

Qt Creator Refactor Replace

So C++ symbol renaming is possible. Coming to VS from Qt Creator I feel your pain, to the point where I've considered converting preexisting VS projects of considerable size to use Qt Creator instead.

I don't buy the argument that this is specifically hard in C++. In addition to the fact that it already works very well in Qt Creator, there's the fact that the compiler and linker can find and match symbols: If that wasn't possible you couldn't build your application.

In fact, languages like Python that are dynamically typed also have renaming tools. If you can create such a tool for a language where there are no explicit references to variable type you can definitely do it for C++.

Case in point:

> ... Rope, a python refactoring library... I tried it for a few renames, and that definitely worked as expected.

Stack Overflow - What refactoring tools do you use for Python?

Solution 11 - C++

Well in spite of comments by all you experts I totally disagree that refactoring support issue has something to do with C++ language semantics or any language semantics for that matter. Except the compiler builder themselves don't choose to implement one in first case due to their own reasons or constraints whatsoever they maybe.

And offense not to be taken but I am sorry to say Mr jsb the above link you provided to support your case (i.e of yosefk) about C++ defect is totally out of question. Its more like you providing direction to "Los angeles" when someone asked for of "San Franisco".

In my opinion raising refactoring difficulty issue for certain language is more like raising a finger on language integrity itself. Especially for languages which is sometimes just pain.... when it comes to their variable declaration and use. :) Okay! tell me how come you loose track of some node within a node tree ... eh? So what it is do with any language be it as simple as machine level code. You know you VS compiler can easily detect if some variable or routine is dead code. Got my point?

About developing third party tool. I think compiler vendors can implement it far more easily and effectively if they ever wanted to then a third party tool which will have to duplicate all the parsing database to handle it. Nowadays compiler can optimize code very efficiently at machine code level and I am hearing here that its difficult to tell how some variable is used previously. You haven't paid any real attention to inner working of compiler I suppose. What database it keep within.

And sure its the almost same database that IDE use for all such similar purposes. In previous time compiler were just a separate entity and IDE just a Text Editor with some specialization but as times goes by the gap between compiler and IDE Editor become less and its directly started working on similar parsed database. Which makes it possible to handle all those intellisense and refactoring or other syntax related issues more effectively. With all precompile things and JIT compiling this gap is almost negligent. So it almost make sense to use same database for both purpose or else your memory demand go higher due to duplication.

You all are programmers - I am not! And you guys seems to be having difficulty visualizing how refactoring can be implemented for C++ or any language that I can't comprehend. Its just all about for something you have to put more effort for some less depending on how heavy is a person you trying push.

Anyway way VS a nice IDE especially when it comes to C#.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionxyzView Question on Stackoverflow
Solution 1 - C++JSBձոգչView Answer on Stackoverflow
Solution 2 - C++BrianView Answer on Stackoverflow
Solution 3 - C++mem64kView Answer on Stackoverflow
Solution 4 - C++Binary WorrierView Answer on Stackoverflow
Solution 5 - C++fukanchikView Answer on Stackoverflow
Solution 6 - C++Dirk BesterView Answer on Stackoverflow
Solution 7 - C++Stepan YakovenkoView Answer on Stackoverflow
Solution 8 - C++Liviu GheorghisanView Answer on Stackoverflow
Solution 9 - C++support_msView Answer on Stackoverflow
Solution 10 - C++TerrabitsView Answer on Stackoverflow
Solution 11 - C++NickyBlueView Answer on Stackoverflow