share
Stack OverflowWhat is negative code?
[+150] [10] Anonymous
[2010-09-27 02:14:08]
[ terminology code-metrics loc ]
[ http://stackoverflow.com/questions/3800707] [DELETED]

I was reading the Wikipedia article on Douglas McIlroy [1] and found a quote that mentions

"The real hero of programming is the one who writes negative code."

What does that mean?

(16) should be community wiki - SilentGhost
(37) No, it shouldn't -- this phrase has a specific and non-controversial meaning. - Cory Petosky
(4) Explaining "Negative code" isn't subjective and argumentative. Whether "Negative code" is good or bad may be, but that wasn't what Anonymous asked. - Andrew Grimm
(1) Indeed. Apparently it doesn't matter if 10 or 100 or even 1000 people like the question. When 5 people decide it's subjective, then it definitly must be subjective. - Rene Saarsoo
What makes a question eligible for being a Community Wiki? (Link to answer is fine with me) - thenonhacker
[+247] [2010-09-27 02:15:04] Thilo

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

See for example this famous anecdote [1] 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.

[1] http://www.computerhistory.org/highlights/macpaint/

(86) Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take - Antoine de Saint-Exupéry - systempuntoout
(1) 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
(4) @systempuntout - and then there was Einsten's "(A scientific theory) should be as simple as possible, but no simpler" - Jonathan Day
(8) Nothing runs faster, or is more reliable, or requires less maintenance, than code that isn't there. "When in doubt, dike it out!" - TMN
(2) @systempuntoout - Either you've played Civilisation 4, or you're good at quotes. - Bevin
(1) @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. - GordonM
Today I did my first real "negative coding". Thanks. - Trufa
1
[+54] [2010-09-27 04:57:49] Ugly American

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.


(12) 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
(3) 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
(2) @Michael: yes. Unfortunately, yes. - Michael Petrotta
(2) are we talking about same Bill G. who has a company that by that metaphor produces 10000 GTON jets? :) - Daniel Mošmondor
(2) @Daniel: He doesn't work there anymore. Maybe that's why... - Thilo
(11) 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
(2) @Michael: LOC is a popular performance metric for students when boasting to each other. - Dennis
(1) @Ugly American, is something missing from that Bill Gates quote? It doesn't make sense to me. ie Measuring aircraft what by weight? - Ashley Henderson
@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
2
[+40] [2010-09-27 09:34:10] schot

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 [1] (On the cruelty of really teaching computing science).

[1] http://userweb.cs.utexas.edu/users/EWD/

3
[+20] [2010-09-27 12:06:58] wooptoo

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


4
[+17] [2010-09-27 02:17:02] Ira Baxter

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

This means, as practical advice, that small code that accomplishes the job, is much better than big code that do the same thing, all other things being equal.


(1) 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
(1) @Visage: "... all other things being equal". - Ira Baxter
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
5
[+13] [2010-10-19 21:02:39] Jay

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.


(3) That's awesome. - Chris
(1) More proof that counting lines of code is a very gameable metric :-) - paulecoyote
(3) 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
6
[+11] [2010-09-27 07:44:02] lbedogni

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.


(1) Also, writing less LOC means you can make less errors and spot them easily- - lbedogni
(2) Not true for Haskell and other languages that can be compressed to random noise. :) - Macke
Sure, my point was not "compressing code", but writing efficient algorithms that do thins in less LOC :) +1 for you comment. - lbedogni
7
[+9] [2010-10-16 21:33:47] Trufa

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


(3) Well said. There is a similar famous quote from 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
(1) Well said. WILL PLAGIARIZE A++++++ :) - maayank
LOL go crazy with it! - Trufa
Corrected the translation for you :) - RCIX
@RCIX Thank you!! - Trufa
8
[+6] [2010-10-19 20:53:14] Jay

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.


Hilarious solution! - Noctis Skytower
9
[-4] [2010-10-16 21:51:22] Vinothbabu

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


(6) My quote has brought me negative vote... heheh - Vinothbabu
(1) 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
10