What is Code Golf?
Smartpedia: Code Golf is a competition between programmers in which the winner is the one who solves a programming task with the fewest characters.
Code Golf – the solution with the fewest characters wins
Code Golf is a competition between programmers. Like golf, where athletes try to put a ball into a hole with as few strokes as possible, the one who solves a task with as few characters as possible wins. Mostly implementations in a predefined programming language are compared. Setting time limits for the submission of solutions is also not uncommon.
The origin of code golf goes back to a Perl community, where developers started to solve a basic task in a thread with decreasing amounts of code. In July 2000, the First Annual Perl Golf Apocalypse was proclaimed.¹ Today, code golf is also called script golf, as scripting languages such as Ruby, Rust, Phython, PHP, JavaScript, Lisp, etc. are often used in competitions. Alternatively, it is sometimes called Spartan programming.
What is the benefit of Code Golf?
What is the benefit of code golf for software developers?
- Participating in a contest is fun for many developers.
- It allows the comparison of solutions of different developers. Such a comparison is usually rarely possible in software development, because usually teams work together on a solution or individual developers take care of individual tasks or requirements.
- It helps to improve skills, e.g. when features of programming languages are used that are rarely or never used.
- It could also be a training for whiteboard coding.
And where is the benefit for companies? Of course companies are interested in good software developers and high-performance solutions, but most of all they are interested in solutions that are easy to understand and maintain. Developers not only spend a lot of time reading code implemented by other developers during a code review, but often – e.g. during a refactoring – they also deal with lines of code that they themselves wrote earlier. For a competition, it may make sense to shorten variable or function names to single letters – as some minifiers do – or to do without comments, but in a professional context, colleagues and clients will rarely be happy about this. Certainly it makes sense to minimise the number of variables, to avoid persistent variables or to use short, meaningful variable names – but always with a view to the traceability and maintainability of a software.
Notes:
[1] Annoucing the First Annual Perl Golf Apocalypse
If no specific programming language is specified in a competition, the development of own programming languages is also possible. Here you can find a list of so-called esoteric programming languages or Golfing Languages.
In a certain way there is a similarity between Code Golf and Code Kata. A code kata is an exercise that focuses on learning new skills and developing successful routines. One difference is that a code kata is about finding multiple solutions, whereas code golf is about implementing one with the fewest characters.
Here you can find a nice collection of code golf examples.
Here you will find additional information from our Smartpedia section: