Code golf
This article needs additional citations for verification. (September 2021) |
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.[1][2] Code golf challenges and tournaments may also be named with the programming language used (for example, Perl golf).
Etymology
[edit]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
[edit]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,[3] and later popularised through the use of Perl to write a program that performed RSA encryption,[4] 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.[5] 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
[edit]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
[edit]The first recorded dedicated golfing language is GolfScript,[6] released in 2007, with a derivative called floGScript releasing the next year.
In 2014, CJam, heavily inspired by GolfScript, released.[7] Along with APL, it would inspire the creation of Jelly, releasing in 2015.
Since the release of Jelly, many dedicated golfing languages have been released. Notable examples include Japt, Pyth, 05AB1E, Uiua, Vyxal, and Charcoal.
Design
[edit]Some golfing languages, such as Japt and Pyth, are transpiled to conventional programming languages[8] (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 or related to golfscript, are stack-based and concatenative, though counterexamples exist, most notably Husk.
Most golfing languages are high-level, and almost all shorten symbols to a single character. In modern implementations, such as in Uiua and 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 parenthesises 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
[edit]An example of GolfScript code to print 1000 digits of pi:[9]
;''
6666,-2%{2+.2/@*\/10.3??2*+}*
`1000<~\;
An example of Vyxal 3 code for Fizz buzz, annotated with an explanation[10]:
⑤ƛ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
, ## printTypes 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.
Practical applications
[edit]This section needs expansion. You can help by adding to it. (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.[11]
See also
[edit]- Code poetry
- Data compression
- Minification (programming)
- Obfuscation (software)
- Perl pastimes
- Kolmogorov complexity
References
[edit]- ^ Code Golf Stack Exchange. About code-golf. Retrieved 2021-12-21.
- ^ "Introduction to Code-golf | ASSIST Software Romania". Retrieved 2023-03-23.
- ^ Greg Bacon (1999-05-28). "Re: Incrementing a value in a slice". Newsgroup: comp.lang.perl.misc. Usenet: 7imnti$mjh$1@info2.uah.edu. Retrieved 2011-07-12.
- ^ Back, Adam. "RSA in 5 lines of perl". Retrieved 2011-01-10.
- ^ Andersen, Christian; Gram, Christian (1962). Lærebog i Kodning for GIER (PDF). Vol. 1 (3 ed.). Copenhagen: Regnecentralen. p. 104. Retrieved 2020-05-16.
- ^ "Timeline of golfing languages - Esolang". esolangs.org. Retrieved 2026-01-02.
- ^ "CJam". SourceForge. 2016-10-01. Retrieved 2026-01-02.
- ^ "japt/README.md at master · ETHproductions/japt". GitHub. Retrieved 2026-01-02.
- ^ "GolfScript Examples". Retrieved 2023-03-23.
- ^ "1, 2, Fizz, 4, Buzz". Code Golf Stack Exchange. Retrieved 2026-01-02.
- ^ Bonnetain, Xavier; Perrin, Léo; Tian, Shizhu (2019), "Anomalies and Vector Space Search: Tools for S-Box Analysis", Lecture Notes in Computer Science, Cham: Springer International Publishing, pp. 196–223, doi:10.1007/978-3-030-34578-5_8, ISBN 978-3-030-34577-8, retrieved 2025-05-12