Thursday, June 25, 2009

K.I.S.S. and the 5 rules of laziness

This article took me a lot longer than I had expected.

I could try to bring up the usual excuses like "my apartment exploded", "my dog (that I don't have) ate my laptop" or "my boss locked me up in my office until my Trac issues were all closed".

Nope, the problem is that simplicity is the frickin' most complicated thing to explain.

I tried to keep it simple and summarize things that I feel are important aspects of simplicity. The resulting article is not even close to what I had intended and I think I have to live with not being satisfied with the result. But at least i lived up to my promise to deliver this.

How often did somebody use K.I.S.S. ("Keep It Simple, Stupid") to tell you how to improve your code?

After using K.I.S.S. on a lot of fellow coders over the years I thought it should be a quite simple task to explain.
Let's cut this short: I was wrong.
There is so much more to this issue than you think in the first moment. Explaining K.I.S.S. would easily fill a ton of books without really getting to the point.
After a lot of discarded posts I remembered a quote from a friend:
"The most important attribute of a good developer is laziness."
Good coding skills in combination with laziness leads to effective simplicity.

So here they are:

My 5 Rules Of Laziness.

  1. Be lazy: Look, the world is full of developers, you are most certainly not the first one to run into that particular problem. Search for an open source project or some sample code that fixes your problem at hand (Never ever forget to give credit to the source of your solution!).
  2. If 1. failed remember being lazy: Ok, so your google-fu is weak. Most likely you will have several possible ways to get the job done. Always pick the easier route.
  3. If you are working on 2., be more lazy: This is a lesson learned from doing projects in an agile way:
    Solve your problems at hand (the ones defined for the current iteration), not the ones that MIGHT occur. Don't forget about the MIGHTs, put them as issues in your tracking system. Discuss them BRIEFLY with your project lead to see if you can replace the MIGHT with a WILL see if it is urgent enough to put in the current iteration. If not, leave it.
  4. If you are impressed by the beauty and complexity of your code:
    - Print out a copy of it.
    - Put it on the wall next to the picture of Picard.
    - Start refactoring until you feel like your cat could understand the code.
  5. Document it!

1 is quite self explanatory. If you can have somebody else do your work: Have him do it.

2 is a little more interesting. It deals with the arch enemy of of software developers:

Complexity

It's a mythical beast more vicious than Grendel and sneakier than Loki. It is secretly plotting to turn your beautiful source code into a pile of gibberish.

Every software development project has the tendency to grow bigger and more complex. The accomplices of complexity (customers, management, team colleagues) are lurking everywhere. Through feature requests and performance improvements they will make your code more complex. But you don't have to make it worse.

If you can pick and choose always go for the solution that is easier to implement.

If  you need to do sorting and you can choose between different algorithms: pick the simplest one. If that proves to be a problem swap it out later on (you should be using something like the the strategy pattern anyway).

Note (let's not talk about why I have to point this out):

Don't take shortcuts (singletons, declaring things static, breaking the separation of concerns principle....).

My 3rd rule is actually an extension of 2. A lot of times you will discover new problems while you are working on your items. If you find something that breaks the software RIGHT NOW:

  • open an issue
  • tell the project lead
  • fix it.

If it MIGHT be a problem there is a 80% chance that it will never actually happen:

  • report it
  • discuss it
  • If you can still put a MIGHT in the sentence describing the problem, leave it.

An example from my personal experience:

A developer was working on a fairly simple piece of model code. After seeing that the issue wasn't resolved after 4 days of work I asked the developer what was going on. He pointed out that the code for adding and removing items to the model wasn't threadsafe, so he started making it threadsafe. He had turned the whole thing into an intricated (an adjective that should make all your warning lights light up at the same time) piece of threadsafe beauty. At least that was what he thought.

The model class was used exclusively inside an Eclipse RCP application. Eclipse uses the reactor pattern, therefore everything is executed in the same thread.

He not only added tons of code that were simply not needed he also broke some things in the process.

My 4th rule is quite simple:

Nobody will be impressed by a complicated piece of code with the exception of the author. And he will lose the love for that code when he has to go back and understand it a few weeks after he wrote it.

Just remember that somebody will have to maintain it. And it will most likely be yourself.

Number 5 I just threw in because without documenting your code (and I mean in your code and not some fancy user docs) rules 1 - 4 are worthless.



No comments: