27
\$\begingroup\$

Hello,

I have spent time looking at the StackExchange sites and especially this has seemed quite interesting to me. But I would like you to explain how it works and what it is about. I know how to program in many languages, but in the answers I always see unusual programs.

I also see that they are like 'challenges', which users respond to by programming, but I do not understand how they can do so in such a few lines of code and how these challenges and their respective responses work? And what specifically does 'code golf' mean?

\$\endgroup\$
4
  • 4
    \$\begingroup\$ Hello! Welcome to the site! I have edited your question so that it is more understandable, and so that it is clearer. If you dislike the changes I've made, feel free to edit in your own changes. \$\endgroup\$ Sep 21, 2017 at 6:59
  • 11
    \$\begingroup\$ unusual programs is a bit of an understatement :-D \$\endgroup\$
    – Luis Mendo
    Sep 21, 2017 at 10:18
  • 5
    \$\begingroup\$ This might be FAQ-worthy - it's certainly asked frequently! \$\endgroup\$
    – wizzwizz4
    Sep 22, 2017 at 6:13
  • \$\begingroup\$ @wizzwizz4 If the answers will be written in a canonical and authoritative way, definitely. \$\endgroup\$
    – Mast
    Sep 27, 2017 at 8:46

2 Answers 2

36
\$\begingroup\$

What does "Code Golf" mean?

Code golf, (Or Codegolf) as the name suggests, is the art of making programs that are as short as possible that complete a task.

What is "Code Golf & Coding Challenges"?

Code Golf & Coding Challenges – formerly known as "Programming Puzzles & Code Golf" or PPCG – is a Community in which people ask Questions (Challenges), and others answer them. The challenges are usually (See above), although can sometimes be , and others. All challenges must have an Objective Winning Condition, which means there must be a rule that can determine which of two answers is better. In Code Golf, this is "the shortest code wins".

What makes a challenge, what makes an answer?

A challenge is something that can be solved by a program, this usually means taking certain inputs and providing certain outputs, with some criteria to show how some solutions are objectively better than others. (This usually has nothing to do with what's good practice, that's Code Review's job. :p)

Answers are just solutions to the task a challenge provides, these must contain:

  • The language the solution was written in
  • The length of the solution in bytes
  • The solution itself

The way these solutions take input is usually defined by Our I/O Defaults unless otherwise specified in the challenge, and we also have a list of Forbidden Loopholes to prevent "cheap" solutions, like loading and executing the solution from a webpage.

Why is everything so short?

Since this community's creation, users have been trying to outdo eachother, both in code length of the same langauge, and in choice of language.

As you can only go so far with languages like C, Java, Python, or Perl, users (and those from outside the community) create their own esoteric "Golfing Languages". These languages put understandable code to the side, and instead implement tricks to help their code be as short as possible.

GolfScript is one of the earliest languages to do this (And gained a lot of negative heat in its day because of it) and was a Stack Based language where almost every command was a single ASCII character. This means that reversing a string goes from string.reverse(input) to just -1%.

Now, languages like Jelly have their own Code Page so they can make the most out of each byte and use other tricks to make it even more ridiculously short.

Don't be intimidated by this!

Although users like to try to outgolf each other at the language level, (I myself am competing with RProgN2), this is all in good fun, and competition is really intra-language, rather than inter-language. Feel free try to take on challenges in any language, and try to do better than anyone else who's already done it, or do it first to set the record. It is, of course, all in good fun.

Check out our FAQ for more info!

\$\endgroup\$
4
  • 11
    \$\begingroup\$ To outline the fact that this is all for fun: there are people submitting answers using Operation flashpoint or Minecraft. It's also common practice to help improve a solution or, as said in this answer, to post better answers than previous ones in the same language, when a different approach is found \$\endgroup\$
    – scottinet
    Sep 21, 2017 at 8:54
  • 6
    \$\begingroup\$ When starting codegolfing yourself, it might be useful to look for Tip-topics. Like Tips for golfing in <all languages> or Tips for golfing in Java for example. And when first creating a challenge of your own, it's best to use the Sandbox for proposed challenges to get feedback, and alter things in your challenge you might have overlooked. \$\endgroup\$ Sep 21, 2017 at 9:18
  • \$\begingroup\$ @scottinet There have even been winning answers in Minecraft. \$\endgroup\$
    – KSmarts
    Sep 22, 2017 at 14:45
  • \$\begingroup\$ And there's nothing stopping you from posting a longer answer as long as it approaches the problem differently. I've done this at least once. Generally you'll get comments about how to improve and you can do better next time. For example, I learned about map (and sneaky ways to define variables in one) from that answer and wrote this one. Mostly though, I do KoTH challenges. \$\endgroup\$ Sep 28, 2017 at 17:38
12
\$\begingroup\$

I would just like to add that while the in fact is a competition, it won't feel like one very much. It's more of showing off nice approaches to solving the challenge — there's only one answer for most languages in each thread. Also, as already pointed out, people will tell you when they see a way to improve ( = shorten) your code. So, in the end, it is more of a "collaborative effort" to overcome the challenge in the least number of bytes than a competition.

I cannot but second the Don't be intimidated by this. The people are very friendly and will help you out. Often, they will show you a much better approach in the comments, and it's not rare for a challenge to be cut into 1/2 of the size by this.

I'd recommend you to start golfing in a language you know, but aren't a particular master of. Browse the docs liberally, try to find the best solution, use the help of other people and you will soon find your knowledge of that language skyrocket. For me, this happened with Perl 6. And you can even learn whole new approaches to programming like this — most likely functional programming — which is, in my humble opinion, totally awesome, and makes it possible to write concise, elegant and powerful code.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ "only rarely you will find [a challenge] with two answers in the same language" - Python, in particular, would beg to differ! \$\endgroup\$
    – Shaggy
    Sep 21, 2017 at 11:58
  • 1
    \$\begingroup\$ @Shaggy: Reformulated. Now I hope Python is content! :) \$\endgroup\$
    – Ramillies
    Sep 21, 2017 at 13:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .