What’s Code Golf? And why should Developers take part in such contests?

Bharat Ramakrishna
Skillenza
Published in
4 min readSep 11, 2019

Code Golf is not your run of the mill programming contest. It takes its inspiration from Golf. You know how, in Golf, the idea is to minimize your score?

In much the same way, in Code Golf contests, the idea is to write out a program under certain minimizing constraints.

These constraints are generally of two types. Solve a problem either by using the fewest number of source code bytes, or number of characters. Note that the problem setter is not looking for a beautifully coded up solution, or an efficient solution. It’s just to reduce the size of the damn program!

The most common type of Code Golf challenge is to use the fewest number of characters possible. So, from now on, unless we say otherwise, when we refer to Code Golf, we mean a contest to minimize the number of characters.

Got that?

Good.

We do need to clarify a couple of things.

Code Golf challenges can be language agnostic (that is, code up your solution in any language you want) or language specific.

Language agnostic challenges are probably the most interesting. And controversial!

Yes, I hear some of you programmers getting ready to throw stuff at me. Java programmers specially! Clearly programming in Java is not optimal.

Consider coding up Hello, World in Java.

It’ll look something like this:

Ugh.

Too many characters.

What about in Python?

This:

Nice!

What does this mean?

Does this mean Java programmers have no hope? Are Language Agnostic Code Golf Contests fair?

Yes and no. All’s fair in love, war and programming.

It’s true that Python programmers have an edge over the Java people. But Ruby programmers laugh at them both!

Here’s Hello World in Ruby:

Couple of fewer characters in the source code.

What’s the lesson here?

Does one class of programmers have an advantage over others?

Not really.

If you’re a good programmer, you should be able to pick up other languages quickly. It’s just a matter of learning the syntax.

And that’s the beauty of Code Golf!

Shop around the programming languages, explore their syntax and come up with your best solution to the problem.

Just look at the Hello, World example.

It looked at first that Python would win.

Then, after some exploration, looks like Ruby’s the best. It could be that there are better solutions out there, but you never know.

Oh wait.

Just thought of something.

Ruby’s solution could be optimized even further!

You could write:

Syntactic sugar ftw!

Of course, I didn’t just suddenly think of this. It’s just for dramatic effect. And I wanted to show you how you could use syntactical tricks in a language to optimize your solution even further. Even in such a simple program as Hello, World.

That’s the beauty of Code Golf, folks!

Your solutions depend on the kind of problem statement too. For mathematically related problems, you may want to use a language like Perl or something. For other cases, maybe Python wins out. Explore solutions in different languages to see what works best. And be aware or figure out syntactical tricks. Some contests accept only a select few languages. In some other cases, it’s a free for all. Depends on the contest rules.

(There are languages specifically designed for golfing such as GolfScript. But for the most part, many Language Agnostic Code Golfing Contests do not support such made to order languages. You’ve got to be creative with the mainstream languages!)

This is why you should go Language Agnostic Code Golfing every now and then. Not only will you get exposure to almost all the mainstream languages out there, you’ll get to explore a given language’s features and syntax to come up with an optimal situation.

It’s a different way of exercising programming muscles! And trying out new things in different ways is always good, right? It makes you a better programmer in the end …

I do want to say that there’s room for Language Specific Code Golfing Contests too. You’ll get to explore the syntax of a specific programming language. But this sort of contest seems to lack the certain Je ne sais quoi of Language Agnostic Contests.

Found the concept of Code Golf interesting and want to try it out? Why don’t you try a sample Code Golf Challenge on Skillenza’s platform and see if you can figure out the tips and tricks of the trade? Our editor supports 21 different languages, so you can really play around with different ideas. We also run code golf coding contests every month with lots of exciting prizes to be won. Try Code Golfing out! You’ll love it!!

--

--