Geeks With Blogs


Google My Blog

Catch me at: The List!

My InstallScript Utility Belt My Amazon Wishlist
My Standard Disclaimer

Blogus Maximus Rubbing people the wrong way since 1970...

Ever see some code and think “I could rewrite that tighter and cleaner“ but when you do so, you wonder if you've actually improved anything or just made it harder to understand at first glance?

In the two examples below, both chunks of code end up doing the same thing. So... while option 1 is more intuitive (to me at least) than option 2, I couldn't help but wonder if this was strictly a matter of personal style and preference, or was there a potential performance difference in doing one way vs the other? For example, in option 2 you are potentially doing two assignment operations, etc...

If expression Then
   myVar =
   myVar = myControl.Text
End If


myVar = myControl.Text
If expression Then myVar =

So I did a little experimenting, ran the code a few bajillion times and found out that it actually is faster to use option one, in spite of the fact that it takes more lines of code. so now you know... and of course, option 1 also has the added bonus of being more obvious to the next guy who comes along to maintain your code. In many cases, that's more important (to me) than the potential performance boost (unless it's a big one)


Posted on Monday, October 31, 2005 8:31 AM General Interest | Back to top

Comments on this post: Do you optimize too much?

# re: Do you optimize too much?
Requesting Gravatar...
heh, yeah I'm victim of the same thing sometimes. I really do try to make my code readable first, performant second.

...but sometimes I just look at something and I'm like "dude, thats horrendous" and start rewriting for speed.
Left by Eric Newton on Oct 31, 2005 8:42 AM

# re: Do you optimize too much?
Requesting Gravatar...
The other thing that isn't mentioned here is that with option #1, myObject or myControl is allowed to not be initialized to anything, where as in option #2, myControl MUST be initialized.

It's subtle, but it's these kinds of assumptions that introduce bugs. The example is a little bad, only because they don't technically do the same thing.
Left by Nicholas Paldino [.NET/C# MVP] on Oct 31, 2005 9:10 AM

# re: Do you optimize too much?
Requesting Gravatar...
One idea I came across (it could have been Joel on Software) is the idea that once programmers know what they're doing, allowing them to quickly understand a segment of code, and keep it in their head is quite important.

From this perspective, the second option MAY be better from a maintenance perspective. Sure the first option looks like what you'd see in an intro textbook, but if bright programmers can extract the idea behind the block from both options at the same speed, then the second would be preferable in terms of screen real estate its taking up.

That said, if I was writing this, I'd probably go with the first option, just so no one accidentally adds code before the if or something crazy like that.
Left by Cliff on Oct 31, 2005 1:49 PM

# re: Do you optimize too much?
Requesting Gravatar...
that's funny, saving screen real estate was what inspired me to look at rewriting it in the first place.
Left by Chris Williams on Oct 31, 2005 2:28 PM

# re: Do you optimize too much?
Requesting Gravatar...
In option 1 you assign myVar only once while in option 2 you ALWAYS assign myVar to myControl.Text and then SOMETIMES assign it to, assigning myVar twice, which is why it runs slower.

The proper 2 option should be:

If expression Then myVar = Else myVar = myControl.Text

-or- how 'bout the same in c#?

myVar = (expression) ? : myControl.Text;
Left by Brian on Oct 31, 2005 7:56 PM

Your comment:
 (will show your gravatar)

Copyright © Chris G. Williams | Powered by: | Join free