将“有效地处理遗留代码” 帮助某人使用从 VB6 移植到 VB.NET 的应用程序?

发布于 2024-07-25 09:14:02 字数 624 浏览 8 评论 0原文

我想重构一个最初用 Visual Basic 6.0 编写并随后移植的大型遗留应用程序到.NET。 为了充满信心地做到这一点,我想对现有代码进行单元测试,以便可以比较之前和之后的情况。 做到这一点最简单、最有效的方法是什么?

有一本名为《有效使用旧代码”看起来这可能对我有帮助。 然而,看起来它只处理面向对象的语言,而 Visual Basic 6.0 不一定是 OO。 这本书还能帮助我吗? 我希望读过它的人可以作证。

具体来说,该应用程序除了表单本身之外不使用任何类。 它直接从表单访问数据库,并且不一致。 有几个人从事这个项目,都使用自己的风格,没有任何标准。

正如我所说,这个项目已被移植到VB.NET。 但是,它仅在 Visual Studio 2008 下编译的意义上进行移植。 所有编码概念都是 Visual Basic 6.0。

I would like to refactor a large legacy application originally written in Visual Basic 6.0 and subsequently ported to .NET. In order to do this with confidence, I want to have unit tests around the existing code so I can compare before and after. What's the easiest and most effective way of doing this?

There's a book called "Working Effectively with Legacy Code" that looks like it might help me. However, it looks like it only deals with object-oriented languages and Visual Basic 6.0 is not necessarily OO. Can this book still help me? I'm hoping someone who's read it can vouch for it.

Specifically, this application uses no classes other than the forms themselves. It accesses the database directly from the forms, and not consistently. There were several people working on this project all using their own styles with no standards whatsoever.

As I said, this project has been ported to VB.NET. However, it's only ported in the sense that it compiles under Visual Studio 2008. All of the coding concepts are Visual Basic 6.0.

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

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

发布评论

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

评论(7

維他命╮ 2024-08-01 09:14:02

它不仅仅处理面向对象 (OO) 语言。 大部分内容都是关于如何处理 C 中的遗留代码。

所以是的,买它!


有整整一章(第19章)叫做:

我的项目不是面向对象的。 如何进行安全的更改?

还有 vbUnit,一个 xUnit 实现可以帮助您使用TDD< /a> 与 Visual Basic 6.0

实际上,我误读了这个问题,并认为您要移植,而不是您已经移植了。 在这种情况下,您有大量的“遗留”VB.NET 代码,这这本书完全适合你。 您可以利用 VB.NET 的 OO 功能并使用本书的其余部分。

我真的不能再推荐这本书了。

It does not just deal with object-oriented (OO) languages. Large sections are about how to deal with legacy code in C.

So yes, buy it!


There is a whole chapter (Chapter 19) called:

My project is not object oriented. How do I make safe changes?

Also there is vbUnit, an xUnit implementation that may help you use TDD with Visual Basic 6.0.

Actually, I misread the question and thought you were going to port, not that you already have ported. In this case, you have a ton of 'legacy' VB.NET code that this is totally the book for you. You can take advantage of VB.NET's OO capabilities and use the rest of the book.

I really cannot recommend this book more.

来世叙缘 2024-08-01 09:14:02

是的; 本书的概念伟大而有力,并且超越了 OOP。 VB6可以是面向对象的,尽管它不像某些语言那样完全面向对象。

本书中最强大的概念之一是“接缝”,本质上是您可以分解代码、注入测试或隔离或抽象功能的地方。 这只是甚至适用于过程代码的概念之一。

Yes; the book's concepts are great and powerful and extend beyond OOP. And VB6 can be object oriented, although it's not as thoroughly object-oriented a language as some.

Among the most powerful concepts in the book is that of "seams," essentially places where you can break your code up, to inject tests or to isolate or abstract bits of functionality. This is but one of the concepts that applies even to procedural code.

当爱已成负担 2024-08-01 09:14:02

您可能想让问题更具体。

虽然 VB6 本身并不是一种纯粹的 OO 语言,但有足够多的 OO 元素,大家应该很熟悉; 此外,还有许多用于 VB6 IDE 的单元测试插件。

但让我们考虑一些高级 VB6 组件:

  • 表单是对象(您可以创建新实例)。
  • 模块的行为类似于仅具有静态方法的类。
  • 课程& 用户控件与您所获得的对象非常接近。 缺少构造函数使事情变得很困难,但希望原始开发人员继续使用 Initialize 或编写一致的 Init 子程序。
  • VB 中的事件很奇怪; 这也可能是最让你绊倒的地方。 依赖于特定事件顺序的隐藏的、有状态的代码无疑分散在各处。
  • 属性页。 嗯,就是这样。

从 VB 的最佳实践开始。 如果应用程序在编写时没有考虑到最佳实践代码,我认为采取这一步将为您省去很多麻烦。

You may want to make the questions more specific.

While VB6 isn't a pure OO language per se, there are enough elements of OO that things ought to be familiar; also, there are a number of unit testing plug-ins for the VB6 IDE.

But let's consider some of the high-level VB6 components:

  • Forms ARE objects (you can create new instances).
  • Modules behave like classes with only static methods.
  • Classes & UserControls are as close to objects you'll get. The lack of constructor makes things rough, but hopefully the original developer stayed with Initialize or wrote a consistent Init sub.
  • Events in VB are odd; it's also what'll probably trip you up the most. Hidden, stateful code that depends on a particular ordering of events are no doubt scattered all over the place.
  • Property pages. Well, it is what it is.

Start with VB's best-practices. If the app wasn't written with best-practices code in mind, I think taking that step will save you a lot of trouble down the road.

青衫儰鉨ミ守葔 2024-08-01 09:14:02

我认为您会发现表征测试最有好处。 它们应该是自动化的。 如果没有特征测试,您将只能通过实际运行来手动测试您的应用程序/代码。 添加新代码时很容易错过关键功能的测试。 这是我自己的经历。

添加新代码时,Sprout 方法和 Sprout 类很重要。

I think that you will find characterization tests the most benefit. They should be automated. Without characterization tests you will then be left with testing your application/code manually by actually running it. It is so easy to miss testing critical functionality when adding new code. This comes from my own experience.

Sprout Method and Sprout Class are important when adding new code.

世态炎凉 2024-08-01 09:14:02

我建议看一下 Martin Fowler 的重构:改进现有代码的设计,这是一个优秀的必读。

您可能正在寻找类似于Visual Basic 中的专业重构的内容。 我还没读过,但看起来很适用。

I would suggest taking a look at Martin Fowler's Refactoring: Improving the design of existing code, which is an excellent must-read.

You might be looking for something along the lines of Professional Refactoring in Visual Basic. I haven't read it, but it looks applicable.

何时共饮酒 2024-08-01 09:14:02

我拥有一份副本,购买它是为了尝试控制我们的 C/C++ 项目。 因为我错过了 C#/.NET 提供的功能。

这些书非常 C/C++ 风格,但是,正如 John 所说,有一章是关于在没有对象的情况下工作的。

但是,如果如您所说,您的代码已移植到 .NET,那么它就不再是 Visual Basic 6.0 代码。 .NET 有许多 Visual Basic/C# 方法可让您连接到代码并进行测试。 尽管如此,本书还是会给你一个很好的概述,让你了解连接到应用程序的不同方法,以及控制大型旧项目的策略。

I own a copy, that I purchased to try get our C/C++ project under control. As I missed the functionality C#/.NET has to offer.

The books is very C/C++ ish, but, as John say, there is the chapter of working without objects.

But, if as you say, your code is ported to .NET it is not longer Visual Basic 6.0 code. .NET has lots of Visual Basic/C# ways of letting you hook into your code and test. But that said, the book will give you a good overview of the different ways you can hook into an application, and strategies for getting a large old project under control.

默嘫て 2024-08-01 09:14:02

您肯定有一些工作需要完成,但在进行任何 .NET 工作之前需要考虑以下策略。

将尽可能多的底层 FORM 代码移出到类中:每个表单一个类文件作为开始。 本质上,表单事件处理程序除了代理调用底层类实例中的方法之外什么都不应该做; 所有自定义方法显然都可以转移到类文件中。

在阅读了 Microsoft 和其他人提供的帮助您准备 .NET 迁移的最佳实践指南(等)后,您基本上已经做好了必须重新编码/重构的小麻烦:错误处理等烦恼,事件排序、后期绑定对象/变体、集合等。

关于错误处理代码的一句话:复制意大利面条式错误是特别困难的,特别是因为普通的 Visual Basic 编码人员不擅长处理错误。将其用作控制流逻辑。 如果例程中有两个以上的子例程,那么将每个 On Error 块分解为单独的子例程几乎是值得的。

清理代码后,您还可以考虑将 Visual Basic 代码重构为合理的抽象(例如,单个数据适配器类和连接等),但您将是对此的最佳判断者。

您可能不喜欢这种方法,因此一定要使用一种形式来试水。

You definitely have some work cut out for you, but here's a strategy to consider before doing ANY .NET work.

Move out as much of the underlying FORM code as you can into a class: One class file per form as a start. In essence, the form event handlers should do nothing but proxy calls to methods in an underlying class instance; all of the custom methods can obviously be shifted over to the class file.

After reading the best practices guide (etc.) by Microsoft and others to help you to prepare for .NET migration, you're basically set for the little bits of hell you'll have to recode/refactor: Annoyances such as error handling, event ordering, late bound objects/variants, collections, etc.

A word on error handled code: It's is especially hard to duplicate spaghetti on-errors, particularly since the average Visual Basic coder had a bad knack for using it as a flow-of-control logic. It's almost worth breaking apart each On Error block into separate sub-routines, if you have more than a two in a routine.

Once the code has been cleaned-up, you may also consider refactoring the Visual Basic code into sensible abstractions (for example, a single data adapter class & connection, etc.), but you'll be the best judge of that.

You may not like this approach, so definitely test the waters with one form.

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