What can be optimized?

I’ve always thought there were 2 types of things that could be optimized: 

1. Things that need to be “cleaned up”.

2. Things that never should have been written in the first place.

Simple example of Type 1: You rush to get something up and running, and in your first code review, you find the exact same code multiple times. So you write a function, parameterize a few variables, tighten it up, and reference it all over the place. Cool. 

Simple example of Type 2: You have an SQL SELECT inside an iteration. At 500 iterations it runs smoothly. At 50,000 iterations, it becomes non-functional. Your only hope to scale this thing is to rethink the whole process to run with one SQL SELECT (and maybe a database redesign) outside the iteration. You basically have to start over. What were you thinking? 

You need to trust your “process” that Type 1 things will rise to the surface in due time, thus avoiding premature optimization. 

For Type 2 things, there is no such thing as “premature optimization”. They need to be designed and written properly in the first place.