Code Golf : Beware of taking your hobby to work

I will aim to address the issue of over optimizing software code in enterprise software applications.  Looking for ways to optimize code and learning new techniques and concepts really make software development fun, but writing over optimized code does come with a health warning and can have some serious side effects on your project.

In order to provide some examples of optimized code, I have used PHP, but it should be borne in mind that the concepts and principles in this article can used in all programming languages.

All software engineers are aware of the programming algorithm known as “Fizz Buzz” (a popular interview question!) Essentially the concept behind Fizz Buzz is as follows:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

This solution usually looks something like this;

So far so good, now usually this is more than enough to solve the problem, nice simple easy enough to understand algorithm that serves as an example of how well you understand some of the basic concepts of programming. Everybody will be impressed with your skills!

Code Golf

The problem is when you want to flex your geek muscles, and attempt a bit of Code Golf. Code Golf is a fun recreational computer programming competition in which participants strive to achieve the shortest possible code that implements a certain algorithm. Don’t get me wrong code golf is fun, and I recommend anyone to try it. However, the danger with code golf is when it is transferred to the world of enterprise software development.

There are a number of solutions to the Fizz Buzz solution in the code golf world, here are some of the samples in PHP. These are often one liners;

These are some great examples and no doubt the guys who came up with these solutions, spent hours optimizing their code, and scouring the documentation, forums and blog posts and stackoverflow learning all the tricks needed to optimize code for the Compiler!

The problem is when you come across a sample of code golf in enterprise applications. Let’s take for an example a billing application, for a company, when somebody has noticed some irregularities in the billing process, which is losing the company alot of money. After some triage it is narrowed down to a single line of code that is causing the issue. There is some documentation that provides the purpose for the calculation, and you discover the code was developed by a team of programmers that have since moved on!

To complicate matters, it appears this same line of code is in a function that is being called by several other procedures in the application, and it works as expected in those procedures.

We should VART alot more

I believe most enterprise programmers need to tread a fine line, between having fun and writing code that is maintainable. There are 4 rules I adhere to when coding, and that is

  • V = Verbosity – code needs to vebose enough to be understood
  • A = Adaptability – Code needs to scalable enough that it can be reused with ease
  • R = Readability – Code should be readable, I don’t believe in comments, they tend to become out of date and very little use, therefore the code should be readable
  • T = Testability – all code needs to unit testable, so smaller chunks of code is best.

Conclusion

The important thing to remember is that when you’re writing code, you’re not always writing it for the compiler, which giving everything is syntactically correct will pretty much compile anything you throw at it, but most importantly you are writing it for the poor guy, who has to pick it up where you left off 2 – 3 years down the line and he needs to make some changes because requirements have changed!

So by all means enjoy taking part in Code golf tournaments but please leave that code out of your commercial software development.

What are your views on code optimization? I would love to read your feedback, why not leave a comment below

Gary Woodfine

Freelance Full Stack Developer at threenine.co.uk
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.
π