Improving Legacy Code
My suggestions to a client on improving their legacy code.
Initially:
- Move all source code from VSS into TFS.
- Ensure all code needed to compile is within the workspace.
- Ensure all dependencies are correct so compilation of any one module will automatically compile needed dependencies for that module.
- Add UnitTest++ (or CppUnit – whatever floats your boat) to project.
- Add Log4Cxx to project – more man-hours have gone into writing this than your own system.
As you develop new code or extend older code:
- Move the logical projects into folders. Create another physical project alongside this project. Usually you will call this Test or Tests.
- For each module you are editing in add a c++ source file in the test project. Format for this will be Test.cpp.
- Write the test for the part you are changing, this should be as concise as possible. See Feathers “Working Effectively with Legacy Code
” and Kerivsky “Refactoring to Patterns
” books for tips.
And eventually:
- Use Msbuild for automated building of source. This will build all code, every time, and warn of errors and can even bundle things into installers, etc.
- Turn on treat warnings as errors and fix.
- Get a static analysis tool and slowly fix the errors (only about 20 are relevant – the rest are pedantic).
- Make everyone use “Convert tabs to spaces” and set to 4. (HICPP)
- All if and else statements must be enclosed in braces, even if a single line. (HICPP)
- Apply SOLID principles to code (Single responsibility, Open/Closed, Liskov Substitution, Interface, Dependency Inversion).
- Evangelise to others in team.
Others:
- You should be able to get ~1,500 unit tests per second.
- Some things you can’t test (UI, threading) so try to separate code from logic at test that (DoSomethingUI and DoSomething, and read The Humble Dialog Box).
- It will seem at first you will be writing a lot of extra code with tests and refactoring, but stick with it.
Did I miss anything?
June 29th, 2011 at 08-59
You at ABB on Belasis at the moment? Sure I’ve seen you walking down Belasis Avenue a couple of times…