在整个代码库中更改变量名称是否只会带来灾难?
假设您决定将 Stack Overflow 的名称更改为 Frack Overflow。
现在,在您的代码中,您已经拥有数十个对象、变量和选择器,其名称“Stack”有一些变体。 您现在希望将它们替换为“Frack”。
所以我的问题是,您会选择通过正则表达式过滤器运行整个代码库并更改所有这些名称吗? 或者你会让他们这样吗?
Let’s say that you decide to change the name of Stack Overflow to Frack Overflow.
Now, in your code you already have dozens of objects and variables and selectors with some variation of the name "Stack". You want them to now be replaced with "Frack".
So my question is, would you opt to run your entire codebase through a regular expression filter and change all of these names? Or would you let them be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我会使用优秀 IDE 的“重命名”功能来为我完成此操作。
I would use the "rename" feature of a good IDE to do it for me.
这取决于,真的。
在像 C++ 这样的语言中,您可以摆脱这种情况,因为编译器会立即让您知道是否会出现问题。 然而,其他不那么挑剔的语言将允许您引用不存在的变量,最糟糕的情况就是以空引用抛出异常的形式出现。
我曾经参与过一个 Flex 项目,代码库非常混乱,我们决定仔细检查代码并对其进行一些美化,以满足 Adobe AS3 编码标准。 由于我是该项目的新手,因此我没有意识到某些类中的变量名称实际上引用了 hibernate(为后端服务器运行 java webapp)用于创建映射的持久对象。 因此,重命名这些变量会导致整个 Flex 前端行为异常,即使我们使用 IDE 中的“正确”重构工具进行重命名也是如此。
但实际上,我想说的是,在门口检查一下你的强迫症,并一次做出一点改变。 每当您更改大型项目中的数十个文件时,就有可能破坏项目的稳定性,在这种情况下,从这种风险中获得的好处并没有得到回报。
It depends, really.
In a language like C++, you can get away with this because the compiler will let you know right away if something would break. However, other less-picky languages will allow you to refer to variables which don't exist, and the worst that happens is a slap on the wrist in the form of an exception being thrown for a null reference.
I was working on a flex project once where the codebase was a real mess, and we decided to go through the code and beautify it a bit to meet the Adobe AS3 coding standards. Since I was new to the project, I didn't realize that the variable names in some classes actually referred to persistent objects which hibernate (running the java webapp for the backend server) was using to create mappings. So renaming these variables caused the entire flex frontend to misbehave, even when we did it with the "correct" refactoring tools in our IDE.
But really, I'd say to check your OCD at the door and make your changes a little at a time. Any time you change dozens of files in a large project, you risk destabilizing it, and in this case, the benefit derived from such a risk doesn't pay off.
我首先会问自己一个问题:为什么? 这是最终的风险/回报判断,只有您才能做出。
出于风格原因,我非常不愿意这样做,但对于类重构来说,这可能是合法的。
I'd first ask myself the question why? It is a risk/reward judgement at the end of the day which only you can make.
I would be very reluctant to do it for stylistic reasons, but for class re-factoring it may be legitimate.
好吧,不一定是一场灾难,但它肯定会在大型代码库上造成一些麻烦。 这就是为什么我讨厌匈牙利表示法:如果您碰巧更改了变量的类型,它会让您更改所有变量名称。
Well, not necessarily a disaster, but it certainly can cause some trouble on large code bases. That's why I hate hungarian notation: it makes you change all of your variable names if you happen to change its type.
如果您的解决方案中存在引用特定客户实现的名称的对象、成员和字段,我会努力重构它们以使用更通用的名称,并且我会让 Resharper 进行重命名,而不是一些通用的文本搜索和替换工具。
If there are objects, members and fields in your solution with names that reference a certain customer implementation, I would work hard to re-factor these to use more generic names instead, and I would let Resharper do the re-naming, not some generic text-search-and-replace tool.
只需使用重构工具,如 JetBrains 的 Resharper 或 CodeRush 和 Refactor! 由 DevExpress 提供。 它们会自动更改整个代码库中变量的所有引用,并且可以执行更多操作。
我相信重构! 甚至包含在 Visual Studio 的 VB 版本中。 我使用 Resharper,并且拒绝在没有它的情况下进行开发。
Just use a refactoring tool like Resharper by JetBrains or CodeRush and Refactor! by DevExpress. They change all references of a variable in your entire codebase automatically and can do much more.
I believe Refactor! is even included in the VB version of Visual Studio. I use Resharper and I refuse to develop without it.
如果我使用源代码版本控制系统(如 svn、git、bazar、mercurial 等),我就不会害怕重构我的代码。
使用某种“查找替换全部”或重构某些 IDE、编译(如果不是动态语言)并运行测试(如果有)。
如果出现严重错误,您始终可以使用源代码控制系统恢复代码。
If I were using a source code version control system (like svn, git, bazar, mercurial etc) I would not be afraid to refactor my code.
Use some kind of "find replace all" or refactoring of some IDE, compile (if it is not a dynamic language) and run your tests (if any).
If something goes horribly wrong, you can always revert your code using the source control system.
重命名可能是最常见的重构。 我们强烈建议您随时重构代码,因为这使您可以灵活地在第一次编写应用程序时不必就名称、代码放置等做出永久性决定。 如果您不熟悉这个想法,我建议您从维基百科页面开始,然后深入Martin Fowler 的网站。
但是,如果您需要编写自己的正则表达式来重命名事物,那么恕我直言,您可以使用一些更好的工具。 不要浪费时间重新发明轮子,然后修复新轮子意外损坏的地方。 如果可以的话,请使用现有的工具(IDE 或其他工具)来完成这些肮脏的工作。
即使您有“几十个”东西需要重命名,我认为您最好手动一一找到它们,然后使用自动重命名来修复整个代码中的所有实例。
Renaming is perhaps the most common refactoring. It is rather encouraged to refactor your code as you go, as this gives you the flexibility of not having to make permanent decisions about names, code placement, etc. as you are first writing your application. If you are not familiar with the idea, I would suggest you start with the Wikipedia page and then dive into Martin Fowler's site.
However, if you need to write your own regex to rename things, then imho you could use some better tools. Don't waste your time reinventing the wheel -- and then fixing whatever your new wheel broke by accident. If you have the option, use an existing tool (IDE or whatever) to do the dirty work.
Even if you have "dozens" of things to rename, I think you're better off finding them one by one manually, and then using an automatic Rename to fix all instances throughout your code.
我认为你这样做需要充分的理由。 当我在大型代码库中进行的更改会产生大量潜在副作用时(这种情况时有发生),我通常会寻找一种方法来使编译器在我错过的地方失败。 而且,如果可能的话,我倾向于分阶段进行,以尽量减少中断。
不过,我不会只是为了重命名而重命名。
You need good justification for doing it, I think. When I make changes that have a large number of potential side effects across a large codebase, which happens from time to time, I usually look for a way to make the compiler fail on spots I've missed. And, if possible, I tend to do it in stages so as to minimize the break.
I wouldn't rename just for the sake of renaming, though.