I’ve been reading the Programming Puzzles & Code Golf part of the StackExchange for a few years – mostly for the codegolf. I really like the concept of simple challenges that almost any competent programmer can solve, but only the best can produce the algorithm that executes the fewest instructions that always provides the expected output in the lowest number of bytes

What is Code Golf

If you’ve never tried code-golfing you should definetely check it out. Codegolfing is about solving a particular problem by developing an algorithm using the fewest possible bytes. Almost every single programming language is allowed – and there are even programming languages made for code golfing. These languages are known as golfing languages and includes:

In most cases a golf challenge asks you to create a function that takes some input and outputs the correct output in as few bytes as possible.

 

A code-golf example

I had been reading the Codegolf part of StackExchange for a long time when I finally decided that it was time to step in. This challenge was my very first code-golf attempt.

The challenge was to create a program that takes no input and produces the following output, and no more:

Built-in constants that contain 9 or more of the above characters are disallowed.

Besides the restraints defined by the author, code golfers has to abide by some rules that define standard loopholes that are forbidden. Because of this, you are not allowed to just print the input as is.

 

My solution

When I got to this challenge I decided that RegEx should solve this quickly (and hopefully shortly), and decided to take a shot at it using C#

This code loops over all the chars between 0 and ~ and matches each char with a regular expression. The output is printed using a Ternary Operator – if a match is found the char is printed – otherwise nothing is printed.

The code above was my final submission – it started out as much longer, but was cut from 168 bytes to a mere 106 bytes with help from some of the regular codegolfers.

I realized a few things that I knew (theoretically), but I just hadn’t given it a thought:

  • You don’t have to use an integer to loop – you can loop with chars (in my example from from ‘0’ to ‘~’)
  • The keyword var is shorter than char
  • You dont have to use a.ToString() to convert a char to a string – you can just do a+””
  • The RegEx \w targets underlines too (not just words, digits and whitespace)
  • There are lots of places in C# where it is not neccesary to use Whitespace (for instance when using Ternary Operators)
  • Variable names should always be one letter – and if you use a variable defined by a function multiple times, you should define it as a variable

 

If this was just a regular programming task, my submission might have looked like this instead

There are no comments, but it should be easy to see what is happening in the code.

 

Other submissions

Submissions were send in in over 30! different programming languages including C, C#, Java, Javascript, PHP, Perl, PowerShell, Bash, Python and Ruby. Almost everyone had thought the same as me – filtering a loop using regex, but some had used native commands to print ranges of chars. The winner of the code-golf was (as one would expect) using a dedicated golfing language called Convex. The shortest solution submitted by GamrCorps was:

Explanation:

What I really like about golfing is that people still upvote and take interest in submissions that won’t win. As an example of that I received more than 20 comments and suggestions on how to better improve my code for future codegolf submissions – and while the best solution had done it in 9 bytes, other solutions were far more popular, such as the Perl, Ruby and Javascript implementation receiving more attention and upvotes.

 

Why should I codegolf

When working with code on a daily basis you probably don’t ever want to see golfing code in any code that you and your team has to maintain. Still I argue that getting involved with code golfing will be good for your code quality. Not to shave off a few additional bytes of your sourcecode, but to promote alternative thinking.

You really start to explore the syntax of a language – and more often than not your standard textbook won’t be of any help here.

Although syntax can really make a difference in code length (a+”” is 4 bytes, whereas a.ToString() is 12 bytes), most of the good solutions are based on code that executes the absolute minimum number of instructions required to solve the problem.

I believe that learning to see a solution to a programming task as a series of instruction and contemplate how few instructions it can be completed in can help you write good, efficient and easily readable code.

 

Golfcode in software

This should be obvious, but if you ever decide to use golfcode in your software (not recommended!) remember to document and comment the code so that other can figure out what the hell you were thinking when you decided to replace this code:

with this