Code golf

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Code golf is a type of recreational computer programming competition in which participants strive to achieve the shortest possible source code (not to be confused with binary Sizecoding) that implements a certain algorithm. Playing code golf is known as "golf scripting". Code golf tournaments may also be named with the programming language used (for example Perl golf).


The length of the shortest possible program that produces a given output (in any fixed programming language) is known as the Kolmogorov complexity of the output, and its mathematical study dates to the work of Andrey Kolmogorov in 1963. Code golf, however, can be more general than this, as it often specifies a general input-output transformation that must be performed rather than asking for a single output with no input.

Whilst the term "code golf" was apparently first used in 1999 with Perl,[1] and later popularised through the use of Perl to write a program that performed RSA encryption,[2] similar informal competition was known to have been popular with earlier APL hackers. Today the term has grown to be applied to a wide variety of languages, which has even triggered the creation of dedicated golfing languages. More systematic practical use of terse code is known as Spartan Programming.[3]


The term "code golf" is derived from the similarity of its goal with that of conventional golf, where participants seek to achieve the lowest possible score, rather than the highest, as is the standard in most sports and game scoring systems. Also, key strokes and club strokes.

Dedicated golfing languages[edit]

Several new languages have been created specifically with code golfing in mind. The most well-known examples include GolfScript and Flogscript, which are Turing-complete languages which provide powerful constructs for concisely expressing ideas in code. Golfing languages are generally considered[by whom?] to be esoteric, as useful applications other than for code golfing are very limited due to their limitations on overall speed, readability and function.[citation needed] Binary Lambda Calculus was designed to make Algorithmic Information Theory more concrete, but results in a scheme suitable for code golf.[citation needed] A list of dedicated golfing languages can be found on the wiki for esoteric programming languages.[4]

An example of GolfScript code to print 1000 digits of Pi:

`50<~\; #truncate for webpage purposes
 -> 3141592653589793238462643383279502884197169399375

Many code golf websites such as Programming Puzzles & Code Golf have many users constantly creating new and unique golfing languages to win code golf challenges. Some other notable examples of golfing languages are Pyth and CJam.

Types of code golf[edit]

Some code golf questions, such as those posed on general programming sites, may not require implementation in a specific programming language. However, this limits the style of problems that it is possible for the problem designers to pose (for example, by limiting the use of certain language features). In addition, the creation of such "open" questions has resulted in the design of code golf specific programming language dialects such as REBMU (a dialect of REBOL). Both online and live competitions may also include time limits.

Tournaments by language[edit]



See also[edit]

External links[edit]


  1. ^ Greg Bacon (1999-05-28). "Re: Incrementing a value in a slice". Newsgroupcomp.lang.perl.misc. Usenet: 7imnti$mjh$ Retrieved 2011-07-12. 
  2. ^ Back, Adam. "RSA in 5 lines of perl". Retrieved 2011-01-10. 
  3. ^ Spartan Programming, Jeff Atwood, Coding Horror, 08 Jul 2008
  4. ^ "List of dedicated golfing languages on the esowiki". 
  5. ^ "Anarchy Golf". 
  6. ^ "Just Another Golf Coding". 
  7. ^ "Shortening codes". 
  8. ^ "4clojure League". Retrieved 2011-07-24. While the primary purpose of is to teach Clojure "by doing", you may also choose to compete for the shortest solution. This is affectionately known as code golf: the lower your score the better, get it? If you choose to participate, we'll score your correct solutions based on the number of non-whitespace characters (and some more metrics in the future). We'll also provide a chart showing how you stack up compared to everyone else on the site.