The World According to Nick
My take on Software, Technology, Politics, and anything else I feel like talking about.
Tuesday, September 14, 2004

Don't Write This Code! 

Warning: Geek Alert! Some coworkers of mine have been telling me stories now that I'm leaving, of the guy who used to sit where I do now. Mostly they've been doing this in an effort to show me the kinds of stories they'll make up about me once I'm gone (4 days and counting!). Anyway, the guys who used to sit where I do (who will remain nameless) apparently considered himself to be somewhat of a .NET Guru. However, then they shared with me a method which he used extensively to iterate through a collection which I will share with you now. If I ever see you write this code... I swear I will bring severe harm unto you.

ArrayList myList = MethodThatReturnsAList();
int i = 0;
try
{
   while( true )
   {
      object o = myList[i];
      // Do Something With o
      i++;
   }
}
catch( ArgumentOutOfRangeException )
{
   // Exit the loop
}

I can understand why he doesn't work here any more. From my understanding, he left just before getting canned.

Update: A certain Miss Macy has asked for a description as to why this is bad. More than that, she has requested it to be understood by non-geeks (which may be asking for a lot). So... here is the short version (cause I don't wanna bore you with the long version).

First of all, there are programming constructs meant just for this purpose... known as the for loop and the foreach loop which are readily identifiable as used for iteration through a collection of things (thus making for very readable code):

ArrayList myList = MethodThatReturnsAList();
foreach( object o in myList )
{
   // Do Something With o
}

Now then... doesn't that look so much nicer? Not only that, but the bad version above performs terribly. When the exception is thrown at the end of the "loop", the .NET runtime has to do a stack unwind which will take a couple seconds... that from a loop that should normally take a fraction of that depending on the size of the collection.

So it's not standard, it's less readable, and it performs like shit. All in all, it's just a bad way to do things.

Comments:

Can you please explain why for your readers that aren't able to read teckie code & know what it means?

  Posted at September 14, 2004 3:49 PM by Blogger miss macy  
Post a Comment

Related Links:

About Me



Name: Nick
Home: Wauwatosa, WI, United States

I'm a Software Consultant in the Milwaukee area. Among various geeky pursuits, I'm also an amateur triathlete, and enjoy rock climbing. I also like to think I'm a political pundit.


 View My Profile

Archives
 Home Page

Subscribe to this Feed

Search Archives
Previous Posts
The Real Cost of Immigration
Monkey Crime Wave
Blog Rolling
Did You Remember?
Rest Week
Undisclosed Sources II
How Do You Convince Voters?
Undisclosed Sources
Food Service Rants
Goodbye Indian Larry

Personal Links
Carnival of the Badger
The Coding Monkey
del.icio.us Links
Flickr Photos
Blog Critics Reviews





Blogroll Me!

music
books
video
culture
politics
sports
gaming

www.flickr.com
This is a Flickr badge showing public photos from Nick_Schweitzer. Make your own badge here.

Credits

Blogcritics: news and reviews







This page is powered by Blogger. Isn't yours?

Weblog Commenting and Trackback by HaloScan.com

RSS-to-JavaScript.com

Listed on BlogShares

Design By maystar