Monday, 26 January 2009

[Code] Improving code by removing it

Less is more. It's a quite trite little maxim, but sometimes it is really true.

One of the improvements I've made to our codebase over the last few weeks is removing chunks of it.

We'd written the software following XP tenets, including YAGNI (that is, You Aren't Going to Need It). Inevitably, in a few places we fell short.

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 were over-implemented with extra bells and whistles that were not required, but that seemed like a good idea at the time.

So I've simplified the code, improved the product performance, and reduced the level of global code entropy by simply removing the offending features from the codebase. Helpfully, my unit tests tell me that I haven't broken anything else during the operation.

A simple and thoroughly satisfying experience.

So why did the unnecessary code end up there in the first place? Why did one developer feel the need to write extra code, and how did it get past review (or the pairing process?) Almost certainly something like:
  • It was a fun bit of extra stuff, and the developer wanted to write it. (Hint: Write code because it adds value, not because it amuses you)
  • Someone thought that it might be needed in the future, so it was best to code it now. (Hint: That isn't YAGNI. If you don't need it right now, don't write it right now)
  • It didn't appear to be that big an "extra", so it was easier to implement it rather than go back to the customer to see whether it was really required. (Hint: It always takes longer to write, and to maintain extra code. And the customer is actually quite approachable. A small extra bit of code snowballs over time to a large piece of work that needs maintenance.)
  • The developer invented extra requirements that were not documented in the story that justified the extra feature. The requirement was actually bogus. (Hint: Developers do not set requirements; the customer does.)
What are you working on right now? It is all needed?

No comments: