怎么办一个11000行的C ++源文件? [英] What to do about a 11000 lines C++ source file?

查看:110
本文介绍了怎么办一个11000行的C ++源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我们有这么大的(是11000行巨大?)mainmodule.cpp源文件在我们的项目,每次我必须触摸它我cringe。



这个文件是如此中央和大,它不断积累越来越多的代码,我想不出一个好的方法,使其实际开始收缩。



文件在我们的产品的几个(> 10)维护版本中使用和主动更改,因此它真的很难重构。如果我简单地把它拆分,说一开始,3个文件,然后合并回来的更改维护版本将成为一场噩梦。而且如果你分割一个具有这么长而丰富的历史的文件,跟踪和检查 SCC 历史中的旧变化突然变得更难了。



该文件基本上包含我们程序的主类(主要内部工作分派和协调),所以每次添加一个功能时,它也会影响这个文件和每次它增长。 : - (



在这种情况下你会做什么?任何关于如何将新功能移动到单独的源文件,而不麻烦 SCC 工作流程

(注意工具:我们使用C ++与 Visual Studio 我们使用 AccuRev 作为 SCC ,但我认为 SCC Araxis Merge 做文件的实际比较和合并)

解决方案


  1. 在文件中找到相对稳定的代码(不变快,分支之间变化不大),可以作为独立单元将它移动到自己的文件中,并且在所有分支中都是自己的类,因为它是稳定的,这不会导致(许多)尴尬的合并,必须应用于不同的文件


  2. 在文件中查找一些基本上只适用于小数字的代码的分支,可以独立。不管它是否改变快,因为分支数量少。将此移动到它自己的类和文件。重复。


因此,我们已经摆脱了无处不在的代码,某些分支。



这给你一个管理不善的代码的核心 - 它需要无处不在,但它在每个分支都不同(和/或它不断变化,分支在其他人的后面运行),但它在一个单一的文件,你没有成功尝试在分支之间合并。别那样做。永久分割文件 ,也许通过在每个分支中重命名它。它不是主,它是主要的配置X。 OK,所以你失去了通过合并应用相同的更改到多个分支的能力,但在任何情况下,这是合并的代码的核心不工作得很好。如果你不得不手动管理合并来处理冲突,那么在每个分支上手动应用它们是没有损失的。



我认为你错了说SCC的种类没有关系,因为例如git的合并能力可能比你使用的合并工具更好。因此,对于不同的SCC,在不同的时间发生核心问题,合并是困难的。但是,您不太可能能够更改SCC,因此该问题可能无关紧要。


So we have this huge (is 11000 lines huge?) mainmodule.cpp source file in our project and every time I have to touch it I cringe.

As this file is so central and large, it keeps accumulating more and more code and I can't think of a good way to make it actually start to shrink.

The file is used and actively changed in several (> 10) maintenance versions of our product and so it is really hard to refactor it. If I were to "simply" split it up, say for a start, into 3 files, then merging back changes from maintenance versions will become a nightmare. And also if you split up a file with such a long and rich history, tracking and checking old changes in the SCC history suddenly becomes a lot harder.

The file basically contains the "main class" (main internal work dispatching and coordination) of our program, so every time a feature is added, it also affects this file and every time it grows. :-(

What would you do in this situation? Any ideas on how to move new features to a separate source file without messing up the SCC workflow?

(Note on the tools: We use C++ with Visual Studio; We use AccuRev as SCC but I think the type of SCC doesn't really matter here; We use Araxis Merge to do actual comparison and merging of files)

解决方案

  1. Find some code in the file which is relatively stable (not changing fast, and doesn't vary much between branches) and could stand as an independent unit. Move this into its own file, and for that matter into its own class, in all branches. Because it's stable, this won't cause (many) "awkward" merges that have to be applied to a different file from the one they were originally made on, when you merge the change from one branch to another. Repeat.

  2. Find some code in the file which basically only applies to a small number of branches, and could stand alone. Doesn't matter whether it's changing fast or not, because of the small number of branches. Move this into its own classes and files. Repeat.

So, we've got rid of the code that's the same everywhere, and the code that's specific to certain branches.

This leaves you with a nucleus of badly-managed code - it's needed everywhere, but it's different in every branch (and/or it changes constantly so that some branches are running behind others), and yet it's in a single file that you're unsuccessfully trying to merge between branches. Stop doing that. Branch the file permanently, perhaps by renaming it in each branch. It's not "main" any more, it's "main for configuration X". OK, so you lose the ability to apply the same change to multiple branches by merging, but this is in any case the core of code where merging doesn't work very well. If you're having to manually manage the merges anyway to deal with conflicts, then it's no loss to manually apply them independently on each branch.

I think you're wrong to say that the kind of SCC doesn't matter, because for example git's merging abilities are probably better than the merge tool you're using. So the core problem, "merging is difficult" occurs at different times for different SCCs. However, you're unlikely to be able to change SCCs, so the issue is probably irrelevant.

这篇关于怎么办一个11000行的C ++源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