Today is a good day to code

Occam’s Razor and Coding Philosophy

Posted: December 31st, 1969 | Author: | Filed under: Uncategorized | No Comments »

Occam's Razor and Coding Philosophy

Picture of IrvinI think that the GOF, when originally sitting down to write Design Patterns probably intended to do so in order to promote the philosophy of William of Ockham. For those not familiar with Occam's Razor, or Ockham's Razor as some spell it, it is the philosophy dealing with the nature of competing solutions to a particular problem and their complexity. To paraphrase the Wikipedia definition of Occam's Razor, it states that when given two potential solutions to a problem whose outcomes are both correct, the solution with the simplest implementation is the true correct solution. If the solution is predictive, the solution with the fewest assumptions must be the correct solution.

In applying this to coding, which I try to do, you'd have to view all coding or programming as trying to solve a problem, or balancing an equation. In a business, you have a business need that can be met with software. So this is an unbalanced equation, the business need is on one side of the equal sign, and the lack of anything is on the right. To solve this equation for zero, you will have to write software. Typically there are more than 50 ways to write any piece of software to solve the same business need. What makes this tough is that all 50 ways would probably work. Applying Occam's Razor to these 50 solutions will always yield a single proper solution, which will be the simplest to implement and of the least complicated code.

Some people would argue that this means the fewest lines of code, however one could, if one were so inclined write 5 lines of terribly complicated code, so sometimes many lines of code can be the proper solution.

In my experience, the developers I've met come in two varieties. There are the developers who like to program for programming's sake and can come up with some really cool stuff in R&D, but that's rarely useful in production code, well, I won't be nice, its rareley useful in any application. Basically, if your solution is more complicated than another solution, then your solution is wrong, incorrect, not right!

Then you have the coders who apply Occam's Razor to everything, whether they know it or not, and who write concise and clear code because they are always looking for the simplest solution. These developers are usually highly sought after, and command senior positions in organizations. The reasons are simple, they churn out code that is reliable and simple.

I think that Object Oriented Programming as a whole tends to uncomplicate software development. Some patterns appear to simplify development even further. However, with gratuitous application design patterns can take simple problems and turn them into unmanagable beasts, which is the wrong solution. What is really needed here is for many coders to stop being so territorial about their code, and open it up to scrutiny. Allow people to question your use of this framework, or that pattern, and openly consider that perhaps that isn't right for this project, then be willing to change. Lots of the all-night coding marathons which are now the folklore of the dot-com bubble are due to not applying the simplest solution to the problem. Philosophy and programming really go hand in hand, its just a shame that more programmers don't have literature, philosophy, or sociology backgrounds. In practice, however I do find that some of the best programmers I've ever met are highly artistic, and many have degrees in English, Philosophy, Science, etc… In fact many hiring managers and headhunters are starting to look for these qualities in their hiring of programmers.