Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safest way to change variable names in a project

So I've been working on a relatively large project by myself, and I've come to realise that some of the variable names earlier on were.. less than ideal.

But how does one change variable names in a project easily? Is there such a tool that can go through a project directory, parse all the files, and then replace the variable names to the desired one? It has to be smart enough to understand the language I imagine.

I was thinking of using regexp (sed/awk on linux?) tools to just replace the variable name, but there were many times where my particular variable is also included as a part of strings.

There's also the issue about changing stuff on a c++ namespace, because there is actually two classes in my project that share the same name, but are in different namespaces.

I remember visual studio being able to do this, but what's the safest and most elegant way to do this on linux?

like image 311
kamziro Avatar asked Jan 12 '11 05:01

kamziro


People also ask

How do you change multiple variable names?

So in that case, try ctrl + F to open the Find/Replace window an then write your variable in the search field to find all the instances of that variable, and then in replace text field, enter the text or variable name you want to replace with current variable. and then click on Replace All button at the bottom.

Should you shorten variable names?

Don't shorten names unless you really need to. It really only makes sense if the variable name is already super long. For example, con is a popular abbreviation for a network connection, but it could also mean a drawback (pro vs con), or the end of an array.

Which is incorrect way of naming variable?

Variable name may not start with a digit or underscore, and may not end with an underscore. Double underscores are not permitted in variable name.


4 Answers

It's called refactoring, but I don't remember if there's a great way to do it in C++ -- I think maybe Eclipse C++ had it; might be worth taking a look.

like image 171
user541686 Avatar answered Oct 02 '22 12:10

user541686


Safest (non automated way) way:

  1. Make sure all your unit tests work.
  2. Save everything into source control.
  3. Globally replace var with XXXvarXXX (seriously)
    a. Or maybe just the files you think need editing.
  4. Try and compile. Everything that does not compile is easy to undo just remove the XXX.
  5. When it compiles run the unit tests.
  6. When the unit tests work. Do a global replace of XXXvarXXX to the new name.
  7. Make sure the unit tests still work
  8. Save everything in source control.

Tongue only half in cheek. :-)

like image 23
Martin York Avatar answered Oct 02 '22 11:10

Martin York


I remember visual stuio being able to do this, but what's the safest and most elegant way to do this on linux?

You can do pretty much what you used to do in visual studio in Eclipse using the re-factoring tools, which is available for Linux.

like image 24
hhafez Avatar answered Oct 02 '22 10:10

hhafez


$400 a f'n copy, but here you go: http://www.xref.sk/xrefactory/download.html

I've of course never used it.

like image 43
Edward Strange Avatar answered Oct 02 '22 11:10

Edward Strange