Thinking Inside the Box and Premature Optimization

At the last DC ALT.NET meeting, there was a good discussion regarding premature optimization.  Anything that involves this brings me to a rather famous quote by a great computer scientist, Donald Knuth which states:

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."

Don't Think Five Steps Ahead
 
The context of the conversation revolved around how when you learn a new language, you have a tendency to think inside the box and try not to prematurely optimize the code, instead, just meet the original intent of what you were trying to do as quickly and efficiently as possible and make it conform to the specification.  For example, one person there was a .NET guy who knew nothing of Java until put on a project recently where it was exclusively used.  This forced him to think more clearly about the problem and not just throw design patterns, caching solutions and so on.  When we are familiar with a language and all of its nuances, it sometimes gets the better of us, and we start throwing frameworks in there that have done things for us in the past just because they seemed appropriate then. It rang very true for me as well when I did my first Ruby on Rails application and I saw that I didn't need to bring in X framework or Y framework and my solution did just fine without it.  In fact, I thought it was a stronger piece because I didn't clutter it with frameworks and instead solved the direct domain problem at hand first.

It kind of reminds me of a conversation I once had with a developer which went like this:

Me: Why did you use x for this piece?
Him:  Because it's always worked for me in the past.
Me:  What problem does it solve?  Did you have that problem this time around?
Him:  Well, no, I was just thinking five steps ahead

So, basically what we can infer from this conversation is that the pain point just hadn't been hit yet.  Instead, reflexively, we have a tendency to throw these things in there because at one point, for one particular project, we felt that pain.  That's not to say that optimization is a bad thing, but once we start doing it, we need concrete measures for what the optimization is trying to fix and how well it does it.  Another part to consider is how easy is it to read.

ADD with DDD

Some of the guidance for this can be traced back to Domain Driven Design.  With this, you pay more attention to the model, the ubiquitous language and so on, and the frameworks in turn will come into play.  Instead, many people put their frameworks first and try to get the domain model to fit around it.  They are distracted by shiny things with their ADD to put the domain model first and then the frameworks.  I know I've been guilty of it in the past and it's been a learning adventure when I was earlier in my career.

I'll be back next with a real technology post on IoC, Unity and all that, so stay tuned.  Until next time...

kick it on DotNetKicks.com

1 Comment

Comments have been disabled for this content.