是否有解决方案可以简化多个地方遗留代码的更新?

发布于 2024-09-04 10:49:21 字数 732 浏览 4 评论 0原文

我正在使用一些旧代码,这些代码最初是为处理两种不同类型的文件而设计的。我最近的任务是向此代码添加一种新类型的文件。我的大部分问题都是通过用一个新条目填写一个扩展的 XML 文件来解决的,该条目处理从列表命名到文件如何以复数小写形式写入的所有内容。但这最终是不够的,因为 24 个不同的代码文件中可能有 50 个不同的位置,我必须更新仅针对原始两种文件类型分支的硬编码 switch 语句。

不幸的是,这并没有一致性;有些方法一半通过 XML 文件进行操作,一半通过硬编码进行操作。有些文件看起来像是可以在 XML 文件中操作,但实际上并非如此,而有些我认为需要更新硬代码的文件则不需要它。因此,找到其中大部分问题的唯一方法是在只有部分系统可运行时对整个系统进行测试,找到需要修复的步骤(幸运的是,错误日志记录实际上告诉我发生了什么事),然后再次运行整个过程。这浪费了时间来测试已经确认可以工作的代码部分,最好把时间花在测试我必须在其之上添加的新部分。

这很麻烦,而且幸运的是,我可以预见在不久的将来我将不得不添加另一种新类型的文件。

有没有任何解决方案可以帮助这种努力?我可以输入当前功能的一些参数,记录整个代码项目中实际需要更新的点,并在下次需要向代码添加新功能时运行一些不错的东西。它甚至不需要完全自动化,这可以帮助我直接导航到所有内容中的特定点,甚至可能记录需要加载的参数类型。

毫无疑问这很重要,但代码由 ASP.NET 页面、一些 ASP.NET 控件、数百个 C# 代码文件和一些附加 XML 文件组成。目前这一切都在几个大型 Visual Studio 2008 项目中。

I'm working in some old code which was originally designed for handling two different kinds of files. I was recently tasked with adding a new kind of file to this code. Most of my problems were solved by filling out an extensive XML file with a new entry that handled everything from what lists were named to how the file is written in plural lower case. But this ended up being insufficient, as there were maybe 50 different places in 24 different code files where I had to update hardcoded switch-statements that only branched for the original two file types.

Unfortunately there is no consistency in this; there are methods which operate half from the XML file, and half off of hardcode. Some of the files which look like they would operate off of the XML file don't, and some that I would expect that I'd need to update the hardcode don't need it. So the only way to find the majority of these is to run through testing the whole system when only part of it is operational, finding that one step to fix (when I'm lucky that error logging actually tells me what is going on), and then running the whole thing again. This wastes time testing the parts of the code which are already confirmed to work, time better spent testing the new parts I have to add on top of it all.

It's a hassle and a half, and to my luck I can expect that I will have to add yet another new kind of file in the near future.

Are there any solutions out there which can aid in this kind of endeavour? Something which I can input some parameters of current features, document what points in a whole code project actually need to be updated, and run something nice the next time I need to add a new feature to the code. It needn't even be fully automated, something that'll help me navigate straight to the specific points in everything and maybe even record what kind of parameters need to be loaded.

Doubt it matters specifically, but the code is comprised of ASP.NET pages, some ASP.NET controls, hundreds of C# code files, and a handful of additional XML files. It's all currently in a couple big Visual Studio 2008 projects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

游魂 2024-09-11 10:49:21

不完全是你所描述的,但是如果你可以在代码中引入一个接缝并放下一些你可以分解和模拟的接口,一套单元/集成测试将在很大程度上帮助你修改你可能不会的旧代码充分理解好。

Not exactly what you are describing, but if you can introduce a seam into the code and lay down some interfaces you can break out and mock, a suite of unit/integration tests would go a long way to helping you modify old code you may not fully understand well.

清晰传感 2024-09-11 10:49:21

我完全同意关于使用 Michael Feathers 的书来学习如何将新测试融入遗留代码中的评论。我还强烈推荐 Martin Fowler 的《重构》。听起来您需要为代码做的是实现“用多态性替换条件”重构。

我想你今天的代码看起来有点像这样:

if (filetype == 23)
{
  type23parser.parse(file);
}
else if (filetype == 69)
{
  filestore = type69reader.read(file);
  File newfile = convertFSto23(filestore);
  type23parser.parse(newfile);
}

你想要做的是将所有“if (type == foo)”类型的逻辑抽象为在工厂中创建的策略模式。

class FileRules : pReader(NULL), pParser(NULL)
{
private:
  FileReaderRules *pReader;
  FileParserRules *pParser;
public:
  void read(File* inFile) {pReader->read(inFile);};
  void parse(File* inFile) {pParser->parse(inFile);};
};

class FileRulesFactory
{
  FileRules* GetRules(int inputFiletype, int parserType)
  {
    switch (inputFiletype)
    {
    case 23: 
      pReader = new ASCIIReader;
      break;
    case 69:
      pReader = new EBCDICReader;
      break;
    }
    switch (parserType)
    ... etc...

那么你的主代码行看起来像这样:

  FileRules* rules = FileRulesFactory.GetRules(filetype, parsertype);
  rules.read(file);
  rules.parse(file);

完成这个重构,并添加一组新的文件类型、解析器、读取器等,变得就像编写一个专属于你的新类型的文件类型一样简单。

当然,还是去看书吧。我在这里过于简单化了,可能会出错,但你应该从中了解如何处理它。我还可以推荐另一本书“Head First Design Patterns”,其中有关于工厂模式的精彩部分(如果您喜欢那些“Head First”类型的书籍。)

I completely agree with the comment about using Michael Feathers' book to learn how to wedge new tests into legacy code. I'd also strongly recommend Refactoring, by Martin Fowler. What it sounds like you need to do for your code is to implement the "Replace conditionals with polymorphism" refactoring.

I imagine your code today looks somewhat like this:

if (filetype == 23)
{
  type23parser.parse(file);
}
else if (filetype == 69)
{
  filestore = type69reader.read(file);
  File newfile = convertFSto23(filestore);
  type23parser.parse(newfile);
}

What you want to do is to abstract away all the "if (type == foo)" kinds of logic into strategy patterns that are created in a factory.

class FileRules : pReader(NULL), pParser(NULL)
{
private:
  FileReaderRules *pReader;
  FileParserRules *pParser;
public:
  void read(File* inFile) {pReader->read(inFile);};
  void parse(File* inFile) {pParser->parse(inFile);};
};

class FileRulesFactory
{
  FileRules* GetRules(int inputFiletype, int parserType)
  {
    switch (inputFiletype)
    {
    case 23: 
      pReader = new ASCIIReader;
      break;
    case 69:
      pReader = new EBCDICReader;
      break;
    }
    switch (parserType)
    ... etc...

then your main line of code looks like this:

  FileRules* rules = FileRulesFactory.GetRules(filetype, parsertype);
  rules.read(file);
  rules.parse(file);

Pull off this refactoring, and adding a new set of file types, parsers, readers, etc., becomes as simple as writing one exclusive to your new type.

Of course, go read the book. I vastly oversimplified it here, and probably got stuff wrong, but you should get the general idea of how to approach it from this. I can also recommend another book, "Head First Design Patterns", which has a great section on the Factory patterns (if you like those "Head First" kinds of books.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文