HOME

TheInfoList



OR:

Code golf is a type of recreational
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
competition Competition is a rivalry where two or more parties strive for a common goal which cannot be shared: where one's gain is the other's loss (an example of which is a zero-sum game). Competition can arise between entities such as organisms, indi ...
in which participants strive to achieve the shortest possible
source code In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer. Since a computer, at base, only ...
that solves a certain problem. Code golf challenges and tournaments may also be named with the programming language used (for example, ''
Perl Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language". Perl was developed ...
golf'').


Etymology

The term "code golf" is derived from the similarity of its goal with that of conventional
golf Golf is a club-and-ball sport in which players use various Golf club, clubs to hit a Golf ball, ball into a series of holes on a golf course, course in as few strokes as possible. Golf, unlike most ball games, cannot and does not use a standa ...
, 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 In algorithmic information theory (a subfield of computer science and mathematics), the Kolmogorov complexity of an object, such as a piece of text, is the length of a shortest computer program (in a predetermined programming language) that prod ...
of the output, and its mathematical study dates to the work of
Andrey Kolmogorov Andrey Nikolaevich Kolmogorov ( rus, Андре́й Никола́евич Колмого́ров, p=ɐnˈdrʲej nʲɪkɐˈlajɪvʲɪtɕ kəlmɐˈɡorəf, a=Ru-Andrey Nikolaevich Kolmogorov.ogg, 25 April 1903 – 20 October 1987) was a Soviet ...
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 Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language". Perl was developed ...
, and later popularised through the use of Perl to write a program that performed RSA encryption, 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. 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 languages have been created specifically with code golfing in mind. Examples include GolfScript, Flogscript, Stuck, and Vyxal, which are
Turing-complete In computability theory, a system of data-manipulation rules (such as a model of computation, a computer's instruction set, a programming language, or a cellular automaton) is said to be Turing-complete or computationally universal if it can be ...
languages that provide constructs for concisely expressing ideas in code. Because golfing languages compete for extreme brevity, their design sacrifices readability, which is important for practical production environments, and therefore they are often
esoteric Western esotericism, also known as the Western mystery tradition, is a wide range of loosely related ideas and movements that developed within Western society. These ideas and currents are united since they are largely distinct both from orthod ...
. Sometimes, however, a language is designed for a practical purpose but turns out to be suitable for code golf. An example of GolfScript code to print 1000 digits of pi: ;'' 6666,-2%* `1000<~\; Code golf websites include novel golfing languages created by users to win code golf challenges. Other popular languages include 05AB1E, Husk, Pyth, CJam and Jelly.


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 A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
. 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

In 2019, cryptography researchers Xavier Bonnetain, Léo Perrin and Shizhu Tian used code golf and the closely linked concept of
Kolmogorov complexity In algorithmic information theory (a subfield of computer science and mathematics), the Kolmogorov complexity of an object, such as a piece of text, is the length of a shortest computer program (in a predetermined programming language) that prod ...
to show that the
S-box In cryptography, an S-box (substitution-box) is a basic component of symmetric key algorithms which performs substitution. In block ciphers, they are typically used to obscure the relationship between the key and the ciphertext, thus ensuring Clau ...
used in the Streebog and
Kuznyechik Kuznyechik (, literally "grasshopper") is a symmetric block cipher. It has a block size of 128 bits and key length of 256 bits. It is defined in the National Standard of the Russian Federation GOST R 34.12-2015 and also in RFC 7801. The name of ...
cryptography algorithms was significantly less random than its creators claimed it was.


See also

* Code poetry *
Data compression In information theory, data compression, source coding, or bit-rate reduction is the process of encoding information using fewer bits than the original representation. Any particular compression is either lossy or lossless. Lossless compressi ...
* Minification (programming) * Perl pastimes *
Kolmogorov complexity In algorithmic information theory (a subfield of computer science and mathematics), the Kolmogorov complexity of an object, such as a piece of text, is the length of a shortest computer program (in a predetermined programming language) that prod ...
* Kata (programming)


References

{{reflist


External links


CodeGolf.StackExchange.com: Questions and answers on programming puzzles and code golfGolf
on the esoteric programming languages wiki
List of dedicated golfing languagesList of all languages used 10 times or more on code golf stack exchange, ranked by brevity
Programming contests