Essay 5Throw Away Your Old Code

Recently, one of my colleagues, Mustafa, mentioned that I was “doing it again.” I was code hoarding: commenting out code that I wasn’t really planning on ever using again. I just didn’t have the heart to delete it right then, even though we version control all of our source code (and you absolutely should be doing the same). Since I could always get that old code back anyway, there was no reason to be commenting out code when I could just delete it.

Code hoarding is one of many habits that seems right on the surface. It’s a carryover from other staple engineering principles that aren’t really relevant to programming. If we were building a car, we would probably save all our scrap metal because we could reuse it later. It would be stupid just to dump it. Labor and material are really critical in traditional engineering. In the physical world, it’s much easier to work with something that’s almost right than to rip it apart and start over.

In programming, we tend to put too much weight on those elements. Is our work really labor intensive? Not really. Typing isn’t that strenuous. What about material? Last time I checked, we don’t have a shortage of keystrokes.

At the same time, code hoarding ends up actually creating more obstacles. The reality is, most of the time, I never uncomment code that I commented out days or weeks ago. Instead, the blobs of monochromatic gibberish get dispersed around the actual code I’m writing at the moment. It’s annoying to look at. Every time I work around the old code, it’s distracting me from what matters right now.

Even if I do decide to re-implement something I wrote a while back, the code I commented out usually doesn’t fit properly anyway. Maybe I’ve moved that piece of logic somewhere else. Objects or methods I reference in the old code may have changed. Trying to resuscitate old code means I spend more time jerry-rigging things than writing it correctly and cleanly again. The code I wrote last week was written by a version of me that only knew what last week’s application looked like.

By deleting code instead of hoarding it in comments, we keep the codebase lean. What’s on the page should reflect exactly how the application works right now. Nothing more, nothing less. By getting rid of old code now, there are no extraneous bits of gibberish to leap over when we’re in the middle of programming. We don’t have to wonder, later, whether that huge glob of commented-out but seemingly important code is really all that important still.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.77.71