When to Refactor

Today I found a class file that had been created with 1161 lines in it. Files this big should immediately ring alarm bells because the class they contain is almost certainly doing too many things. Class files this big are inevitably difficult to test and sure enough the test file was about 1700 lines.

Some guidelines on when to refactor:

  • The class is doing more than one thing.
  • You can't get a method on one screen.
  • There is poor on non existent documentation.
  • There is poor or no unit testing.
  • Class names and method names fail to adequately describe what the classes and methods do.
  • Methods have too many parameters. In the case of the class above the constructor took (Resource, Resource, pointer to function, Resource) which isn't necessarily too many its just a little strange and that strange smell points to refactoring.

Documentation is tied to unit tests because the unit tests describe what class methods do.

Here's a useful example of TDD:
http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata