The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

What's the for loop for?

Last post 07-26-2008 3:03 PM by Faxmachinen. 31 replies.
Page 1 of 1 (32 items)
Sort Posts: Previous Next
  • 07-23-2008 4:06 PM

    What's the for loop for?

     A couple years back we had this programmer.  New to us, but not a newbie.  Understood software reasonably, but not as good with the hardware side of things.  Let's call him D.

    Well, we are a hardware company.  We make pressure sensors and the corresponding software.  We also have to test the hardware, so we have a test rig and testing software that exercises the electronics.

    For some background, our sensors are thin flexible sheets that insert into a handle which contains the electronics.  Of course, the handle has to check for proper alignment.  Two contacts on the sensor are wired together.  If the electronics in the handle detect continuity, we assume good sensor insertion.

    Of course, the test rig can close or open the circuit between these contacts to verify the handle under test detects both aligned and unaligned states.

    So on to our story.
    D was tasked with changing the test application to work with a new set of electronics.  The test procedure itself was nearly unchanged.  He asks me to explain a piece of code:

    bool TestAlignment()
    {
      bool ok = true;
      for (int i = 0; i < 2; ++i)
      {
        testRig->SetAlignment(i);
        ok = ok && handle->IsAligned() == i;
      }
      return ok;
    }

    D was confused as to why we would use a for loop.  It took quite a few minutes to convey the idea that there are two states to check.

  • 07-23-2008 4:22 PM In reply to

    • movzx
    • Not Ranked
    • Joined on 07-11-2008
    • Posts 40

    Re: What's the for loop for?

    I might have found myself questioning the for loop as well. It just seems weird having it for that small bit of code, especially without any sort of context. If the 2 was maybe represented as NUMBER_OF_STATES it would have clicked a lot faster (at least for me).

  • 07-23-2008 4:24 PM In reply to

    Re: What's the for loop for?

    slight optimization

    for (int i = 0; (i < 2) && ok; ++i)
      {
        testRig->SetAlignment(i);
        ok &= (handle->IsAligned() == i);
      }

    If you fail the first alignment, you might as well not check the second.

  • 07-23-2008 4:27 PM In reply to

    • dgvid
    • Top 500 Contributor
    • Joined on 04-19-2008
    • Virginia, United States
    • Posts 64

    Re: What's the for loop for?

    I'm confused as to why you would use a for loop here, too, assuming the hard-coded value of 2 is not likely to change in the future. For a value of 2, I would unroll that loop and produce what, in my opinion, is much more readable code:

    testRig->SetAlignment(0);
    bool ok = handle->IsAligned() == 0;
    testRig->SetAlignment(1);
    return ok && handle->IsAligned() == 1;
  • 07-23-2008 4:40 PM In reply to

    • Vechni
    • Top 200 Contributor
    • Joined on 09-12-2007
    • Baltimore, MD
    • Posts 185

    Re: What's the for loop for?

    dgvid:

    I'm confused as to why you would use a for loop here, too, assuming the hard-coded value of 2 is not likely to change in the future. For a value of 2, I would unroll that loop and produce what, in my opinion, is much more readable code:

    testRig->SetAlignment(0);
    bool ok = handle->IsAligned() == 0;
    testRig->SetAlignment(1);
    return ok && handle->IsAligned() == 1;

     

     seconded.

  • 07-23-2008 4:42 PM In reply to

    Re: What's the for loop for?

    pitchingchris:

    ..

    If you fail the first alignment, you might as well not check the second.

    True, but I shortened the example for posting.  The actual code returns the correctness of both checks.  It's useful for production personnel to know just how it failed - it indicates whether it's likely a short or open.

    dgvid:

    unroll loop

     I agree that, for the example I listed, unrolling the loops makes some sense.  However, the actual is a little bit longer and the duplication is a pain. There is error checking for communicating with the test rig, delay to make sure the handle under test has had time to check the alignment state itself.

     I tried getting to the essence, but maybe lost a little too much.

  • 07-23-2008 5:51 PM In reply to

    Re: What's the for loop for?

    Vechni:

    dgvid:

    I'm confused as to why you would use a for loop here, too, assuming the hard-coded value of 2 is not likely to change in the future. For a value of 2, I would unroll that loop and produce what, in my opinion, is much more readable code:

    testRig->SetAlignment(0);
    bool ok = handle->IsAligned() == 0;
    testRig->SetAlignment(1);
    return ok && handle->IsAligned() == 1;

     

     seconded.

     

    Thirded.  This is a test case after all, not optimized code. 

  • 07-23-2008 5:56 PM In reply to

    Re: What's the for loop for?

    pitchingchris:

    for (int i = 0; (i < 2) && ok; ++i)
      {
        testRig->SetAlignment(i);
        ok &= (handle->IsAligned() == i);
      }

    If you fail the first alignment, you might as well not check the second.

    Yes. Not like you're doing it though.

     

    rpar PROTON all
  • 07-23-2008 6:15 PM In reply to

    Re: What's the for loop for?

    DogmaBites:
    ok = ok && handle->IsAligned() == i;
    It's been a long while since I touched C++, but was is this supposed to do?

    ok = ok && handle->IsAligned();

    I can understand, but what does the "== i" do? 

  • 07-23-2008 7:31 PM In reply to

    Re: What's the for loop for?

    billhead:

    DogmaBites:
    ok = ok && handle->IsAligned() == i;
    It's been a long while since I touched C++, but was is this supposed to do?

    ok = ok && handle->IsAligned();

    I can understand, but what does the "== i" do? 

     

     It is a comparison operator that returns a boolean, which is then checked against ok and that boolean is then assigned to the variable.

  • 07-23-2008 7:34 PM In reply to

    Re: What's the for loop for?

    billhead:

    DogmaBites:
    ok = ok && handle->IsAligned() == i;
    It's been a long while since I touched C++, but was is this supposed to do?

    ok = ok && handle->IsAligned();

    I can understand, but what does the "== i" do? 

    My guess is IsAligned() returns an integer value, probably the get counterpart to SetAlignment (since he passes i to that). Thus he has to compare to an integer value, and he compares it to the value he just assigned to it. Why it's not GetAlignment(), I don't know.

  • 07-23-2008 7:41 PM In reply to

    Re: What's the for loop for?

    aquanight:
    billhead:

    DogmaBites:
    ok = ok && handle->IsAligned() == i;
    It's been a long while since I touched C++, but was is this supposed to do?

    ok = ok && handle->IsAligned();

    I can understand, but what does the "== i" do? 

    My guess is IsAligned() returns an integer value, probably the get counterpart to SetAlignment (since he passes i to that). Thus he has to compare to an integer value, and he compares it to the value he just assigned to it. Why it's not GetAlignment(), I don't know.

     

     It probably returns a boolean.  Assuming of course that something is either aligned or not.

  • 07-24-2008 4:35 AM In reply to

    Re: What's the for loop for?

    I think that the question is more or less valid.

    Resorting to unusual techniques like this should flag that the structure is wrong.

    I would favour the following:

    bool TestAlignment()
    {
      bool ok = TestAlignment(0);
      ok &&= TestAlignment(1);
      return ok;
    }

    bool TestAlignment(int i)
    {
        testRig->SetAlignment(i);
        return (handle->IsAligned() == (bool)i);
    }

    Linux is not a code base. Or a distro. Or a kernel. It's an attitude. And it's not about Open Source. It's about a bunch of people who still think vi is a good config UI.

    Notice: Phorm, and its agents including ISPs collecting data on Phorm's behalf, are specifically forbidden from performing any processing or monitoring of the content of the above post. Hence, under the Regulation of Investigatory Powers Act 2000 any such attempt to profile this page by Phorm or its agents is illegal.
  • 07-24-2008 4:42 AM In reply to

    • ammoQ
    • Top 10 Contributor
    • Joined on 04-13-2005
    • Vienna.Austria.Europe.Earth
    • Posts 3,444

    Re: What's the for loop for?

    GettinSadda:

    I would favour the following:

    bool TestAlignment()
    {
      bool ok = TestAlignment(0);
      ok &&= TestAlignment(1);
      return ok;
    }

     

    Now get rid of the useless variable, and we are done.

    bool TestAlignment()
    {
      return TestAlignment(0) && TestAlignment(1);
    }

    beanbag girl 4ever
  • 07-24-2008 5:26 AM In reply to

    Re: What's the for loop for?

     Funroll Loops!

    The good doctor is here to help. I promise this time I will not screw up the operation.
  • 07-24-2008 9:28 AM In reply to

    Re: What's the for loop for?

    DogmaBites:

     I agree that, for the example I listed, unrolling the loops makes some sense.  However, the actual is a little bit longer and the duplication is a pain. There is error checking for communicating with the test rig, delay to make sure the handle under test has had time to check the alignment state itself.

     

    And your immediate reaction was to write a hardcoded 2-iteration loop instead of a subroutine?

  • 07-24-2008 10:02 AM In reply to

    Re: What's the for loop for?

    ammoQ:

    GettinSadda:

    I would favour the following:

    bool TestAlignment()
    {
      bool ok = TestAlignment(0);
      ok &&= TestAlignment(1);
      return ok;
    }

     

    Now get rid of the useless variable, and we are done.

    bool TestAlignment()
    {
      return TestAlignment(0) && TestAlignment(1);
    }

    For the love of God you people have over complicated this.. I'm not sure if you are joking or not, but here we go..

     

    bool TestAlignmen()
    {
        return testRig->SetAlignment(true) && handle->IsAligned() && !testRig->SetAlignment(false) && !handle->IsAligned();
    }
  • 07-24-2008 10:04 AM In reply to

    Re: What's the for loop for?

    ammoQ:

    GettinSadda:

    I would favour the following:

    bool TestAlignment()
    {
      bool ok = TestAlignment(0);
      ok &&= TestAlignment(1);
      return ok;
    }

     

    Now get rid of the useless variable, and we are done.

    bool TestAlignment()
    {
      return TestAlignment(0) && TestAlignment(1);
    }

     

    Yeah, but I left that in so that it could be extended to values of (2) (3) (4) etc.

     

    Linux is not a code base. Or a distro. Or a kernel. It's an attitude. And it's not about Open Source. It's about a bunch of people who still think vi is a good config UI.

    Notice: Phorm, and its agents including ISPs collecting data on Phorm's behalf, are specifically forbidden from performing any processing or monitoring of the content of the above post. Hence, under the Regulation of Investigatory Powers Act 2000 any such attempt to profile this page by Phorm or its agents is illegal.
  • 07-24-2008 10:14 AM In reply to

    • ammoQ
    • Top 10 Contributor
    • Joined on 04-13-2005
    • Vienna.Austria.Europe.Earth
    • Posts 3,444

    Re: What's the for loop for?

    morbiuswilters:

    bool TestAlignmen()
    {
        return testRig->SetAlignment(true) && handle->IsAligned() && !testRig->SetAlignment(false) && !handle->IsAligned();
    }

     

    Here you are assuming that testRig->SetAlignment returns the parameter. The OP gives no indication that this assumptions holds.

    beanbag girl 4ever
  • 07-24-2008 10:44 AM In reply to

    Re: What's the for loop for?

    ammoQ:

    morbiuswilters:

    bool TestAlignmen()
    {
        return testRig->SetAlignment(true) && handle->IsAligned() && !testRig->SetAlignment(false) && !handle->IsAligned();
    }

     

    Here you are assuming that testRig->SetAlignment returns the parameter. The OP gives no indication that this assumptions holds.

    True, but it's not like it's that difficult to make it do that.  This is the more difficult case because if it returns true on successful setting or void then it's easy to just anticipate that value and use it. 

  • 07-24-2008 10:50 AM In reply to

    • IMil
    • Not Ranked
    • Joined on 05-26-2006
    • Posts 30

    Re: What's the for loop for?

    ammoQ:
    Now get rid of the useless variable, and we are done.
    
    bool TestAlignment()
    {
      return TestAlignment(0) && TestAlignment(1);
    } 
    
    Nice code; the only problem is that it isn't correct. In the original post, the SetAlignment() procedure was called for both 0 and 1; your code may skip the second call.
  • 07-24-2008 10:56 AM In reply to

    • ammoQ
    • Top 10 Contributor
    • Joined on 04-13-2005
    • Vienna.Austria.Europe.Earth
    • Posts 3,444

    Re: What's the for loop for?

    IMil:
    ammoQ:

    Now get rid of the useless variable, and we are done.

    bool TestAlignment()
    {
    return TestAlignment(0) && TestAlignment(1);
    }

    Nice code; the only problem is that it isn't correct. In the original post, the SetAlignment() procedure was called for both 0 and 1; your code may skip the second call.
     

    That can be corrected easily:

     bool TestAlignment(int i)
    {
        static bool ok;
        bool result;
        testRig->SetAlignment(i);
        result = (handle->IsAligned() == (bool)i);
        if (i==1) {
            result &&=ok;
        }
        else {
            ok = result;
            result = true;
        }
        return result;
    }

    beanbag girl 4ever
  • 07-24-2008 11:09 AM In reply to

    Re: What's the for loop for?

     I think that everyone is missing the point of the WTF.

     This all could have been avoid if the code looked like this:

    bool TestAlignment()
    {
      bool ok = true;

       //there are two states to check, therefore we loop through twice

      for (int i = 0; i < 2; ++i)
      {
        testRig->SetAlignment(i);
        ok = ok && handle->IsAligned() == i;
      }
      return ok;
    }

  • 07-24-2008 11:25 AM In reply to

    Re: What's the for loop for?

     

    Aaron:

    DogmaBites:

     I agree that, for the example I listed, unrolling the loops makes some sense.  However, the actual is a little bit longer and the duplication is a pain. There is error checking for communicating with the test rig, delay to make sure the handle under test has had time to check the alignment state itself.

     

    And your immediate reaction was to write a hardcoded 2-iteration loop instead of a subroutine?

    Why are you assuming I am the original author of the test code?  My purpose of posting the story was the surprise at someone else's difficulty of understanding code that seemed obvious to me.  Nowhere did I state that I wrote it.  If you think the purpose of the code was not obvious, then this may not be a real WTF. 
  • 07-24-2008 1:05 PM In reply to

    Re: What's the for loop for?

    Come on guys, we're better than this. All I see is terrible assumptions. Can't anybody reformat the code without changing its functionality?

    bool TestAlignment()
    {
    	testRig->SetAlignment(0);
    	if (handle->IsAligned() != 0)
    			return false;
    	
    	testRig->SetAlignment(1);
    	if (handle->IsAligned() != 1)
    			return false;
    	
    	return true;
    }

    Or the loop variety:

    const int ALIGNMENTS = 2;
    
    bool TestAlignement()
    {
    	for (i = 0; i < ALIGNMENTS; i++)
    	{
    		testRig->SetAlignment(i);
    		if (handle->IsAligned() != i)
    			return false;
    	}
    	return true;
    }
    rpar PROTON all
  • 07-24-2008 1:23 PM In reply to

    • ammoQ
    • Top 10 Contributor
    • Joined on 04-13-2005
    • Vienna.Austria.Europe.Earth
    • Posts 3,444

    Re: What's the for loop for?

    Faxmachinen:
    Come on guys, we're better than this. All I see is terrible assumptions. Can't anybody reformat the code without changing its functionality?
     

    Apparantly, not even you can do that. Unlike the original post, both of your versions never call testRig->SetAlignment(1) if the first test fails.

    beanbag girl 4ever
  • 07-24-2008 1:34 PM In reply to

    • tiro
    • Top 500 Contributor
    • Joined on 05-20-2005
    • USA, Pennsylvania
    • Posts 59

    Re: What's the for loop for?

    DogmaBites:


    bool TestAlignment()
    {
      bool ok = true;
      for (int i = 0; i < 2; ++i)
      {
        testRig->SetAlignment(i);
        ok = ok && handle->IsAligned() == i;
      }
      return ok;
    }

    D was confused as to why we would use a for loop.  It took quite a few minutes to convey the idea that there are two states to check.

     

    Well, this wouldn't probably be considered a fairly confusing snippet of code in the software engineering world.  Using 0 and 1 instead of defining constants that represent those states is usually considered bad practice.  1 and 0 are magic numbers that could mean anything, but ALIGNED and UNALIGNED is much more unabigious, or maybe a boolean if it's the sort of thing you're sure will never have more than two states.  It's also kind of a strange use of a for loop, but that might make sense in context.

  • 07-24-2008 2:20 PM In reply to

    Re: What's the for loop for?

    ammoQ:
    Apparantly, not even you can do that.

    Indeed. Let's try again.

    bool TestAlignment()
    {
    	testRig->SetAlignment(0);
    	if (handle->IsAligned() != 0)
    	{
    		testRig->SetAlignment(1); // TODO: Check if this is necessary
    		return false;
    	}
    
    	testRig->SetAlignment(1);
    	if (handle->IsAligned() != 1)
    			return false;
    	
    	return true;
    }
    rpar PROTON all
  • 07-24-2008 5:26 PM In reply to

    Re: What's the for loop for?

    DogmaBites:

    Why are you assuming I am the original author of the test code?  My purpose of posting the story was the surprise at someone else's difficulty of understanding code that seemed obvious to me.  Nowhere did I state that I wrote it.  If you think the purpose of the code was not obvious, then this may not be a real WTF. 

     

    Okay, chill - maybe you didn't write it but you implied that it was perfectly logical to use a loop for two essentially discrete operations and had to explain this to someone who found it awkward and/or confusing.  If I'd been handed down this code from a 3rd party and somebody asked me why it used a loop, my response probably would have been "Good point, it does seem pretty silly here."

  • 07-24-2008 5:49 PM In reply to

    • Moose
    • Not Ranked
    • Joined on 06-05-2008
    • Posts 3

    Re: What's the for loop for?

    Faxmachinen:

    ammoQ:
    Apparantly, not even you can do that.

    Indeed. Let's try again.

    bool TestAlignment()
    {
    	testRig->SetAlignment(0);
    	if (handle->IsAligned() != 0)
    	{
    		testRig->SetAlignment(1); // TODO: Check if this is necessary
    		return false;
    	}
    
    	testRig->SetAlignment(1);
    	if (handle->IsAligned() != 1)
    			return false;
    	
    	return true;
    }

     

     

    Which seems to me far more complicated than the original!

    Here's mine, for the record: 

     
    bool TestAlignment()
    {

        testRig->SetAlignment(0); ok = !handle->isAligned();

        testRig->SetAlignment(1);

        return ok && handle->IsAligned();

    }


    I see no reason for the loop either in this particular case.
  • 07-25-2008 8:56 AM In reply to

    Re: What's the for loop for?

     Using a loop like this should never pass a code review. Write a fucking function asshole.

  • 07-26-2008 3:03 PM In reply to

    Re: What's the for loop for?

    Moose:
    Which seems to me far more complicated than the original!
    Yes, apparantly the original was so simple that not even you understood it. Did you ever consider what happens if IsAligned() returns 2?
    rpar PROTON all
Page 1 of 1 (32 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems