views:

76

answers:

3

You have some code you want to remove associated with an obsolete piece of functionality from a ruby project. How do ensure that you get rid of all of the code?

Some guidelines that usually help in refactoring ruby apply, but there are added challenges because having code that isn't being called by anything won't break any unit tests.

Update: Has anyone written anything that allows you to guess based on your version control history if there are commits where you have since deleted most, but not all, of the code and can point out the remaining code?

A: 

Especially in a dynamically typed language, there is no easy way to do this. If you have unittests, thank the developer that wrote them because it will help you remove the code correctly. But you're basically SOL. Remove the code, if it breaks, put it back, figure out where it broke, attempt to work around it, and repeat.

xyld
A: 

Look at your code coverage. Any code which isn't covered may be part of the code you have left to remove (if any). (Just be sure you have removed you tests. =])

strager
+2  A: 

Current thoughts:

  • Identify the outermost part of the stack associated with the obsolete functionality: the binary script calling it, or the unit tests calling it.
  • Look for methods that are only called by methods associated with the obsolete functionality. I often use git grep for this.
  • In theory, running mutation testing and looking for code that used to be mutation resistant when the old test suite applied, but is now mutation prone might help. It only helps if your code was well-tested in the first place! (Or you can use code coverage tools such as rcov rather than mutation testing)
  • Running test suites will ensure you haven't removed anything you shouldn't have!
    • Using autotest can save you time if you're constantly running tests.
  • If your code was well-structured, it should be easier to find related methods that need to be removed.
Andrew Grimm
+1, although it gets a little bit boring, because the answer to pretty much any question regarding software engineering in Ruby (or any language, for that matter) seems to be: "have high-quality tests". :-)
Jörg W Mittag