Premature Optimization.

In coding world the ‘optimized’ code is termed as a ‘Dream girl’, everyone wants her.

But as usual Dream girls are tough and hard to crack.images

If you have one, you will go and do some show off.

You will get all the desired attention and ‘oohs’ and ‘aahs’ from everyone around you, including your manager.

No doubt it’s a beautiful creation.

A piece of code so wonderfully written that it flows like water.

You can’t resist but will fall in love with it instantly.

But we know already that there is always the other sides of the coin.

“When” is the question you need to ask for optimization.

Again if we connect it with our analogy of ‘Dream girl’ it’s the right age that matters.

No matter how amazed you are by seeing your five year old dressed up like diva, it is going to take a toll on you and her to get through in her rough years wearing that silk silhouette.

It is better if she has matured in her ways and then the perfection comes.

Coming back to IT world, there is one very famous saying in Database domain:

Optimize till it hearts and then de-optimize till it works.

Why:

Optimization is not easy as we already know. It gives a real tough time to the person trying to achieve it (what is the fun otherwise?).

But it has the potential to drive away your focus from goal to methodology.

Let me share my own experience on it.

I went to one of the renowned company’s job interview for technical position, and they wanted to test my coding skills.

I and other interviewees were given a problem to solve.

I was very determine to apply all the SOLID principles and correct Design Patterns in my coding. And that drove me off my goal of delivering a working solution to the given problem.

Rather than solving the problem I was focused on how can I achieve the best design for my work.

There is nothing wrong in that approach, but time was the constraint.

I couldn’t complete my assignment as my design gone huge and complex. References seems crossing each other.

By the time I realized I need to deoptimize my code, it was late.

I let myself hunted by ghost of premature optimization and that didn’t serve the purpose.

  1. I lost focus on ‘goal’.
  2. I got stuck  in applying the principles rather than utilizing them as tool I have.

Solution:

  1. Start with end in your mind. Clearly understand the problem definition first. Solution comes later.
  2. Your aim is ‘Get it done’. Because no matter how beautiful the non-working code is, it is still non-working.
  3. Meet your goal first. May be with some messy code. It is Ok.
  4. Now start the rework and make it optimized. This way you don’t miss you end goal as it’s already met. Make sure you don’t break anything with refactoring though.
  5. You can take as many iterations as you want and time permits to get you code nearer to perfection.(Let me tell you there is no perfect code. Believe me.)
  6. Here you go.

Disclaimer:

In IT industry there are no hard bound rules/steps to achieve something. It can be achieved with predefined steps and/or without any.

There is nothing like you just can’t go with optimization. You can surely do it with your experience and expertise.

Coding is an art and each artist is different, and these differences make it more interesting.

Leave a comment