Navigate/Search

for (int i = 0; i < someUpperbound; i++){ /*do stuff*/ }

I don’t often comment on the search strings that bring people here – they certainly aren’t as interesting as the lesbian vampire sex permutations that bring random (and disappointed) browsers to The Mystery of the Haunted Vampire. (See what you have to look forward to, Doug?)

This morning, however, I got a doozy, one that really and truly prompted a “Dude. Wait. What?” moment, complete with mental needle-on-record sound effects. If you aren’t a programmer or otherwise IT-minded, skip this one – I really won’t mind. If you are, however, please join me after the jump…

If you’ve followed along, you probably know (or can infer) what the title code does: create a counter, set it to zero; while it’s less than some predetermined value, do stuff; when done doing stuff, increment the counter; repeat as necessary (Your ‘stuff’ might contain a condition that can exit the loop, for instance), or until the counter tells you to stop. It’s called iteration, and it’s a pretty basic concept. So what could have been so bad?

This: ‘C# for loop iterate in random order’

Dude. Wait. What?

It’s called ‘iteration’ for a reason – it iterates. It progresses in a sequential order through the elements of some kind of array or list. It doesn’t have to be a 0, 1, 2, … , n progression; heck, you can even change the value of i inside the {/*do stuff*/} part – quite useful if you’re testing elements and removing them from your list and thereby changing the value of someUpperbound. But to try and use a for loop to access elements randomly?

Seriously? Randomly? Admittedly, some data structures store their elements randomly; there’s nothing about lists or trees that guarantees that their elements are stored in any particular order. But accessing them randomly? No. Not what the for loop is for. It certainly isn’t what the .NET for loop is for.

And so, my anonymous searcher, please, please take some advice. Don’t. Just… Don’t. I know it’s probably already too late; 8 hours in internet time is a long time. Still, this is too important to let pass: I don’t care what kind of deadline you’re under, what the specs are, what your lead is telling you, what your clever streak is telling you.

Don’t. Do. It.

Step away from the keyboard, walk around, take a smoke break or walk around the block or ride the elevator up and down a few times. Get some coffee or water or Jolt or what-have-you and really think about the problem you’re really trying to solve. That search phrase is so fundamentally flawed that I can’t believe that you really mean to go about things that way.

See, don’t try and get clever with a basic concept – it’s almost always a one-way ticket on the Dumbass Express to WTF-istan. And no one wants that, particularly the code monkey who will inherit your cleverness.

And trust me – you really won’t want that if said future code monkey knows where you live.

12 Responses to “for (int i = 0; i < someUpperbound; i++){ /*do stuff*/ }”

  1. kate r Says:

    Wait. THE protected Static? Doug’s Protected Static? and is that carnacki the one who comments at dkos all the time? It is a small, tiny world.

  2. kate r Says:

    Wait. THE protected Static? Doug's Protected Static? and is that carnacki the one who comments at dkos all the time? It is a small, tiny world.

  3. protected static Says:

    Yes to both :-)

    Ah, the internet, a digital Zen paradox: simultaneously far larger and smaller than we realize.

  4. protected static Says:

    Yes to both :-)

    Ah, the internet, a digital Zen paradox: simultaneously far larger and smaller than we realize.

  5. Doug Says:

    I didn’t know you were carnacki! How did Kate figure that out — did she follow you back from DKos?

    As for random iteration, no comment. Sounds like something I might do without a second thought. In fact, I think it describes my life.

  6. protected static Says:

    Oh no, I’m not Carnacki – I’m the site’s webmaster (I own the domain name) and I occasionally post there when the mood strikes me…

    Mostly I’m just Carnacki’s web bitch :-)

    I saw Kate’s trackback when I logged in this morning and I left a comment on her blog. She found MotHV all on her own.

  7. Doug Says:

    I didn't know you were carnacki! How did Kate figure that out — did she follow you back from DKos?

    As for random iteration, no comment. Sounds like something I might do without a second thought. In fact, I think it describes my life.

  8. protected static Says:

    Oh no, I'm not Carnacki – I'm the site's webmaster (I own the domain name) and I occasionally post there when the mood strikes me…

    Mostly I'm just Carnacki's web bitch :-)

    I saw Kate's trackback when I logged in this morning and I left a comment on her blog. She found MotHV all on her own.

  9. Dean Says:

    Jeebus. So who the fuck is Carnacki then?

    Ok. I can’t think of any reason to attempt to iterate a list in RANDOM order. I can think of several ways to do it, though. Probably the best way would be to create the list in random order and iterate it.

    Maybe that’s what the guy was looking for. Maybe he was looking for a recipe for iterating an unordered list in some sort of order. The more I think about it, the more I think that’s it, and his (we know it’s a him, don’t we?) English skills are lacking.

    There, I hope you sleep better knowing that your fears are likely unfounded.

  10. Dean Says:

    Jeebus. So who the fuck is Carnacki then?

    Ok. I can't think of any reason to attempt to iterate a list in RANDOM order. I can think of several ways to do it, though. Probably the best way would be to create the list in random order and iterate it.

    Maybe that's what the guy was looking for. Maybe he was looking for a recipe for iterating an unordered list in some sort of order. The more I think about it, the more I think that's it, and his (we know it's a him, don't we?) English skills are lacking.

    There, I hope you sleep better knowing that your fears are likely unfounded.

  11. protected static Says:

    I am Carnacki Spartacus!

    Oh, we know it’s a him… And I hope to Ghu you’re right – I was having visions of a RandomList class where MoveNext(), well, didn’t.

  12. protected static Says:

    I am <del>Carnacki</del> <a href="http://www.hauntedvampire.com/&quot; rel="nofollow">Spartacus</a>!

    Oh, we know it's a him… And I hope to Ghu you're right – I was having visions of a <code>RandomList<T></code> class where <code>MoveNext()</code>, well, didn't.

Leave a Reply