Why abstraction is important when constructing algorithms

In order to get better at algorithms (my greatest weakness), I’m currently going through the Stanford algorithm design course. To learn the concepts better, I decided to implement Karatsuba multiplication in JavaScript. (The fruits of my labor can be found on my miscellaneous algorithm GitHub repository.).

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.


Thanks for reading my post! If you enjoyed it or it helped you, please consider liking/tweeting this page, commenting, or following me on GitHub or Twitter!