Why abstraction is important when constructing algorithms
This algorithm, only 43 lines, took me about an hour to code. I already knew the details of the algorithm from the video. Why? Because I was prepending the wrong number of zeroes to the number when figuring out where to split the number.
Why was this happening? Originally, my
prependZeroes method was being done inline, and I wasn’t testing it properly.
Abstraction is important because you can test individual parts of an algorithm to make sure they are correct so you can figure out where your algorithm is failing. It also allows your code to be more symmetrical, making it a lot easier to follow.
If I had abstracted the prependZeroes method and had written unit tests for that method specifically, this algorithm would have been much faster to write. Instead, I wasted a ton of time writing
console.log statements in order to figure out where my bug was.
Break algorithms up into as many functions/parts as possible, so you can test your algorithms much more easily.