'for' loops and time

• In one of my earlier jobs, I worked for a company that handled SMS messages for TV shows. Each show did something different with such messages, but they all passed through our server. I was building a framework to make it easier to develop apps that used these SMS messages.

One of the things I had to do back then was receiving a list of messages, extract from it the messages within two given periods, and then sorting it by the hour the messages were received. Quite easy, right?

<FONT color=#008080 size=2>DateTime</FONT><FONT size=2> start, end;</FONT></FONT><FONT size=2>

</FONT><FONT color=#008000 size=2>//Assing some value to the variables above.

</FONT><FONT size=2></FONT><FONT color=#0000ff size=2>for</FONT><FONT size=2> (</FONT><FONT color=#008080 size=2>DateTime</FONT><FONT size=2> dt = start; dt < end; dt = dt.AddHours(1))</FONT></FONT><FONT size=2>

{

</FONT><FONT color=#008000 size=2>    //Get the messages from the list in a foreach loop, if the hour matches, append them into another list.

</FONT><FONT size=2>

}

Where DateTime is a structure from .NET whose name is self-explaining enough.

However, the project manager couldn't understand what was between the parenthesis. No, he'd never seen in his entire life any for loop that didn't go like "for (int i = 0;...". You could substitute another variable name for the 'i', but that's it. He'd argue about a for loop if it used floating points instead of integers.

I tried explaining to him that the things between the ';'s are commands, all optional, and that you could do practically anything between them. A "for (;;)", for example, would be an infinite loop, akin to "while (true)". So I was just creating a DateTime variable and incrementing its hours with each iteration, until it reached some limit. But no, he argued it was plain wrong because he couldn't understand it.

We were done for the day, but the next day I found he altered my code (the one checked out in my machine, instead of checking in a version into the CVS system he'd set up himself). It was something like:

<FONT color=#0000ff size=2>

for</FONT><FONT size=2> (</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> month = startingMonth; month < endingMonth; month++)</FONT>

<FONT size=2>{</FONT><FONT size=2>

</FONT><FONT color=#0000ff size=2>    for</FONT><FONT size=2> (</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> day = 1; day < 32; day++)</FONT>

</FONT><FONT size=2>    {</FONT></FONT><FONT size=2>

</FONT><FONT color=#008000 size=2>        //Code to change the value of i if the first loop is still in the first iteration and it doesn't begin on day 1.

</FONT><FONT size=2>

</FONT><FONT color=#008000 size=2>        //Code to deal with the 6 months that don't have more than 30 days.

</FONT><FONT size=2></FONT><FONT color=#008000 size=2>        //Code to deal with leap years.</FONT></FONT><FONT color=#008000 size=2>

</FONT><FONT size=2></FONT><FONT color=#0000ff size=2>        for</FONT><FONT size=2> (</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> hour = 0; hour < 24; hour++)</FONT>

<FONT size=2>        </FONT></FONT><FONT <P><FONT size=2>{ </FONT></FONT><FONT mce_keep="true" <p> </FONT><FONT mce_keep="true" <p>

</FONT><FONT color=#008000><FONT size=2>            //Actual productive part of the code. Appends messages matching the time into another list.</FONT>

</FONT>

<FONT size=2>        }</FONT>

<FONT color=#008000><FONT size=2>        //code to change halt the loop prematurely if the first loop is in the last month and doesn't go until the last day of the month.</FONT>

</FONT>

<FONT size=2>    }</FONT>

<FONT size=2>}</FONT>

<FONT size=2>And it still didn't handle the problem we'd have months later when we wanted messages between the December of a given year and January of the year that came right next, for example. *sigh*...</FONT></FONT>

•  What an idiot. Shouldn't be to touch a computer at all. Anyway, you could have just changed that into a while loop, maybe he is more familiar with that...

• Quite a WTF. Did you confront him? (I'm well aware that being young and/or new greatly diminishes one's ability to confront a superior about their WTF-ery.)

• Regardless of your bosses technical (lack of) know how, changing the code on your machine without telling you after such a discussion seems to be your boss saying "I don't trust you to do your job". I can see why this was a former job.

• So, he found it easier to understand nested for-loops with logic to modify the variable controling the loops and additional logic to break the loops than a simple loop using .AddHours(1)? Hate to see his reaction when he discovers iterators.

•  I'm appalled that he would suggest a triple for loop for anything..  talk about Big O problems!

• @VGR said:

Quite a WTF. Did you confront him? (I'm well aware that being young and/or new greatly diminishes one's ability to confront a superior about their WTF-ery.)

I did. But then his superior just said that we shouldn't waste time with "geeky discussions" since we were behind schedule and that if that "stuff" worked we should leave it as it was.

@notromda said:

I'm appalled that he would suggest a triple for loop for anything..  talk about Big O problems!

Quadruple loop. He still had to loop through the messages looking for matches.

•  The real question is why were you "Assing" values to variables?

• +1 for the 'while' loop.

I wonder what would this manager say to such code:

```        foreach (DateTime date in getAllDates()) { ... }

...

private IEnumerable<DateTime> getAllDates()
{
for (DateTime date = StartDate; date <= EndDate; date = date.AddDays(1))
yield return date;
}
```

• Ok, here's my solution (if this was happening now of course): Overwrite his changes, then check in the code.

Manager: "Hey what happened to my changes?"

"Huh? What changes?"

"I changed that loop"

"Really? Odd. Let me check the CVS logs"

"No I changed it in your copy"

"My copy? I had to trash some changes I made and get a new copy from CVS. Why didn't you follow the normal checkout procedure?"

"Uhmm... never mind"

• @notromda said:

I'm appalled that he would suggest a triple for loop for anything..  talk about Big O problems!

Aren't both inner loops O(1)? So theoretically, complexity isn't that bad. But it never ceases to amaze me how clueless and arrogant people can be at the same time.

• Gee, WTF. What happened to good-old sorting. You do have a database to store the messages, right?

And this list stored in memory, is it used just once and thrown away? If yes, how was that created, that precluded a database handily making the sorting and/or filtering? If no, why it's not sorted to start with? In the latter case, only O(log(n) + m) would be needed (n: all messages, m: selected messages). Which would be much faster, especially for sparse periods (long time, few messages).

• [quote user="Renan "C#" Sousa"]But then his superior just said that we shouldn't waste time with "geeky discussions" since we were behind schedule and that if that "stuff" worked we should leave it as it was.[/quote]
I've been in that position before when on joining a major project I was told "we don't have time for design, we only have time to do the work". Guess which project rolled in a good 6 months later than the deadline?

•  @OzPeter said:

[quote user="Renan "C#" Sousa"]But then his superior just said that we shouldn't waste time with "geeky discussions" since we were behind schedule and that if that "stuff" worked we should leave it as it was.

I've been in that position before when on joining a major project I was told "we don't have time for design, we only have time to do the work". Guess which project rolled in a good 6 months later than the deadline?[/quote]

Hey, do we work for the same company?

• We don't have time to do unit tests, just start writing
• You'll write documentation afterwards
• I know it shouldn't be done this way, but it works so don't touch it
• What, you're new here? No, there's no doc. Just read through the code.
Sometimes I want to cry.

• It's been a while since I've seen an O(n*m) radix sort.

• @tster said:

The real question is why were you "Assing" values to variables?

Funny, it says "butting" on my screen.

• @operagost said:

@tster said:

The real question is why were you "Assing" values to variables?

Funny, it says "butting" on my screen.

Clbuttic!

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.