{{More citations needed|date=September 2021}} {{short description|Recreational computer programming competition}} '''Code golf''' is a type of recreational computer programming competition in which participants strive to achieve the shortest possible source code that solves a certain problem.<ref>Code Golf Stack Exchange. [https://codegolf.stackexchange.com/tags/code-golf/info About code-golf]. Retrieved 2021-12-21.</ref><ref>{{Cite web|url=https://assist-software.net/blog/introduction-code-golf|title=Introduction to Code-golf {{!}} ASSIST Software Romania|access-date=2023-03-23}}</ref> Code golf challenges and tournaments may also be named with the programming language used (for example, ''Perl golf'').
== Etymology == 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. While conventional golf players try to minimize the number of club strokes needed to complete the course, code golfers strive to reduce the number of characters necessary (or keystrokes, to extend the metaphor) to write the program.
== History == 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,<ref name='perl-golf-coined'>{{cite newsgroup | author = Greg Bacon | title = Re: Incrementing a value in a slice |message-id= 7imnti$mjh$1@info2.uah.edu | newsgroup = comp.lang.perl.misc | date = 1999-05-28 | url = https://groups.google.com/group/comp.lang.perl.misc/msg/7b97c434492c8d20 | accessdate = 2011-07-12}}</ref> and later popularised through the use of Perl to write a program that performed RSA encryption,<ref name='rsa'> {{cite web | url = http://www.cypherspace.org/rsa/pureperl.html | title = RSA in 5 lines of perl | accessdate = 2011-01-10 | last = Back | first = Adam}}</ref> a similar informal competition is known to have been popular with earlier APL hackers. The challenging nature of aggressively optimizing for program size has itself long been recognized; for example, a 1962 coding manual for Regnecentralen's GIER computer notes that "it is a time-consuming sport to code with the least possible number of instructions" and recommends against it for practical programming.<ref name="GIER">{{cite book |last1=Andersen |first1=Christian |last2=Gram |first2=Christian |url=https://datamuseum.dk/w/images/b/be/LaerebogI.pdf |title=Lærebog i Kodning for GIER |volume=1 |edition=3 |location=Copenhagen |publisher=Regnecentralen |year=1962 |page=104 |accessdate=2020-05-16 }}</ref> Today the term has grown to cover a wide variety of languages, which has even triggered the creation of dedicated golfing languages.
== Dedicated golfing languages == Several new programming languages have been created specifically for the practice of code golf. These languages sacrifice readability for extreme brevity, and often provide high-level functions with single-character names, which makes them unsuitable in production environments where maintainability has higher importance than code size.
=== History === The first recorded dedicated golfing language is GolfScript,<ref>{{Cite web |title=Timeline of golfing languages - Esolang |url=https://esolangs.org/wiki/Timeline_of_golfing_languages |access-date=2026-01-02 |website=esolangs.org}}</ref> released in 2007, with a derivative called floGScript releasing the next year.
In 2014, CJam, heavily inspired by GolfScript, released.<ref>{{Cite web |date=2016-10-01 |title=CJam |url=https://sourceforge.net/projects/cjam/ |access-date=2026-01-02 |website=SourceForge |language=en}}</ref>
in 2015, motivated by CJam and APL, and inspired by J, Jelly released, and would position itself as a dominant contender for tersest language for many years.
Since 2015, many dedicated golfing languages have been released. Notable examples include Japt, Pyth, 05AB1E, Charcoal and Vyxal.{{cn|date=January 2026}}
=== Design === Some golfing languages, such as Japt and Pyth, are transpiled to conventional programming languages<ref>{{Cite web |title=japt/README.md at master · ETHproductions/japt |url=https://github.com/ETHproductions/japt/blob/master/README.md |access-date=2026-01-02 |website=GitHub |language=en}}</ref> (here, JavaScript and Python respectively). This makes the languages easier to learn, and guarantees a solid foundation for the design and performance of the language. This also simplifies the creation of web interpreters, which allow users to use the language without downloading anything.
Many modern golfing languages, being inspired by or related to golfscript, are stack-based and concatenative, but several languages inspired by Jelly, itself tacit and functional, instead follow its footsteps. Other paradigms, such as pattern matching languages, also appear in less notable quantities.
Most golfing languages are high-level, and almost all shorten symbols to a single character. In modern implementations, such as in Vyxal, the languages leverage single-byte character sets to make all commands printable, which further reduces readability due to the variety of symbols, especially with the incompatibility of code comments with the goal of code golf.
Golfing languages almost always make heavy use of operator overloading, and most include easily-accessible forms of text compression. In most languages, closing parenthesizes and other syntactic structures is optional at the end of the program. As a general design principle, dedicated golfing languages are heavily implicit, and most are tacit.
=== Examples === An example of GolfScript code to print 1000 digits of pi:<ref>{{Cite web |title=GolfScript Examples |url=https://www.golfscript.com/golfscript/examples.html |access-date=2023-03-23}}</ref> {{pre|<nowiki> ;'' 6666,-2%{2+.2/@*\/10.3??2*+}* `1000<~\; </nowiki>}} An example of Vyxal 3 code for Fizz buzz, annotated with an explanation:<ref>{{Cite web |title=1, 2, Fizz, 4, Buzz |url=https://codegolf.stackexchange.com/questions/58615/1-2-fizz-4-buzz/278392#278392 |access-date=2026-01-02 |website=Code Golf Stack Exchange |language=en}}</ref> {{pre| ⑤ƛ35f≛kF½•∨, ⑤ƛ ## map over 1..100 35f ## does 3 or 5... ≛ ## divide the number? result is 2-wide bitmask kF½ ## ["Fizz", "Buzz"] • ## multiply previous by bitmask, then concatenate ∨ ## if previous is falsey (empty string), push implicit n , ## print }}
== Types of code golf == 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.
== Practical applications == {{Expand section|date=May 2025}} In 2019, cryptography researchers Xavier Bonnetain, Léo Perrin and Shizhu Tian used code golf and the closely linked concept of Kolmogorov complexity to show that the S-box used in the Streebog and Kuznyechik cryptography algorithms was significantly less random than its creators claimed it was.<ref>{{Citation |last1=Bonnetain |first1=Xavier |title=Anomalies and Vector Space Search: Tools for S-Box Analysis |date=2019 |work=Lecture Notes in Computer Science |pages=196–223 |url=https://doi.org/10.1007/978-3-030-34578-5_8 |access-date=2025-05-12 |place=Cham |publisher=Springer International Publishing |isbn=978-3-030-34577-8 |last2=Perrin |first2=Léo |last3=Tian |first3=Shizhu|doi=10.1007/978-3-030-34578-5_8 }}</ref>
== See also == {{Portal|Computer programming}} * Code poetry * Data compression * Minification (programming) * Obfuscation (software) * Perl pastimes * Kolmogorov complexity
== References == {{reflist}}
== External links == *[https://codegolf.stackexchange.com CodeGolf.StackExchange.com: Questions and answers on programming puzzles and code golf] *[https://esolangs.org/wiki/Golf Golf] on the esoteric programming languages wiki *[https://esolangs.org/wiki/Category:Golfing_language List of dedicated golfing languages] *[https://codegolf.meta.stackexchange.com/a/8891/88192 List of all languages used 10 times or more on code golf stack exchange, ranked by brevity]
Category:Programming contests