In a bunch of recent "code golfs" (code golf offs?) on SO, best answers are always those that work and have the fewest characters. For compiled and interpreted languages, though, the latter criterion produces code that is short but hard to read (bad code, in the eyes of those who favor clarity over concision... it does produce cool code though.)

I'm thinking of some kind of criterion that is not "bytes of code." I'm thinking of something like "number of instructions," but I have no idea if that criterion would work. The idea is for the code to be as efficient as possible for the compiler/interpreter, but as humanly readable as possible. What criterion would work? How would you express it? How would you measure code (time benchmarks, perhaps)?

Lastly, are there any code golf-offs like this? How do they work?

share|improve this question
1  
Number of instructions won't work, because in some languages you could replace dozens of lines with one library call. –  DisgruntledGoat Jul 7 '09 at 11:36
1  
And different CPU's have different instructions, so people programming against a CISC CPU have an advantage. –  jalf Jul 7 '09 at 11:47
    
I think that OP didn't mean machine-level instructions when he said "number of instructions" but many answers and comments seems to be assuming that he did. –  Tahir Akhtar Jan 23 '10 at 5:46
add comment

migrated from stackoverflow.com Jun 25 '10 at 13:05

This question came from our site for professional and enthusiast programmers.

4 Answers

For a start you could count the number of tokens, so long identifiers, proper indentation and comments don't hurt the score.

share|improve this answer
    
What would be a token? I would think that defining variables should not have a "cost" because they get defined anyway, whether you name them or not. Thanks for your answer. –  Yar Jul 7 '09 at 13:22
    
a token is a pretty straightforward concept. It is commonly used in compilers. Every "unit" to be processed by the compiler/interpreter is a token. A variable name is a token. A type name is a token. A + is a token. Basically, anything you're allowed to insert whitespace between. (so 2+2 is 3 tokens, even though there's no space. You're allowed to insert spaces between them) –  jalf Jul 7 '09 at 13:35
    
Interesting stuff, thanks for that. I need to study compilers someday (along with a million other things I need to look at). –  Yar Jul 7 '09 at 18:44
add comment

For compiled and interpreted languages, though, the latter criterion produces code that is short but hard to read (bad code, in the eyes of those who favor clarity over concision... it does produce cool code though.)

Funnily enough, the goal with these things is to produce cool code, not readable code. So I don't see the problem. ;)

Moreover, since it is a test of how well you master the language, it should be your source code that is rated, not the compiled result.

Most of the readability problems can be solved simply by not counting whitespace and comments.

share|improve this answer
    
nice answer. I'd add variable names, or even using variables :) –  Yar Jul 7 '09 at 13:05
    
using variables? –  jalf Jul 7 '09 at 13:35
    
Variables instead of putting everything on one line... –  Yar Jul 7 '09 at 13:54
    
how would you compensate for that? Not everything can be written on one line, so some variables are necessary and should be counted. And more importantly, why should you compensate for this? Whitespace and comments are not part of the semantics for the program, so they can be ignored. The same goes for variable names or function names - the program behaves the same whether they're named x or ReallyLongAndSpecficiName, so the actual name doesn't matter. But where and if a variable is declared certainly makes a difference. –  jalf Jul 7 '09 at 14:05
    
You're probably right. My thought has always been that y.what().who().where() is the same as defining intermediate variables to make the code more readable. But I'm not sure that I can justify that. Hence I'm looking for a criterion, though the whitespace/comments thing would be a great start. –  Yar Jul 7 '09 at 14:37
add comment

The idea is for the code to be as efficient as possible for the compiler/interpreter

The problem is that managed languages like C# & Java JIT compile their IL/bytecode differently in different situations. Often the best way to get optimal code is to keep things simple and let the JIT compiler do the optimisations.

share|improve this answer
add comment

code produced depend upon the compiler, optimizations used, how would you cater for that?

share|improve this answer
    
right, that is in fact the question. my guess is that if you choose the platform (compiler and language) you might be able to do it. –  Yar Jul 7 '09 at 11:26
1  
but you'd get unpredictable results. There is no way to determine while writing the code, how many instructions it's going to turn into. Which means the entire contest becomes pure guesswork, not skill. Adding code to the source might make the compiled output shorter, and vice versa. It is a useless metric. You might as well try to determine who's most fluent in English by translating what they say to Spanish and see what looks best. –  jalf Jul 7 '09 at 13:37
    
good idea, jalf, on the english to spanish thing. might be interesting. Note that optimizing code IS guesswork many times. Doesn't mean there's no skill involved. –  Yar Jul 27 '09 at 0:18
add comment

You must log in to answer this question.

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