Blogus Maximus

Rubbing people the wrong way since 1970...

  Home  |   Contact  |   Syndication    |   Login
  1366 Posts | 10 Stories | 2226 Comments | 1336 Trackbacks

News


Google My Blog

Catch me at: The List!


My InstallScript Utility Belt My Amazon Wishlist
My Standard Disclaimer

Tag Cloud


Archives

Post Categories

Image Galleries

Blogs

Code Camps

CTown Geeks

Geeky Webcomics

High Geek

Magenic Blogs

Microsoft Blogs

My Articles

My Sites

PodCasts

UG

XNA

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 = myObject.property
Else
   myVar = myControl.Text
End If

...OR...

myVar = myControl.Text
If expression Then myVar = myObject.property

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

Feedback

# re: Do you optimize too much? 10/31/2005 8:42 AM Eric Newton
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.

# re: Do you optimize too much? 10/31/2005 9:10 AM Nicholas Paldino [.NET/C# MVP]
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.

# re: Do you optimize too much? 10/31/2005 1:49 PM Cliff
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.

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


# re: Do you optimize too much? 10/31/2005 7:56 PM Brian
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 myObject.property, assigning myVar twice, which is why it runs slower.

The proper 2 option should be:

If expression Then myVar = myObject.property Else myVar = myControl.Text

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

myVar = (expression) ? myObject.property : myControl.Text;


Post A Comment
Title:
Name:
Email:
Comment:
Verification: