I had an epiphany of sorts the other day. Maybe what I’m going to describe is obvious to everyone else out there, but it just “clicked” for me all of the sudden: Occam’s Razor is foundational to our jobs as software developers.
What is Occam’s Razor
It’s a principle that comes up a lot in numerous fields. I first encountered it while working on my thesis in Machine Learning and Data Mining, but it also applies in statistics, medical diagnostics, AND software development.
Occam’s razor (also written as Ockham’s razor and in Latin lex parsimoniae) is a problem-solving principle devised by William of Ockham (c. 1287–1347), who was an English Franciscan friar and scholastic philosopher and theologian. The principle states that among competing hypotheses, the one with the fewest assumptions should be selected. Other, more complicated solutions may ultimately prove correct, but—in the absence of certainty—the fewer assumptions that are made, the better. – from Wikipedia
My over-generalization of the principle? A simple solution is more likely to be correct than a complex one. Why is that? Because in a given solution space, there are fewer simple solutions, and a nearly-infinite number of complex ones.
Consider a really simple problem. “Given an integer X, find the next integer larger than X.” The solution is simple: f(x) = x + 1. But how many more complicated solutions exist for this problem? Here’s just a few:
f(x) = x + 2 – 1
f(x) = (x*100) / (10 * 10)
f(x) = (x^2/x) + 1
You get the idea.
In my head, I picture the solution space of a problem as looking something like this:
Yes, this is an oversimplification, but hey, it’s my head, I’m allowed to use whatever representation I want!
How does this apply to us as software developers? What function is easier to understand, a simple one or a complex one? Which code-base would you rather maintain? One that’s complicated, with convoluted solutions that no one can decipher, or one that’s elegant and simple? Which one do you think is going to be cheaper to maintain?
As software developers, our job is to find simple, maintainable solutions to complex problems. More often than not, we’ll fail to find that perfect solution, and we don’t want to waste too much time searching for it, but we must strive to find solutions at the top of the triangle, and avoid the bottom of it.
Keep Occam’s Razor in mind next time you’re writing code. If the solution you’re implementing seems complex, consider that there might be a simpler solution. You might just have to back up a step to see it.