From the grand archives of “Things Chris was totally confused about for far too long and now half understands so might as well blog it”: code golf. I would hear someone say something like “I got it down that small from my own golf.” I would squint, confused, and just let it pass because it didn’t seem important enough to ask about. Slang for “work”, perhaps, or “tinkering”.
That’s not far off, really. The whole idea is reducing code down to as few characters as possible while remaining functional. In (real) golf, the fewer strokes to get the ball in the hole, the better you are doing. In code golf, the fewer characters you use, the better you are doing.
Sometimes code golf is a part of a competition (or tournament I suppose, keeping with the parlance). Sometimes it’s just for fun. Sometimes it’s useful. Sometimes the format is to solve a particular challenge or algorithm or achieve certain expected output from given input.
The understandability of “golfed” code goes right out the window. The code isn’t supposed to be understood, it’s supposed to be short and work. It’s common to look at a bit of golfed code with complete bewilderment. Here’s one:
function(a){a='0x'+a.slice(1).replace(a.length>4?a:/./g,'$&$&')|0;return[a>>16,a>>8&255,a&255]}
That’s a bit of JavaScript to take a hex color and return the RGB values from it. Hard to tell just by looking at it! It’s by Jed Schmidt, on his site 140byt.es which features tweet-sized bits of JavaScript that do functional things.
In the case of JavaScript, golfed code might be useful occasionally, since it’s all the less code to send over the network.
Wikipedia has an article on code golf, saying it started with Perl in 1999. Just about any language you can think of you can golf (it’s just a concept, after all) and probably even has a tournament. Both Reddit and StackExchange have dedicated subsites just for it.
Besides just being fun, it can be an exercise in developing a deeper understanding of the language as well as stretching your logic and reasoning ability.
Code golf doesn’t have to be formalized, it might just be something you do for fun. When you shave a couple of lines of code off something you’re working on, you golfed it.
As a bit of a relevant aside here, one language I haven’t seen much golf in is SVG. SVG is kinda perfect for it though, as it’s so mathematic, declarative, and referential. It was this Pen that got me thinking about it:
See the Pen Arrows by fuddl (@fuddl) on CodePen.
I tried my hand it a bit too:
See the Pen SVG Golf by Chris Coyier (@chriscoyier) on CodePen.
http://codegolf.stackexchange.com/
Thanks for this article!
JS code-golfing is my main hobby (with a group of guys on Twitter & Github).
You can find most of our work here:
https://gist.github.com/xem/206db44adbdd09bac424
And here are some other interesting links worth mentioning:
– http://www.reddit.com/r/tinycode/
– http://js1k.com
Cheers!
This is the coolest, nerdiest hobby. I’m super inspired by this.
I take that as a compliment :) cheers
On the topic of golfing SVG, the is my favourite horrible example of condensed SVG code, thanks to taking advantage of the HTML 5 parser to fill in all sorts of missing characters:
http://codegolf.stackexchange.com/a/26752
I love code golf! One of the most useful things I’ve made is taking a DOM selector array and converting it to a normal array you can loop over. I did it in 140Bytes.
http://codepen.io/fabean/pen/vEMPrW
Never heard of code-golfing before but the explanation makes perfect sense. Thanks for sharing.
And just in case you use
vi
/vim
and haven’t tried it, you can check out Vim GolfYou can compete live with other coders on CodeWars.com. It also has a scoring system and stuff, which is kind of neat.
vimgolf is a pretty popular ruby gem and worth a peek. http://www.vimgolf.com/
How about do nothing in C#?
I made a tiny aj(ax) lib after reading
github.com/aretecode/aj
That’s funny. I’ve always used the term “a hole in one” when you burst through some code and it works exactly as hoped on first refresh.
One thing I’m really missing in this article is a small section explaining that golfing is actually, almost always a really bad idea in production code.
The thing about real world projects is, there is more to them than writing code that gets the job done. In a real world project it’s also very important for code to be easy to read and understand. Code that’s easy to understand tends to contain less errors and is much easier to maintain.
Too much golfing almost always leads to unnecessarily obscure code that is difficult to wrap your head around. This increases the risk of bugs and makes maintenance much more difficult.
Totally agree Ruben! How many times have you had to deal with “boy scout” code where sure its written in one line but its basically non-readable?
Yes, code-golf is an anti-pattern.
Good code is clear, not esoteric.
Fizzbuzz anyone?
Is this considered golf? http://www.nanochess.org/chess3.html