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?
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!
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 :
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.