Code Golf

Colin DeCarlo
Vehikl News
Published in
2 min readFeb 18, 2016

From time to time a friend and I play a programming game that I consider to be a version of Code Golf. Typically, the goal of Code Golf is to solve some challenge using as little bytes as possible. In the version of the game we play however, the limitation is the use of certain language features. Specifically, we try to use as little variables, conditionals and explicit loops as possible.

When reflecting on these sorts of challenges, I’m able to derive two clear benefits that I get out of them.

Creative Thinking

By imposing these limitations I’m forced to think more creatively about the problem. For instance, by limiting the use of conditionals I have to study the problem more intensely and distill the solution down to a very generalized algorithm. When doing this, I’m always reminded of the Fibonacci sequence and how it’s elements can be calculated directly using Binet’s Formula.

Working with the limited feature set of the language is almost like learning a new language altogether and I find I get similar benefits from learning the “new” language as I do learning other, completely different, languages. I’m exposed to new techniques of accomplishing common tasks and sometimes these techniques are more concise than my old methods. Without a doubt, incorporating these techniques into my everyday programming habits makes me a better developer.

Deeper Understanding

One of the benefits to working in a language like PHP is it’s extensive core. I’ve always said that if you want to calculate something, there is probably a function in the core that does it. Take for instance calculating the minimal number of edits required to transform one string into another, PHP has had the levenshtein function since version 4.0.1.

A downside however to an extensive core is just that, it’s extensive. There’s so much functionality in there that knowing what’s available is a problem in itself. Having to work within the limitations of the challenge you find yourself spelunking through the docs looking for methods you can lean on. In doing so, you gain so much more knowledge about the language that you can draw on in your day to day work.

A side benefit to this improved understanding is you learn how you can abuse certain language features. While I wouldn’t recommend abusing the language in your production code, it’s always fun to bring up these quirks at local meetups.

Challenge

My method to accomplishing these challenges is to first write the algorithm in the simplest, most straight forward manner and then chip away at that solution until I’ve satisfied the challenge conditions.

The most recent challenge we posed was calculating the transposition of a matrix. Below are the various iterations of my solution, I’ve also pushed them to a repo on GitHub.

Just get the solution done and working correctly.
We can eliminate the conditional by instantiating the transposed row between the two for loops.
Eliminate variables by inlining their values.
Arrays (even with multiple dimensions) can be instantiated on the fly.
Array functions are a great way to avoid explicit loops.
After some sleep, I realized the reduction was actually a map.

As you can see, the final solution is radically different from the original. I’m pretty happy with it, no conditionals, no temporary variables, and no explicit loops. As well, I’m happy to report that I finally found a use for array_column.

--

--