\"Regular\" golf vs. code golf: Both are competitions. Both have a well-defined
ID: 639210 • Letter: #
Question
"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#.
Explanation / Answer
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.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.