351

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

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

What does that mean?

2

6 Answers 6

514

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.

2
  • 270
    Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take - Antoine de Saint-Exupéry Sep 27, 2010 at 7:06
  • @systempuntoout (or more precisely...) Perfection is achieved, not only when there is nothing more to add, but also when there is nothing left to take
    – Zippo
    Jul 18, 2012 at 11:17
137

There's a Bill Gates quote along the lines of measuring programmer productivity by lines of code is like measuring aircraft building progress 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.

2
  • 31
    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
    Sep 27, 2010 at 5:59
  • 1
    A more serious analogy would be that measuring software productivity by lines of code is like measuring progress on an auto repair job by counting the number of grease rags used. This could be accurate to an extent: the more work the mechanic does, the more grease rags he'll use. But it counts mistakes and wasted effort as much as effort that actually leads to a goal, and a big mistake -- like accidentally breaking an oil line and having to clean up a huge mess -- gets counted as major progress when it's really the opposite.
    – Jay
    Dec 31, 2013 at 22:02
118

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.

8
  • 11
    More proof that counting lines of code is a very gameable metric :-)
    – paulecoyote
    Oct 20, 2010 at 16:08
  • 47
    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. Oct 21, 2010 at 2:37
  • 7
    A wise teacher may have accepted this BASIC program and used it to highlight the importance of getting an SRS right. Reminds me of a baseball coach who got so frustrated with his team that to show them how to play, he took the bat, got three strikes in a row and not to be outdone, he shouted to his team "See! That's how you bas***** are playing. Now take the bat and play properly!". Also reminds me of the person who wrote "creation saw the creator and blushed" and won the essay contest on 'wine'.
    – Nav
    Aug 26, 2011 at 8:13
  • 3
    @Nav: Reminds me of a similar story that begins the same way. Then the coach throws a ball in the air, swings and misses. He throws it in the air again, swings and misses. He throws it in the air a third time, swings and misses. Then he says to the team, "See, THAT'S how you should be pitching!" (I have no idea what this story might have to do with software development.)
    – Jay
    Aug 26, 2011 at 20:20
  • 14
    I'd be pretty upset if I were disqualified for this. A deterministic problem deserves a deterministic solution, right? When I write a 'Hello World' app I don't code it to check whether if I'm spelling 'Hello' correctly. Sep 28, 2011 at 0:44
34

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.

5
  • 2
    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
    Sep 27, 2010 at 10:49
  • 2
    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
    Sep 27, 2010 at 10:56
  • 1
    @Visage: "... all other things being equal".
    – Ira Baxter
    Sep 27, 2010 at 14:10
  • the point is, I think, that all other things can't be equal between concise code and verbose code. Sep 27, 2010 at 16:20
  • 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
    Oct 20, 2010 at 18:54
27

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.

3
  • 3
    Also, writing less LOC means you can make less errors and spot them easily-
    – LucaB
    Sep 27, 2010 at 12:52
  • 3
    Not true for Haskell and other languages that can be compressed to random noise. :)
    – Macke
    Sep 28, 2010 at 16:57
  • 1
    Sure, my point was not "compressing code", but writing efficient algorithms that do thins in less LOC :) +1 for you comment.
    – LucaB
    Sep 29, 2010 at 10:35
9

Thilo's answer is probably most accurate historically, but the "negative code" metaphor can also include performance and memory use - rewarding efforts to defer execution or allocation of something until it is actually needed.

This "procrastination pays" mentality produced such tongue-in-cheek axioms such as "Doing nothing is always faster than doing something", "The fastest code is the code that never executes", and "If you can put it off long enough, you might not ever have to do it" (referring to deferral of expensive operations until actually required)

One technique to realizing negative code is to challenge initial assumptions and definitions of the problem. If you can redefine the problem / input domain such that "sticky issue #3" is categorically impossible, then you don't have to spend time or code dealing with sticky issue #3. You've eliminated code by optimizing the design.