views:

520

answers:

11

After coming across the code golf trivia around the site it is obvious people try to find ways to write code and algorithms as short as the possibly can in terms of characters, lines and total size, even if that means writing something like:

    //Code by: job
    //Topic: Code Golf - Collatz Conjecture
    n=input()
    while n>1:n=(n/2,n*3+1)[n%2];print n

So as a beginner I start to wonder whether size actually matters :D

It is obviously a very subjective question highly dependent on the actual code being used, but what is the rule of thumb in the real world.

In the case that size wont matter, how come then we don't focus more on performance rather than size?

+17  A: 

I hope this does not become a flame war. Good code has many attributes, including:

  1. Solving the use-case properly.
  2. Readability.
  3. Maintainability.
  4. Performance.
  5. Testability.
  6. Low memory signature.
  7. Good user interface.
  8. Reusability.

The brevity of code is not that important in 21st century programming. It used to be more important when memory was really scarce. Please see this question, including my answer, for books referencing the attributes above.

Yuval F
Thanks! i'll give those book a look regarding the topic
Carlucho
+1 That's right, brevity really isn't that important today. In fact, it often leads to some really ugly code (e.g. nested ternary operators).
Helper Method
I'd phrase t slightly differently: brevity is only useful when it helps with some of the other goals. For example brevity *can* enhance readability and maintainability. Excessive brevity, however can equally well destroy both.
Joachim Sauer
Brevity is paramount in situations where the code gets transmitted a lot over networks. Check the html of www.google.com! It's about bandwith, not memory.
Guge
@Guge - you are very right. I believe also programs for real-time devices will be as short as possible. I guess you should optimize for the resource that is the bottleneck and not worry too much about other resources.
Yuval F
@Yuval - yes, I touched upon it in my answer, but I don't know if anybody read it.
Guge
+5  A: 

Writing "code golf" solutions are often to do with showing how "clever" you are in getting the job done in the most succinct way even at the expense of readability. Quite often, however, more verbose code including, for example, memoization of function results, can be faster. Code size can matter for performance, smaller blocks of code can fit in the L1 CPU cache but this is an extreme case of optimization and a faster algorithm will most always be better. "Code Golf" code is not like production code - always write for clarity & readability of the solution rather than terseness if anyone, including yourself, ever intend to read that code again.

Trevor Tippins
+4  A: 

Whitespace has no effect on performance. So code like that is just silly (or perhaps the golf score was based on the character count?). The number of lines also has no effect, although the number of statements can have an effect. (exception: python, where whitespace is significant)

The effect is complex, however. It's not at all uncommon to discover that you have to add statements to a function in order to improve it's performance.

Still, without knowing anything else, bet that more statements is a larger object file, and a slower program. But more lines doesn't do anything other than make code more readable up to a point (after which adding more lines makes it less readable ;)

John Knoeller
Oh i see your point. So the important thing is statements. How come whitespace is significant in python?
Carlucho
Whitespace is significant in Python because the language designer decided it should be. I think it's intended to make using the language quicker and easier to write code in.
John Knoeller
+8  A: 

A lot of good answers already about what's important versus what's not. In real life, (almost) nobody writes code like code golf, wtih shortened identifiers, minimal whitespace, and the fewest possible statements.

That said, "more code" does correlate with more bugs and complexity, and "less code" tends to correlate with better readability and performance. So all other things being equal, it's useful to strive for shorter code, but only in the sense of "these simple 30 lines of code do the same as that 100 complex lines of code".

Brian
+4  A: 

I don't believe that Code Golf has any practical significance. In practice, readable code is what counts. Which in itself is a conflicting requirement: readable code should be concise, but still easy to understood.

However, I would like to answer your question yet differently. Usually, there are fast and simple algorithms. However, if the speed is top priority, things can get complex real fast (and the resulting code will be longer). I don't believe that simplicity equals speed.

J S
In some languages, it can show you sneaky and inventive ways of doing things, and every once in a while you may find something you like and (if well documented) makes your real code better. It's rare but it happens. If nothing else, code golf is a way to really get to know your language of choice.
Chris Lutz
+1  A: 

This is from "10 Commandments for Java Developers"

Keep in Mind - "Less is more" is not always better. - Code efficiency is a great thing, but > in many situations writing less lines of code does not improve the efficiency of that code.

This is (probably) true for all programming languages (though in assembly it could be different).

Helper Method
I didn't know there were 10 commandments for java... well one more religion cant hurt. can it?
Carlucho
It is not true for code that gets transmitted over a network a million times a day.
Guge
When I started programming in Java, I found this article on Javalobby. Since then it reminds me to comment my code, test it, not reinvent the wheel, etc.
Helper Method
+2  A: 

