Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

"Regular" golf vs. code golf:

Both are competitions. Both have a well-defined set of rules, which I'll leave out for simplicity. Both have well-defined goals; in short, "use fewer hits/characters than your competitors."

To win matches, athletic golfers rely on

  • equipment
    • Some situations call for a sand wedge; others, a 9-iron.
  • techniques
    • The drive works better when your feet are about shoulder width apart and your arms are relaxed.
  • and strategies
    • Sure, you could take that direct shortcut to the hole... but do you really want to risk the water hazard or sand bunker when those trees are in the way and the wind is so strong? It might be better to go around the long way.

What do code golfers have that's analagous to athletic golfers' equipment, techniques and strategies?

Sample answer to get this started: use the right club! Choose GolfScript instead of C#.

share|improve this question
2  
I'm confused... –  TheLQ Sep 2 '10 at 11:06
    
In other words, is there a "right way" to approach solving a code golf challenge? In other other words, what steps can you take to make sure you have the shortest possible code? –  Pops Sep 2 '10 at 16:36
    
Would the approach to solving a code golf problem be any different than solving any other programming problem? Does this question need the code golf restriction? If not then I would venture to guess this question has been answered, if yes then I do not think this is really a constructive question as its too specific. (My two cents for whatever its worth.) –  Chris Dec 27 '10 at 13:07
add comment

4 Answers

I'd say that thorough knowledge of the syntactical oddities of your language help. Here is one I found in Ruby when doing a bit of code golf:

Instead of

require "sequel"
require "nokogiri"
require "chronic"

You can do something like this:

 body_of_your_program if %w{sequel nokogiri chronic}.each{|i| require i}

With this kind of thing, you too can write incredibly elaborate Ruby one-liners!

In Ruby and Perl, you also get the magic variables like "$_" which can be used to do all sorts of magic with strings and regexes. Is your data not strings? Well, you might want to turn it into strings.

Obviously, in C, the preprocessor is your friend.

share|improve this answer
add comment

When I do a Code Golf challenge, I inevitably reach for a common set of design patterns that inevitably lead to shorter code: this set increases over time as more code golf challenges are done or just in the course of normal use of a language.

For example, if I need to have an incremental loop, one thing I might do normally is:

for ($i = 1; $i <= 10; $i++) {
  // code
}

or

$i = 1;

while ($i < 10) {
  // code

  $i++;
}

But this always works too:

while ($i++<10) {
  // code
}

Other things include:

  • Using shorter versions of standard library functions (fwrite() instead of file_put_contents())
  • Using variables to repeat expressions
  • taking advantage of how a language handles brackets and line breaks; in most code golfs, the output rarely requires a complex code block spanning more than one line (unless you're using a language where whitespace is substantial)
  • taking advantage of how a language handles output (<?= shorter than print or echo)
share|improve this answer
add comment

And as always: Know your library!

For most problems, there essential functions are already in the shipped libs. Of course you have to choose a non-verbose language for this or the imports of the needed functions will get longer than a short inline definition.

share|improve this answer
add comment

Depending on the Golf Code challenge, just start by coding it. No mather the amount of initial character, it's the first step. Trying to code from scratch with the least character can just make it harder. After that look for where you can optimize your code (use 1 character variable, do chainning operation, etc.) . This part involves mastering the language in which you are doing the Golf Code, the better you know it, the more you will know tweak that will save you characters.

Example of this strategy/technique :
http://stackoverflow.com/questions/3173415/code-golf-2d-platformer/3173614#3173614

As for the equipment, for most Golf Code challenge, the ideal equipment is to use a custom language made just for the challenge. Sometimes that language already exist, sometimes you can just invent it and code an interpreter of it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.