如何重构 WinForms 应用程序?
我将在不久的将来修复 WinForms 应用程序 (.NET 2.0) 的错误。 查看源代码,我发现很大的代码文件(超过 2000 行),其中大多数是生成的对话框,其中包含大量代码隐藏。
有没有人给我一些建议可以分享? 有任何错误修复或重构 WinForms 应用程序的战争故事或最佳实践吗?
I'm going to bugfix a WinForms application (.NET 2.0) in the near future. Looking through the source code I find large code files (more than 2000 lines) most of them are generated dialogs with lots of code-behind.
Has anyone tips for me to share? Any war stories or best-practices for bug fixing or refactoring WinForms applications?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我首先编写一些单元测试。 如果代码相当密集,您将需要这些来保持理智。
它们会给你一些信心,让你能够在重构中相当积极地进行。
I'd start by writing some unit tests. You're going to need those to keep your sanity if the code is fairly dense.
They'll give you some confidence to be fairly aggressive in your refactoring.
顶部的几件事:
Couple of things from top head :
简短版本:
购买 Michael Feathers 的书《有效使用遗留代码》。
较长的版本:
维护(更改)此类应用程序时最大的挑战是在不破坏某些内容的情况下进行。 这意味着您需要了解应用程序的行为,而最简单的方法就是围绕它进行测试。 正如您所指出的,一开始这可能会令人畏惧,但这并非不可能。 它需要纪律和耐心。
您不必从单元测试开始。 通常更容易获得自动化框架(例如 NUnitForms 或 White)来首先将应用程序作为黑盒驱动。 围绕您需要更改的区域建立一套测试,以便您有足够的信心在不破坏某些内容的情况下进行更改。 然后开始针对单元可测试性进行重构。
如果它与我正在开发的应用程序类似,它主要涉及:
有时重写代码的各个部分比重构它们更容易,但这需要通过测试或引用规范(如果存在)来理解行为。
除了任何实用建议之外,如果您是从事此工作的团队的一员,请确保你们一起工作。 这将使工作变得更轻松、更愉快,并确保您今天所做的更改明天不会被不了解您所做工作的人撤销。
我可以就这个话题写一整天,但羽毛先生更擅长,而且我很饿。 祝你好运!
The short version:
Buy Michael Feathers' book, Working Effectively with Legacy Code.
The longer version:
The biggest challenge when maintaining (changing) such an application is doing it without breaking something. This means you need to understand the behaviour of the application, and the easiest way to do this is to get tests around it. As you've noted, this can be daunting at first, but it is not impossible. It requires discipline and patience.
You don't have to start with unit tests. It's usually easier to get an automation framework, such as NUnitForms or White, to drive the application as a black box first. Build up a suite of tests around the area you need to change to give you enough confidence to change it without breaking something. Then go in and start refactoring towards unit testability.
If it's anything like the application I'm working on, it mainly involves:
Sometimes it will be easier to rewrite sections of the code rather than refactor them, but that requires an understanding of the behaviour, either through testing or by referring to a spec (if one exists).
Apart from any practical advice, if you're part of a team working on this make sure you are working together. It will make the work easier, more enjoyable and ensure that changes you make today aren't undone tomorrow by someone who didn't understand what you were working on.
I could write all day on this topic, but Mr Feathers is much better at it, and I'm hungry. Good luck!
我认为这是基本的东西。 这里很多人一定有很多建议。 祝你好运!
I think this is the basic stuff. A lot of people here must have lots of suggestions. Good luck!