Problem: A function is confusing, but you need to enhance it to support a...


Problem: A function is confusing, but you need to enhance it to support a new feature.

Solution: Write unit tests that document the *current* behavior. Then, you can safely change the function. As long as the tests keep passing, you can be confident the behavior hasn't changed.
Example: A project I'm working on has untested, complex date parsing functions. No one is sure how they work, or what their goal is.

So, we're writing tests like this:
it('returns x when given y')

Then, we can safely begin refactoring.

View original on X