The first step in refactoring code, regardless of the language, is having repeatable, working tests. This allows you to make changes to code safe in the knowledge that your changes do not break anything---if you have evidence to assert that your new code does what the old code did, you'll find it much easier to improve your code base…