tags:

views:

738

answers:

11

I have kept an eye on quite a few of the code golf questions on stack overflow, now i am a programmer constantly learning and developing (As i think is natural) but it bugs me to create code in as few code strokes as possible, is it really useful? it's almost un-readable and un-manageable, other than maybe the challenge or "fun" is code golf really useful?

I can see the element of the "logic" solving being useful to keep our minds working and challenged, but to get it in as few characters as possible, is there a purpose more than "fun" that i'm missing? does it make me a better programmer?

+11  A: 

I'd say it does. There are differences between finding a solution and finding

  • The quickest solution
  • The solution that uses the least resources
  • The solution that uses least characters
  • The solution that will work under any arbitrary circumstances you can think of

Having to find different ways to solve a problem makes you think, and it will probably mean you discover techniques that you'd never otherwise have thought of if the moment you found a solution, you stopped.

DanSingerman
As far as i've seen code golf has been about: "The solution that uses least characters" - that's what i understood it as and it doesn't seem to make much sense in improvement of real programming skills if that is the case?
Shadi Almosri
+2  A: 

It can be useful to learn new unorthodox techniques. Other than that, it's just for fun. It might actually make you a worse programmer.

recursive
Why would it make you worse?
Shadi Almosri
Because if you use the habits you learn in golf in real code, you will write unmaintainable code.
recursive
+6  A: 

I do a bit of it, so hopefully I'm not just wasting time. :)

Apart from being fun, I think it does

  • give you a chance to 'practice keeping your mind challenged' (though many of us perhaps already have enough of that with normal jobs as developers)
  • force you to consider multiple data structures/algorithms on an otherwise trivial problem
  • provide some discipline about testing and refactoring

To expand upon bullet #2, I think it's often the case that in real life most (not all) day-to-day work does not require you to think too hard about data representation. In code golf, the 'shortness' criterion provides an (arbitrary/weird) constraint that forces you to consider more data representations than you would otherwise consider for a trivial program. Sure, a 2-D array works, but what about a 1-D array, or implicitly encoding the data in a giant lookup string, or ... I think these code-golf experiences help keep you 'awake' to the moments when data-representation choices really matter in 'real life'.

As to bullet #3, my usual MO is: (1) solve the problem normally, (2) write some unit tests and save everything, (3) start refactoring (typically in the opposite direction of normal refactoring) to make the code shorter. Occasionally I mess up and the unit tests catch me. This just help instill a unit testing discipline along with willingness to make changes to code without changing behavior.

Those are my thoughts, anyway. Maybe I'm just making up justifications to have fun :)

Brian
Solving the problem normally means dividing things up into multiple functions when needed, ja?
omouse
@omouse: Right. I start out with a bunch of named functions, and then rename to reduce the names to one letter, and then start doing refactorings to inline every function that only has a single call site (which eliminates many functions)...
Brian
+6  A: 

Code golf is a puzzle. Solving puzzles makes you beter at solving similar puzzles, but that's not what I'd call useful. As for making you a better programmer, does writing haiku make you a better novelist? Probably a bit on a superficial level, but not where it matters.

You're bound to learn more about programming by just solving the problems, regardless of how many keystrokes it takes you to do it. Might as well take out the middle man then, and solve useful programming problems, not merely abstract code golf problems.

Joren
With all due respect, I completely disagree. Creatively solving puzzles makes you better *at solving puzzles*, full stop. Moreover, like a novelist writing haiku, it exercises different but relevant thought process from the "proper work", which can provide greater insight for future "proper work". Both examples are quite like a musician practicing modes, which alone don't produce much but in the context of a developing talent provide greater insight into utilizing available instruments (or tools).
eyelidlessness
+6  A: 

The point of code-golf is to learn as much of the crusty corners of a programming language as possible. This may introduce you to new concepts, such as using a map function instead of a for-loop (iterator vs. general loop).

The nice thing about code-golf is that it doesn't optimize for memory or time, you're forced to optimize for keystrokes. This makes it a SOLVABLE problem. I would say it's possible to solve almost any code-golf problem if you know the entire programming language.

omouse
+18  A: 

I think practice is the most underrated part of being a programmer.

Code golfing does not make you a good programmer, but not practicing makes you a bad one. I think the most important things I have done as a programmer have had very little short term benefit.

