What are some techniques I can use to refactor Object Oriented code into Functional code?
- by tieTYT
I've spent about 20-40 hours developing part of a game using JavaScript and HTML5 canvas. When I started I had no idea what I was doing. So it started as a proof of concept and is coming along nicely now, but it has no automated tests. The game is starting to become complex enough that it could benefit from some automated testing, but it seems tough to do because the code depends on mutating global state.
I'd like to refactor the whole thing using Underscore.js, a functional programming library for JavaScript. Part of me thinks I should just start from scratch using a Functional Programming style and testing. But, I think refactoring the imperative code into declarative code might be a better learning experience and a safer way to get to my current state of functionality.
Problem is, I know what I want my code to look like in the end, but I don't know how to turn my current code into it. I'm hoping some people here could give me some tips a la the Refactoring book and Working Effectively With Legacy Code.
For example, as a first step I'm thinking about "banning" global state. Take every function that uses a global variable and pass it in as a parameter instead.
Next step may be to "ban" mutation, and to always return a new object.
Any advice would be appreciated. I've never taken OO code and refactored it into Functional code before.