更改项目中的变量名的最安全方法

时间:2022-03-28 05:36:50

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.

我在考虑使用regexp (linux上的sed/awk ?)工具来替换变量名,但是有很多次我的特定变量也作为字符串的一部分包含进来。

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.

还有一个关于在c++命名空间中更改内容的问题,因为在我的项目中实际上有两个类共享相同的名称,但它们位于不同的命名空间中。

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

我记得visual studio能够做到这一点,但是在linux上最安全、最优雅的方法是什么呢?

6 个解决方案

#1


5  

Safest (non automated way) way:

最安全(非自动)方式:

  1. Make sure all your unit tests work.
  2. 确保所有单元测试工作。
  3. Save everything into source control.
  4. 将所有内容保存到源代码控制中。
  5. Globally replace var with XXXvarXXX (seriously)
    a. Or maybe just the files you think need editing.
  6. 全局地用XXXvarXXX(认真地)替换var,或者仅仅是您认为需要编辑的文件。
  7. Try and compile. Everything that does not compile is easy to undo just remove the XXX.
  8. 试着编译。不编译的所有东西都很容易撤销,只需删除XXX。
  9. When it compiles run the unit tests.
  10. 编译时运行单元测试。
  11. When the unit tests work. Do a global replace of XXXvarXXX to the new name.
  12. 当单元测试工作时。将XXXvarXXX全局替换为新名称。
  13. Make sure the unit tests still work
  14. 确保单元测试仍然有效。
  15. Save everything in source control.
  16. 保存源代码控制中的所有内容。

Tongue only half in cheek. :-)

半舌半颊。:-)

#2


6  

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.

它叫做重构,但是我不记得在c++中是否有很好的方法来实现它——我想也许Eclipse c++有;也许值得一看。

#3


1  

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

我记得visual stuio能够做到这一点,但是在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.

使用可用于Linux的重构工具,您可以在Eclipse的visual studio中做与以前差不多的事情。

#4


1  

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

$400 f'n拷贝,但是这里有:http://www.xref.sk/xrefactory/download.html

I've of course never used it.

我当然从来没用过。

#5


0  

I know C# and visual studio has a great variable changer, but does it work through all the documents im not sure. I know when you change a variable in C# it pops up in the beside the Variable text. Change All

我知道c#和visual studio有一个很棒的变量改变器,但是它能在我不确定的所有文档中工作吗?我知道当你在c#中改变一个变量时它会在变量文本的旁边弹出。改变所有

However you may be stuck with the only option. Change variables and see where the compiler goes wrong. However its not ideal, but may be your only choice

然而,你可能会被唯一的选择所困。改变变量,看看编译器哪里出错了。然而它并不理想,但可能是你唯一的选择

#6


0  

use private variables, rename the variable and the obvious uses, correct the compiler errors.

使用私有变量,重命名变量和明显的用途,纠正编译错误。

#1


5  

Safest (non automated way) way:

最安全(非自动)方式:

  1. Make sure all your unit tests work.
  2. 确保所有单元测试工作。
  3. Save everything into source control.
  4. 将所有内容保存到源代码控制中。
  5. Globally replace var with XXXvarXXX (seriously)
    a. Or maybe just the files you think need editing.
  6. 全局地用XXXvarXXX(认真地)替换var,或者仅仅是您认为需要编辑的文件。
  7. Try and compile. Everything that does not compile is easy to undo just remove the XXX.
  8. 试着编译。不编译的所有东西都很容易撤销,只需删除XXX。
  9. When it compiles run the unit tests.
  10. 编译时运行单元测试。
  11. When the unit tests work. Do a global replace of XXXvarXXX to the new name.
  12. 当单元测试工作时。将XXXvarXXX全局替换为新名称。
  13. Make sure the unit tests still work
  14. 确保单元测试仍然有效。
  15. Save everything in source control.
  16. 保存源代码控制中的所有内容。

Tongue only half in cheek. :-)

半舌半颊。:-)

#2


6  

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.

它叫做重构,但是我不记得在c++中是否有很好的方法来实现它——我想也许Eclipse c++有;也许值得一看。

#3


1  

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

我记得visual stuio能够做到这一点,但是在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.

使用可用于Linux的重构工具,您可以在Eclipse的visual studio中做与以前差不多的事情。

#4


1  

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

$400 f'n拷贝,但是这里有:http://www.xref.sk/xrefactory/download.html

I've of course never used it.

我当然从来没用过。

#5


0  

I know C# and visual studio has a great variable changer, but does it work through all the documents im not sure. I know when you change a variable in C# it pops up in the beside the Variable text. Change All

我知道c#和visual studio有一个很棒的变量改变器,但是它能在我不确定的所有文档中工作吗?我知道当你在c#中改变一个变量时它会在变量文本的旁边弹出。改变所有

However you may be stuck with the only option. Change variables and see where the compiler goes wrong. However its not ideal, but may be your only choice

然而,你可能会被唯一的选择所困。改变变量,看看编译器哪里出错了。然而它并不理想,但可能是你唯一的选择

#6


0  

use private variables, rename the variable and the obvious uses, correct the compiler errors.

使用私有变量,重命名变量和明显的用途,纠正编译错误。