My suggestions to a client on improving their legacy code.

Initially:

  1. Move all source code from VSS into TFS.
  2. Ensure all code needed to compile is within the workspace.
  3. Ensure all dependencies are correct so compilation of any one module will automatically compile needed dependencies for that module.
  4. Add UnitTest++ (or CppUnit – whatever floats your boat) to project.
  5. 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:

  1. Move the logical projects into folders. Create another physical project alongside this project. Usually you will call this Test or Tests.
  2. For each module you are editing in add a c++ source file in the test project. Format for this will be Test.cpp.
  3. 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:

  1. 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.
  2. Turn on treat warnings as errors and fix.
  3. Get a static analysis tool and slowly fix the errors (only about 20 are relevant – the rest are pedantic).
  4. Make everyone use “Convert tabs to spaces” and set to 4. (HICPP)
  5. All if and else statements must be enclosed in braces, even if a single line. (HICPP)
  6. Apply SOLID principles to code (Single responsibility, Open/Closed, Liskov Substitution, Interface, Dependency Inversion).
  7. 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?