Rob Garrett - Blogs

Welcome to Rob Garrett - Blogs Sign in | Join | Help
in Search
Google

Software/Technology Discussion

Software and Technology Tid-bits

Null, Empty Strings and Performance Programmers

Jeff Atwood makes a complaint about "performance programmers" breaking his code.

In his example, Jeff shows the following snippet of code:

If Value <> "" Then
  If nvc.Item(name) = "" Then
    nvc.Add(name, Value)
  End If
End If

... which was then changed to by the performance programmer.:

If Value <> String.Empty Then
  If nvc.Item(name).Equals(String.Empty) Then
    nvc.Add(name, Value)
  End If
End If

The new code now breaks because if the NameValueCollection (nvc) does not have an item in the container with name it'll return null/nothing, which causes the call to Equals to fail with a null reference exception. Jeff's code works because null references can be compared with empty string in C# and VB.NET.

Comparing null references to empty string is not good programming practice in general (in C++ such a comparison would cause an exception). Jeff wrote his code knowing that this comparison was safe because he knew about the language fundamentals in which he was developing, the performance programmer did not. This is a good example of the typical traps that most performance crack teams fall into when tuning an existing application. Developing software is an art form - writing good maintainable code that works and performs well sometimes requires the developer to use non-typical syntax, which can throw other unfamiliar developers of the code into a loop.
Share this post: Email it! | bookmark it! | digg it! | reddit!
Published Wednesday, January 12, 2005 8:38 AM by Rob Garrett

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Chris Hynes said:

I agree that prematurely or overly emphasizing performance is a bad thing, but in this case, I'd argue that Jeff wrote bad code. The code should look like this, for both readability and performance reasons:

If Value.Length > 0 Then
If nvc.Item(name) Is Nothing Then
nvc.Add(name, Value)
January 13, 2005 9:44 AM
 

Chris Hynes said:

Grr... Why does the enter key have to submit a form when you're trying to write code... Anyway...

It's as true now with .NET as it was in the early days of VB: to check for a zero length string ALWAYS ALWAYS ALWAYS use the length of the string rather than comparing to "". When you compare to "", not only is it a string comparison (slower), rather than an integer comparison (faster), but a new string is also created.

Furthermore, the if statement to check if the item is already in the collection may be able to be eliminated entirely. If the idea is to set the value of the item in the collection, regardless of whether it exists or not, just setting the Item will suffice:

If Value.Length > 0 Then
&nbsp;&nbsp;nvc.Item(name) = Value
End If

The point is, Jeff's code is unclear because it makes assumptions by not explicitly checking for nulls, and is inefficient because it uses string comparisons where integer comparisions would suffice. A more performant and efficient implementation, in this case at least, is much more simple, clear, and readable.
January 13, 2005 9:51 AM
 

Jeff Atwood said:

I agree with your "he should have used Is Nothing" comment. My use of <> "" doesn't buy me anything and isn't as clear as it should be. This is really trivial code in a really trivial part of the app, though, so that wasn't really my point.

Beyond that, I think you're doing exactly what you said you didn't want to do: prematurely optimizing. And that was my point!
January 15, 2005 1:29 AM
 

TrackBack said:

In my previous entry on the real cost of performance, there were some complaints that my code's slow and it sucks. If I only had a nickel for every time someone told me that. Let's take a look at...
January 15, 2005 2:53 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit

Blurb


Head Shot
Rob Garrett is a British Expat living in Maryland USA. Rob is a trained software engineer and experienced in Windows .NET development.

Rob enjoys listening to Rock music, posting to blogs, driving in the country with the sunroof open, beer (not in conjunction with country driving) and spending time with his family.

This Blog

Syndication

Powered by Community Server, by Telligent Systems