There are many aspects to performance. Performance can for example be measured by memory footprint, speed of execution, bandwith consumption, framerate, maintainability, supportability and so on. Performance usually means spending as little as possible of the most scarce resource.

When applied to networking, brevity IS performance. If your webserver serves a little javascript snippet on every page, it doesn't exactly hurt to keep the variable names short. Pull up www.google.com and view source!

Some times DRY is not helping performance. An example is that Microsoft has found that they don't want a to loop through an array unless it is bigger than 3 elements. String.Format has signatures for one, two and three arguments, and then for array.

There are many ways of trading one aspect for another. This is usually called caching.

You can for example trade memory footprint for speed of execution. For example by doing lookup instead of execution. It is just a matter of replacing () with [] in most popular languages. If you plan it so that the spaceship in your game can only go in a fixed number of directions, you can save on trigonometric function calls.

Or you can use a proxy server with a cache for looking up things over a network. DNS servers do this all the time.

Finally, if development team availability is the most scarce resource, clarity of code is the best bet for maintainability performance, even if it doesn't run quite as fast or is quite as interesting or "elegant" in code.

Guge
sorry but, what does DRY stand for?
Carlucho
Don't Repeat Yourself. It is considered foolish to copy and paste code around, for many reasons.
Guge
+2  A: 

Absolutely not. Code size and performance (however you measure it) are only very loosly connected. To make matter worse whats a neat trick on one chip/compiler/OS may very well be the worse thing you can do in another archictecture.

Its counter-intuitive but a clear well written simple as possible implmentation is often far more efficient than than a devious bag of tricks. Today's optimizing compilers like clear uncomplicated code just as much as humans and complex trickery can cause them to abandon thier best optimizing strategies.

James Anderson
+2  A: 
  1. Writing fewer lines of code tends to be better for a bunch of reasons. For example, the less code you have, the less chance for bugs. See for example Paul Graham's essay, "Succinctness is Power"
  2. Notwithstanding that, the level reached by Code Golf is usually far beyond what makes sense. In Code Golf, people are trying to write code that is as short as possible, even if they know that it's less readable.
  3. Efficiency is a much harder thing to decide. I'm guessing that less code is usually more efficient, but there are many cases where this isn't true.

So to answer the real question, why do we even have Code Golf competitions which aim at a low character count, if that's not a very important thing?

Two reasons:

Making code as short as possible means you have to be both clever, and know a language pretty well to find all kinds of tricks. This makes it a fun riddle.

Also, it's the easiest measure to use for a code competition. Efficiency, for example, is very hard to measure, especially using many different languages, especially since some solutions are more efficient in some cases, but less in others (big input vs small). Readability: that's a very personal thing, which often leads to heated debates.

In short, I don't think there is any way of doing Code Golf style competitions without using "shortness of code" as the criterion.

Edan Maor
+1 For mentioning that readability is mainly subjective (and afaik not measurable in any form).
Helper Method
+1  A: 

It makes a difference if you're talking about little academic-style algorithms or real software, which can be thousands of lines of code. I'm talking about the latter.

Here's an example where a reasonably well-written program was speeded up by a factor of 43x, and it's code size was reduced by 4x.

"Code golf" is just squeezing code, like cramming undergraduates into a phone booth. I'm talking about reducing code by rewriting it in a form that is declarative, like a domain-specific-language (DSL). Since it is declarative, it maps more directly onto its requirements, so it is not puffed up with code that exists only for implementation's sake. That link shows an example of doing that. This link shows a way of reducing size of UI code in a similar way.

Good performance is achieved by avoiding doing things that don't really have to be done. Of course, when you write code, you're not intentionally making it do unnecessary work, but if you do aggressive performance tuning as in that example, you'd be amazed at what you can remove.

Mike Dunlavey
+1  A: 

The point of code golf is to optimise for one thing (source length), at the potential expense of everything else (performance, comprehensibility, robustness). If you accidentally improve performance that's a fluke - if you could shave a character off by doubling the runtime, then you would.

You ask "how come then we don't focus more on performance rather than size", but the question is based on a false premise that programmers focus more on code size than on performance. They don't, "code golf" is a minority interest. It's challenging and fun, but it's not important. Look at the number of questions tagged "code-golf" against the number tagged "performance".

As other people point out, making code shorter often means making it simpler to understand, by removing duplication and opportunities for obscure errors. That's usually more important than running speed. But code golf is a completely different thing, where you remove whitespace, comments, descriptive names, etc. The purpose isn't to make the code more comprehensible.

Steve Jessop