Programming Topics + CVu Journal Vol 30, #5 - November 2018
Browse in : All > Topics > Programming
All > Journals > CVu > 305
Any of these categories - All of these categories

Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.

Title: Improve Code by Removing It

Author: Bob Schmidt

Date: 05 November 2018 17:40:00 +00:00 or Mon, 05 November 2018 17:40:00 +00:00

Summary: Pete Goodliffe takes a scalpel to unnecessary code.

Body: 

We ascribe beauty to that which is simple;which has no superfluous parts;which exactly answers its end...
~ Ralph Waldo Emerson

Less is more. It’s a trite maxim, but sometimes it really is true.

Some of the most exciting improvements I remember making to code involved removing vast chunks of it. Let me tell you, it’s a good feeling.

A war story: As an Agile software development team, we’d been following the hallowed eXtreme Programming tenets, including YAGNI. That is, You Aren’t Gonna Need It: a caution to not write unnecessary code – even code you think is going to be needed in future versions. Don’t write it now if you don’t need it now. Wait until you do have a genuine need.

This sounds like eminently sensible advice. And we’d all bought in to it.

But human nature being what it is, we fell short in a few places. At one point, I observed that the product was taking too long to execute certain tasks – simple tasks that should have been near instantaneous. This was because they had been over-implemented, festooned with extra bells and whistles that were not required, and littered with hooks for later extension. None of these things were being used, but at the time they each had seemed sensible additions.

So I simplified the code, improved the product performance, and reduced the level of global code entropy by simply removing all of the offending ‘features’ from the codebase. Helpfully, my unit tests told me that I hadn’t broken anything else during the operation. A simple and thoroughly satisfying experience.

You can improve a system by adding new code. You can also improve a system by removing code.

Code indulgence

So why did all that unnecessary code get written? Why did one programmer feel the need to write extra code, and how did it get past review or the pairing process?

It was almost certainly the programmers’ indulging their own personal vices. Something like:

Now, we had a well-understood lean development process, very good developers, and procedural checks in place to avoid this kind of thing.

And unnecessary extra code still snuck in.

That’s quite a surprise, isn’t it?

It’s not bad, it’s inevitable

Even if you can avoid adding unnecessary new features, dead pieces of code will still spring up naturally during your software development. Don’t be embarrassed about it! They come from a number of unavoidable accidental sources, including:

So what?

Does this really matter? Surely we should just accept that dead code is inevitable, and not worry about it too much if the project still works. What’s the cost of unnecessary code?

Dead code won’t kill you, but it will make your life harder than it needs to be. It gets in the way and slows you down.

Pete Goodliffe Pete is a programmer who never stays at the same place in the software food chain. He has a passion for curry and doesn’t wear shoes.

Notes: 

More fields may be available via dynamicdata ..