I have spent more time writing algorithmic challenges than code golf challenges, but I still think the principle is the same. If you learn just to learn, you will be better off in the long run.

Jørgen Fogh
omouse
Just like to add that on top of learning the algorithm, it makes you explore language capabilities....codegolf helped me understand lambdas and list comprehensions in Python, because those features helped shorten the code. :)
Snazzer
See i think the algorithmic challenges are great, but not the minimal keystroke issue that code golf provides, isn't it simply the fact that getting the challenge right in a reletivly tidy code sample good enough?
Shadi Almosri
@Shadi: Of course it's "good enough". Like real golf, the challenge is *you* verses the *problem* - it's not *you* vs the other challengers. It's silly to try to compare PHP vs Perl in character count - Perl will win every time. But how few "statements" can you make PHP (or whatever your favorite language is) solve the given problem. The minimal keystroke IS part of the problem. A different problem would be one that is "lowest execution time" or "least memory" or "fewest cpu cycles". Developers are great at redefining the problem. That's not an option in code golf.
ChronoFish
@ChronoFish: but the thing is almost every code-golf question I've seen here has been "you vs the other challengers" AND it has been about character count AND Perl wins almost every time! no one seems to be doing it in the least statements but keystrokes unless I've missed alot of them!
Shadi Almosri
+2  A: 

Code golf is the opposite of how I write code in real life.

It's definitely a brain-expanding practice. When golfing with Perl I learned some crazy bits about which I had no prior idea.

However, a lot of the coding "tricks" required by golf are exactly that -- clever practices that I would almost never use in production code. When I need to fix bugs in the codebase I expect the code to be readable, not clever.

a paid nerd
i prefer it to be readable AND clever :)
Shadi Almosri
Shadi Almosri, I think the difference is that where "tricks" in code golf ought to be clever in terms of doing more with less code, the cleverness that's appropriate in "work" code is more in terms of maximizing clarity while minimizing resource use. The two really are quite opposite. But with that said, cleverness in one likely promotes cleverness in the other... simply because it promotes thinking deeper into problems.
eyelidlessness
I'm a firm believer in http://en.wikipedia.org/wiki/KISS_principle unless your golfing of course. :)
gradbot
+2  A: 

Code golf is primarily just for fun. Its a challenge for programmers to have fun with an obscure goal with other programmers. You will almost never be asked at a job to write code with the least characters possible. BUT its fun, can be good practice for programming, and helps you exercise your brain. SO needs some fun once in a while. :D

CrazyJugglerDrummer
+9  A: 

Yes, but mostly not because of the golf part


I'm fairly sure it does, particularly if you are new to a language. In that case what you need more than anything else is practice.

Playing "outdoor" golf makes most people into better golfers, but not because they are keeping score, just because they are practicing.

Code-golf has many indirect benefits.

  • it supplies motivation
  • it supplies evaluation (you win or you lose, and you see why)
  • for some languages it really makes you search the API for useful stuff
  • it supplies motivation to read other people's code carefully
  • it generates mostly-helpful examples of good algorithms and techniques
  • it forces you to pick "good" algorithms (ok, sometimes they are horridly inefficient, but I've never seen a clumsy approach do well in code-golf.)
DigitalRoss
+3  A: 

I would disagree with a lot of other answers here. I contend that code golf does make you a better programmer. Primarily because it's fun. Think of it from this angle - coding in a normal commercial environment let's face it can be boring as all hell.

If you're inspired by challenge just for challenge's sake, more power to you. I for one gain inspiration from fun challenges far more than business challenges that have me scratching my head for hours before I solve them. That said, I do get a lot of satisfaction from commercial programming though.

If you get inspiration from code golf, then that will make you a better programmer because you will be inspired to learn new techniques that will ultimately make you a better programmer - not because code golf in itself makes you a better programmer.

So I contend that yes - code golf will make you a better programmer - albeit indirectly, but only if it helps enhance your enjoyment of programming.

BenAlabaster
+3  A: 

At the very least it makes you think about all of the parsing rules, if only to remove a space that doesn't need to be there.

The better you know the parsing rules of a language, the better you will be at writing in that language.

Brad Gilbert
How does that follow? At most, this helps you avoid writing code that doesn't compile.
Oli Charlesworth