Sep 28, 2010
Last night I was checking certain social network at home, you know, the usual bad habit, when suddenly I found a phrase that caught my attention. What was that uncertain concept that attracted my senses? Well, it was "negative code". "What is it?" I thought.
As astonished as I was at that time, I took a few seconds to ask my old fellow (G...) "what is negative code?". Of course, as wise as always, it showed me several conversations about that interesting topic. I found many concepts thrown in the air, but nothing that I could define the concept until I found this discussion at stackoverflow. A user named Thilo mentioned a concept that barely filled my expectations, and here is it:
"...reducing lines of code, by removing redundancies or using more concise constructs".
Well, that means something, doesn’t it? Well, yes, but at the same time I felt something was missing. I thought, "Ok, everybody knows that if you code to write less code, but obtain a correct result, that code becomes in saving code lines! But! That isn't negative code!".
A little shocked and confused I continued reading below on the discussion. Thilo also quoted an old anecdote he found:
"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."
Sometimes I think I'm just slow, because I didn't understand what this was trying to say at the beginning. I ask myself:
"Why managers stopped to bother him after he wrote -2000 LOC? I know, he improved the system throwing away the fat and... oh, I see!".
Yes, that was the answer I was looking for. But, what does it mean? Well, as I understood the re-factory concept fits perfectly to negative code, and let me explain why.
Sometimes, we found many new problems to solve in our way and we keep coding and coding until we solve them. There will be times where our code is optimal, but that's not the case all of the time. Sometimes we find old and reviewed problems we already solved before, and we force the same solution to fit those new questions. As time passes these code lines become obsolete. The old code is worth nothing now. What should we do?
Well, there are two solutions.
- We keep updating our code to fit new requirements; we make it more robust, more stable and more compatible with old code.
- Or we just simply throw away that old code and we remake it. Writing lean code, "same result less code".
All those LOC we "saved", or that we didn't write, that is negative code.
We usually fall in a hole when someone says "do not re-invent the wheel, re-use". That, in a way, is correct. Why should you create a new tool if there are many out there? The answer is simple: sometimes, it's more than you need at the moment. Period.
Remember, the less code you use, the less code you maintain!
I encourage you to find better ways to solve the same problem. Throw away those awful lines you wanted to forget and improve them.