views:

48982

answers:

10

I was reading http://en.wikipedia.org/wiki/Douglas_McIlroy and found a quote that mentions "The real hero of programming is the one who writes negative code."

Can any one explain what that means?

+228  A: 

It means reducing lines of code, by removing redundancies or using more concise constructs.

See for example this famous anecdote from the original Apple Lisa developer team:

When the Lisa team was pushing to finalize their software in 1982, project managers started requiring programmers to submit weekly forms reporting on the number of lines of code they had written. Bill Atkinson thought that was silly. For the week in which he had rewritten QuickDraw’s region calculation routines to be six times faster and 2000 lines shorter, he put "-2000" on the form. After a few more weeks the managers stopped asking him to fill out the form, and he gladly complied.

Thilo
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take - [Antoine de Saint-Exupéry](http://en.wikipedia.org/wiki/Antoine_de_Saint-Exup%C3%A9ry)
systempuntoout
Is #LOC a good measure of code quality? I could 'Minify' any C or C++ code and reduce the line count significantly but it'd be a nightmare to maintain.
JBRWilkinson
@JBRWilkinson: No, there are no absolute metrics for code quality. See Ugly American's answer.
Thilo
@systempuntout - and then there was Einsten's "(A scientific theory) should be as simple as possible, but no simpler"
Jonathan Day
Nothing runs faster, or is more reliable, or requires less maintenance, than code that isn't there. "When in doubt, dike it out!"
TMN
@systempuntoout - Either you've played Civilisation 4, or you're good at quotes.
Bevin
@JBRWilkinson: I'd say that there is a "sweet spot" regarding code brevity. Generally, shorter is better, but there comes a point when code can grow too terse and not be easy to decipher to another programmer.
Gordon
Today I did my first real "negative coding". Thanks.
Trufa
+16  A: 

Its tongue-in-cheek. If it costs you $N per average coded line, then coding "negative lines" is surely a winner.

What this means as practical advice, is that small codes that accomplish the job, are much better than big codes that do the same thing, all other things being equal.

Ira Baxter
I see where you are coming from, but concise, easy-to-understand small-footprint code is seldom achieved in one go. It is usually write it so that it works(many lines), optimize for speed(a little fewer line) and optimize for maintenance/readability(fewer lines still). The real cost with the long return of investment is the second and third step, thus they are often skipped entirely. It is like "there is cheap, fast and good - you get to choose two".
RickiG
Actually, IME, optimizing for maintenence/readibility can actually *increase* LOC, since rewriting code to make it more self-documenting tends to also make it more verbose.
Visage
@Visage: "... all other things being equal".
Ira Baxter
@Ira: the point is, I think, that all other things can't be equal between concise code and verbose code.
Tomas Narros
The reason the average line of code costs $N is because you first spend your time writing `X` lines. Then, over several iterations, reducing the final product by `Y` lines. So, the `(X-Y)` remaining lines seem to be very costly because the carnage of refactoring has cut away all the cruft.
Chris
+51  A: 

There's a Bill Gates quote along the lines of measuring programmer productivity by lines of code is like measuring aircraft by weight.

I'd like to add that the LOC metric has encouraged the use of overly long-winded languages and deliberate reinventing the wheel to meet quota.

Ugly American
Yeah, that is the problem with any kind of metric. As soon as you use them to judge people's performance, they will start gaming the numbers.
Thilo
Has anyone actually ever used LOC as performance metric? I've only seen it used for things like "how bug of a project are we talking about here?"
Michael Borgwardt
@Michael: yes. Unfortunately, yes.
Michael Petrotta
are we talking about same Bill G. who has a company that by that metaphor produces 10000 GTON jets? :)
Daniel Mošmondor
@Daniel: He doesn't work there anymore. Maybe that's why...
Thilo
A programmer who wrote code for the Space Shuttle's onboard computers told me that he had to account for the weight of the software! The software was real (money was paid for it); it was on the shuttle; the weight of everything loaded on the shuttle must be accounted for. First example of measuring programmer productivity by weight of code. (Zero wasn't allowed, so he specified 0.00001 gram and all was satisfactory.)
Mark Lutton
@Michael: LOC is a popular performance metric for students when boasting to each other.
Dennis
@Ugly American, is something missing from that Bill Gates quote? It doesn't make sense to me. ie Measuring aircraft *what* by weight?
Ash
@Mark: I agree it's funny, but it's not entirely irrational. If the rogram is big enough, at some point you need more memory and/or a larger hard drive. These are physical objects that have weight.
Jay
@Ash “Measuring programming progress by lines of code is like measuring aircraft building progress by weight.”
Pete Kirkham
@Jay: Ten years before this, programs were stored on punched cards. When you use punch cards, software has negative weight, because the cards weigh less after being punched!
Mark Lutton
@Lutton: Punch cards are coming back. Who needs these new-fangled USB drive things?
Jay
+10  A: 

