How to deal with “First you write, then you optimize”

Recently I’ve made yet another attempt to optimize several quite inefficient report, which had been out there for so long, that nobody remembers, who have written them, and who have speced them out. I did not finish this task, even with the help of my team we just didn’t have enough time to spare on improving something, which “works”. And when I was thinking about it over the weekend, I thought that different people may have different definition of “works”, when it comes to SQL writing.

I always say – it’s easier to write the code correctly on the first place, that to optimize reports which are written inefficiently, but way too often I am getting a response from the stakeholders – we need it now, you can optimize it later. And then later never comes.

This is so obvious, that I would not spend time writing about this yet another 101th time. However this time my thoughts have taken a different path. I thought about why I was unable to finish this optimization, why I didn’t have enough time. The reason was, that something else which I was writing have taken way more time than I originally planned. And the reason why it took so long is that I find myself unable to write, even as a first draft, the code which I know for sure is suboptimal. No matter how many people will tell me that “it does not have to be perfect”. And then – yes, it takes longer.

Now I think that when I teach an optimization class, and when I am showing to my audience some cool technique, my thought process is that everybody should realize how much better this technique is, and than use “that and only that” approach, because how-else-this-is-so-obviously-better. And as for my audience, people think – ok. It’s cool. Good to know. And do not change there code -writing habits…

Once again, I might have been just ignorant, and may be everybody knew it all along, but it was a revelation for me: if I want to teach people good coding habits, if I really want to avoid this situation “first write, then optimize”, not only that I need to show people how to write a coode code. And not only I need to show how not to write. But I also need to teach zero-tolerance to the bad coding. So that people’s minds won’t even wander in this direction:). If it is not written the right way, it is not done.

I am pretty sure nobody would like this idea, but can I at least have a dream :)?


Filed under SQL

3 responses to “How to deal with “First you write, then you optimize”

  1. Sovel

    I completely agree with your point regarding changing codding habits, a need to teach how to write good code and how not to write code.
    I have come across technical term “premature optimization”. Do you think there are occasions where optimization can be done prematurely and will cause additional refactoring?

  2. Henrietta Dombrovskaya

    You know, I never heard this term, and now that you’ve mentioned it, I looked it up. From my perspective (aka – I am a database) – i disagree with what I’ve read about this concept. Actually I think that everything I write in terms of SQL is “prematurely optimized”, because when I start coding I can tell right away whether this particular SQL will be efficient when the data volumes wil grow say 10 times. It might be incorrect for the application development, but for the database development I firmly believe we should code with the future growth considerations in mind. Like if something is executed in a linear manner, it may be OK with a couple of thousands records result set, and NOT ok with a couple of hundred thousands records result sets. And these are the patterns I am trying to find and eliminate before they become problematic. Does it make sense?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s