tags:

views:

185

answers:

4

I have inherited a large C# project where all the variable and parameter names are written_like_this.

Is there some easy way or tool I could use to rename them all automatically with no more than say 10 mouse clicks. So that the above example would become writtenLikeThis (i.e. camel case). And instance variables _writtenLikeThis.

It would also need to update the XMLDoc comments as well.

I could probably knock something up with a Regex, but only if I'm not rewriting a wheel.

Thanks.

+1  A: 

No, refactoring tool in Visual Studio does not support that kind of sophistication.

You would need to do it manually. You could write some utility to rewrite the project files, but then it will operate semantically-detached and may pick up something else other then variable names, which you would rather avoid.

How would you, for example, distinguish between:

string my_badly_named_string = "Hi, there!";

and

string aGoodString = @"We need to rename the my_badly_named_string because
                       it looks ugly";

VS refactoring tool knows the difference because it views semantic on top of source text. Your utility won't.

But honestly, it would be a titan's work. There is also a danger you would introduce more bugs. Better leave it as it is.

Developer Art
I dunno, it sounds pretty simple to handle with a few regex's to me. The example you gave can actually be done in one line using only the built-in .NET string class.
JoshJordan
Regexes are *always* easy right up until the point when you start using them for tasks better suited to parsing :-)
paxdiablo
+2  A: 

I agree with NewInTown that you should just leave it as it is, but, if you want to change the names, and you have good unit tests, then when you need to make some change to fix a bug, in a function, then slowly begin changing the names to the new scheme.

This will prevent you from trying to automate it and creating many new bugs, and you won't be wasting time making changes to working code that doesn't improve the application.

I tend to use time that I am fixing a function to do some refactoring or basic optimizations as I am in that function for some reason anyway.

James Black
A: 

If you've just inherited the project, it might be worth leaving it until you need to start working with it and updating it. If your refactoring excerise is purely renaming, the time spent might be better spent elsewhere. Renaming things as you work on various parts will also visually show where you've been.

If you really wanted to rename the variables and parameter names you would probably want to try and build a process that just parses the code files to do a text replacement (with regular expressions or basic string manipulation depending on what you know, bearing in mind it would be a once only change) This would probably be quicker than evaluating code refactoring tools.

Mark Redman
+1  A: 

I recently did this using resharper. It's a lot faster than the baked in utility in Visual Studio. I had to go to each identifier and do a refactor/rename but it's safe since it renames everything correctly. One important point is that the code must compile completely before you start. It helps to recompile once in a while just be be sure things are still in good shape. It does not look at the source but rather it uses the IL or the codedom or something. For a very large project this is still time consuming but it works.

BTW. I spent a whole day changing all of the local vars in an inherited project that started with an underscore so that they did not. I personally can not stand to look at code that is polluted with an overabundance of leading underscores. But that's just me - every shop is different.

Loki Stormbringer