Writing the same program in less code is a goal for everyone.

If a program took 200 LOC to code, and I write it in 150, i wrote -50 LOC. So I wrote negative code.

LucaB
Also, writing less LOC means you can make less errors and spot them easily-
LucaB
@LucaB: Not true for Haskell and other languages that can be compressed to random noise. :)
Marcus Lindblom
Sure, my point was not "compressing code", but writing efficient algorithms that do thins in less LOC :) +1 for you comment.
LucaB
+38  A: 

To quote another great computer scientist, Edsger W. Dijkstra:

My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger.

From EWD 1036 (On the cruelty of really teaching computing science).

schot
+20  A: 

One of my most productive days was throwing away 1000 lines of code.
- Ken Thompson

wooptoo
+6  A: 

I came upon this question very late and it already has excellent answers, anyway I thought I´d chip in.

Gabriel García Márquez once wrote:

"I'm writing you a long letter because I´m in a hurry"

(It sounds much better in Spanish)

Efficiency is one of the hardest things to achieve, Frederick Taylor suggested the least possible moves, and though he was talking about industry, it holds true for various matters of life including the logic behind programming.

Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take. - Le Petit Prince

Trufa
Well said. There is a similar famous quote from [Blaise Pascal](http://en.wikiquote.org/wiki/Blaise_Pascal) (often mistakenly attributed to Mark Twain): *"I would have written a shorter letter, but I did not have the time"*.
e.James
Yes I´m not sure who actually said it, Anybody said everything in the quotes site, they just make it up, but the quote is great anyhow!
Trufa
Well said. WILL PLAGIARIZE A++++++ :)
maayank
LOL go crazy with it!
Trufa
Corrected the translation for you :)
RCIX
@RCIX Thank you!!
Trufa
A: 

Programming is something like if you love it its like your girl friend, else its going to be your wife.

Vinothbabu
My quote has brought me negative vote... heheh
Vinothbabu
But added to your comment's score, it currently comes out to `1`.
Noctis Skytower
i think my comment and answer is fighting for points on who is leading... heheh
Vinothbabu
+5  A: 

I once worked for a company that announced they were going to start rating programmers by lines of code produced per week. My response was, ALL right! I can score very high by this measure!

For example, instead of being lazy and writing

x=x+3;

I will write:

x=x+1;
x=x+1;
x=x+1;

Loops? Silly idea! If a task needs to be done 20 times in a row, write the code 20 times in a row.

Subroutines? Nah. Just rewrite the code each place you need it.

Etc.

Sadly, management dropped the idea.

Jay
Hilarious solution!
Noctis Skytower
+8  A: 

When I was in high school -- and yes, we had computers back in the 70s, though we had to make them out of animal skins using stone knives -- one of the math teachers ran a programming contest. The rules were that the winning program would be the one that produced the correct output, and that had the smallest product of lines of code times run time. That is, if your program took, say 100 lines of code and ran for 5 seconds, your score was 500. If someone else wrote 90 lines of code and ran for 6 seconds, his score was 540. Low score wins, like golf.

It struck me as a brilliant scoring system, rewarding both conciseness and performance.

But the entry that technically met the winning criteria was disqualified. The problem was to print a list of all prime numbers less than 100. The disqualified entry went something like this (most of the students were using BASIC back then):

100 print "2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,"
110 print "67, 71, 73, 79, 83, 87, 89, 91, 97"

The student who wrote that entry pointed out that not only was it short and very efficient, but the algorithm should be obvious to anyone with even a minimal knowledge of programming, making the program highly maintainable.

Jay
That's awesome.
Chris
More proof that counting lines of code is a very gameable metric :-)
paulecoyote
That BASIC program is brilliant! It is rather upsetting that the teacher disqualified the program. After all, lookup tables (which the program is somewhat similar to) can definitely be found in real-world programming.
Noctis Skytower