When writing code, the simplest solution to a problem is the best
Occam’s razor according to Wikipedia is “the principle that ‘entities must not be multiplied beyond necessity’ (entia non sunt multiplicanda praeter necessitatem). The popular interpretation of this principle is that the simplest explanation is usually the correct one. Simplest is not defined by the time or number of words it takes to express the theory; is really referring to the theory with the fewest new assumptions.” (See the article here)
So what William of Ockham probably didn’t know, was how true his principle would be for modern man, especially programmers. I have had to maintain many systems in my career as a programmer. I have maintained excellent works of pure genius that I have had the privilege to learn from, and I have had to unfortunately also maintain the worst unreadable spaghetti code imaginable. But every now and then, I come across a situation where the code has been totally over complicated.
Sometimes, programmers learn a new buzzword (LINQ, Lambda etc.) So without really understanding the concept, they try to make use of this ‘new’ technology or programming concept to perform a specific function in code that otherwise could have been done in a much simpler way. While I am not saying that you shouldn’t use new programming concepts, I am saying that you shouldn’t use them for the sake of using them and without knowing what you are doing. If in doubt between two approaches, you need to keep the following in mind:
- How easy will it be to code
- How long will it take me to code
- Do I understand what I need to do
- How easy will it be for others to maintain
- How correct will my code be
- Am I trying to ‘show off’ by using this code
Always be a humble programmer and never try to show off. Some of the absolutely best programmers I have come across have been the humblest of people. It’s usually the cowboys that get things wrong. Think of what kind of legacy you want to leave. Remember, your code (if it works) will remain for long after you are gone.