Programming is too delicate a task to heavily rely on generalisms. Even at the relatively high level of programming we do, what works in one situation isn’t guaranteed to work in another. As developers we typically lack the imagination necessary to foresee even a fraction of how real users will use our systems. This combination makes it dangerous to rely on anything but firsthand experience.
Good developers must pursue their intuition and satisfy their curiosity by means of continuous experimentation. Sadly, too many developers look down on micro optimization, ensuring that curious developers never ask their performance question for fear of being ridiculed (I found a couple of newsgroup posts from years ago where someone asked about string to integer conversion and I arrogantly shot them down for asking such an insignificant question). It’s rather silly that on the one hand we fear leaky abstractions is getting the best of our community, and on the other we look down at attempts to gain greater understanding.
The most cited reason against micro optimization is that clarity/maintainability/readability should trump any minor performance gains. That implies that the two concepts are mutually exclusive – which may be true in some cases, but in other cases isn’t at all (such as generics). And, much like you can’t know if something is a micro (or a macro) optimization until you try, there’s a good chance you can’t really tell the impact on maintainability until you let your creativity loose.
Back to my point about generalities, and we can see how easy it is for someone not to have to try it out for themselves. There are countless blogs that praise StringBuilders as the fastest way to concatenate a string – which is true except for when it isn’t – and even more blogs that praise the superior performance of stored procedures – which is also true except when it isn’t. Blogs and bloggers aren’t to blame though. In the end, developers who sheepishly follow, perhaps because others have shamed them into not asking, are responsible for their own enlightenment.
I also want to point out that when it comes to both micro optimization and premature optimization, unit tests should play a significant role in your experimentations. Properly written unit tests will ensure that an optimized version works within the same parameters as the original. TDD should embrace an addition to their red/green/refactor motto --> red/green/optimize/refactor.
If you don’t optimize early and often, you’ll never understand the value and cost of those optimizations. All you’ll have to go by are general statements made by others, rather than an actual understanding of what’s happening beneath the compiler.
While the Alpha Geeks are off mastering Ruby, I think I might spend some spare cycles trying to familiarize myself with MSIL.
