Ruby Code Golf is an activity that I personally found vastly improved my understanding of the Ruby language structure and syntax, however there are some downsides to participation of which the beginner Rubyist should be aware.

Background

Code Golf is an activity where one attempts to solve a given coding problem with as few characters as possible. As a competition, it is a race to finish the given task both first, and with the lowest character count.

In preparing to return to studying full stack development after an 18 month hiatus, I accidentally found myself involved in Ruby Code Golf while trying to brush up on my Ruby syntax. In fact, I did not encounter the term ‘Code Golf’ until several months after I had been unwittingly doing it.

While completing the daily challenges on the CodeSignal website, I noticed that the user rankings for the solutions to each challenge were based firstly on the number of characters for the given solution, and secondly by the first to complete the solution. So, if I was the second user to complete a challenge in 50 characters, my placement was right behind the first user to complete the challenge in 50 characters, jumping ahead of everyone who completed the challenge in more characters, but in less time than I. This ranking system brought out some competitiveness in me, and lead to my practice of golfing Ruby code.

On CodeSignal, a daily challenge has a set time during which users can submit solutions. The solutions page for a challenge hides all source code until the challenge timer has run out; however, user rankings and character counts are still visible.

When solving a challenge, I would first code my own solution, then make revisions to attempt to get as close to the shortest solution as possible. After the challenge ended I would review the highest placing user solutions and spend a great deal of time dissecting other’s code to learn how it was put together.

Things I Loved About Code Golf

  1. Reading lots of code. In reviewing solutions to improve my terseness I had plenty of practice reading the code of others and dissecting logic different from my own. My improvement
  2. A great review of Ruby core methods. In chasing short solutions, I became very familiar with a lot of Ruby’s core methods, especially Array and Enumerable methods. Not only was I able to use methods in their usual forms, but also in many of their more obscure and esoteric forms - really getting an exploration of what all a particular method could do.
  3. Better feel for approaching problems. After some time golfing code, I noticed I had a more instinctual approach to problem solving. Having seen many different ways to attack a given challenge, I could more quickly decide which data structures and methods to pull from my toolbox in order to start building a solution. I gained more experience with Regular Expressions for String challenges, and even found myself reaching for bitwise operators that had previously confused me. I had a better feel for when to user iteration through a method or a loop (or when to use recursion), without having to think as much about edge cases before typing out code.
  4. Understanding Precedence. While chasing the shortest code, parentheses were my enemy in code golf. Eradication of parentheses is wholly reliant on putting operators in such an order so that I got the correct result in the shortest characters.
  5. Knowing when to quit. I found that I could easily drop down the rabbit hole chasing a shorter version of a solution to a challenge. Being one or two characters away from a great placement in the solution rankings is as frustrating as it can be infuriating. I found an obsessive drive to try and tweak one last revision to get a better solution. Perhaps the best thing I learned was how to tell myself, “This is good enough for today. This is a good enough solution. Let’s move on to something else.”

Pitfalls You Should Look Out For

  1. Bad coding style. Forget do...end blocks. All blocks are {} blocks. Avoid parentheses and don’t give any variable a name longer than a single character. Luckily, I had experience with good Ruby coding style. But if you are going to golf code, expect to throw best practices out the window and write some really ugly looking, garbled code. It is easy to pick up some bad habits here.
  2. Lack of clarity. Beyond just code style sins, It is easy to develop the instinct to write unnecessarily clever code all the time. Code golf is about packing as much functionality into each line of code as possible, without considering other’s need to easily understand what that code is doing. Remember that generally you should be writing code that others will read, and that sometimes brevity needs to be eschewed for the sake of clearly communicating what your code is doing.
  3. Lack of method writing. This is what I struggle with after my stint golfing code. Defining methods adds a lot of extra characters to a solution. Now when I write code (outside code golf) I have a tendency to avoid extracting logic into separate methods unless I can foresee a need for multiple calls to that method in various points in my code. This is my bad habit, and leads me to writing methods that are often times too dense, and try to do too much at once.