Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 157 Stack Exchange communities.

What is meta?
Here's how it works:
  1. Any Stack Exchange user can ask a question
  2. The community provides support, votes on ideas, and reports bugs
  3. Your voice helps shape the way Stack Exchange operates

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?


migration rejected from Aug 28 '15 at 18:13

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as off-topic by Wrzlprmft, S.L. Barth, Sunshine, Billy Mailman, James Aug 28 '15 at 18:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question pertains only to a specific site in the Stack Exchange Network. Questions on Meta Stack Exchange should pertain to our network or software that drives it as a whole, within the guidelines defined in the help center. You should ask this question on the meta site where your concern originated." – Wrzlprmft, S.L. Barth, Sunshine, Billy Mailman, James
If this question can be reworded to fit the rules in the help center, please edit the question.

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
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

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

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. – Dan Rosenstark 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). – Dan Rosenstark Jul 7 '09 at 18:44

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.

nice answer. I'd add variable names, or even using variables :) – Dan Rosenstark Jul 7 '09 at 13:05
using variables? – jalf Jul 7 '09 at 13:35
Variables instead of putting everything on one line... – Dan Rosenstark 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. – Dan Rosenstark Jul 7 '09 at 14:37

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.


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

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. – Dan Rosenstark Jul 7 '09 at 11:26
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. – Dan Rosenstark Jul 27 '09 at 0:18

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