|
What's the for loop for?
Last post 07-26-2008 3:03 PM by Faxmachinen. 31 replies.
-
07-23-2008 4:06 PM
|
|
-
-
movzx


- Joined on 07-11-2008
- Posts 12
|
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).
|
|
-
-
pitchingchris


- Joined on 04-30-2007
- Elizabethtown, KY
- Posts 360
|
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.
|
|
-
-
dgvid


- Joined on 04-19-2008
- Virginia, United States
- Posts 49
|
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;
|
|
-
-
Vechni


- Joined on 09-12-2007
- Baltimore, MD
- Posts 100
|
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.
|
|
-
-
DogmaBites


- Joined on 07-15-2008
- Posts 46
|
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:
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.
|
|
-
-
Soviut


- Joined on 09-13-2007
- Posts 90
|
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.
|
|
-
-
Faxmachinen


- Joined on 03-19-2007
- Posts 190
|
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
|
|
-
-
billhead


- Joined on 11-14-2007
- Posts 12
|
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?
|
|
-
-
VibroKatana


- Joined on 07-23-2008
- Posts 4
|
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.
|
|
-
-
aquanight


- Joined on 11-19-2006
- Posts 81
|
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.
|
|
-
-
VibroKatana


- Joined on 07-23-2008
- Posts 4
|
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.
|
|
-
-
GettinSadda


- Joined on 05-25-2006
- North-East Scotland
- Posts 243
|
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 it's 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 it's agents is illegal.
|
|
-
-
ammoQ


- Joined on 04-13-2005
- Vienna.Austria.Europe.Earth
- Posts 3,291
|
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
|
|
-
-
DrJokepu


- Joined on 03-20-2008
- London, UK
- Posts 96
|
Re: What's the for loop for?
The good doctor is here to help. I promise this time I will not screw up the operation.
|
|
-
-
Aaron


- Joined on 07-10-2007
- Posts 177
|
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?
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- Cambridge, MA
- Posts 2,257
|
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();
}
Tired of incompetent moderation? Wondering where all the clever discussion went? Try irc.slashnet.org #TDWTFMafia. We don't ban or kick and everyone is welcome.*
*Stupid people will be mocked mercilessly and encouraged to commit suicide, however.
|
|
-
-
GettinSadda


- Joined on 05-25-2006
- North-East Scotland
- Posts 243
|
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 it's 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 it's agents is illegal.
|
|
-
-
ammoQ


- Joined on 04-13-2005
- Vienna.Austria.Europe.Earth
- Posts 3,291
|
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
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- Cambridge, MA
- Posts 2,257
|
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.
Tired of incompetent moderation? Wondering where all the clever discussion went? Try irc.slashnet.org #TDWTFMafia. We don't ban or kick and everyone is welcome.*
*Stupid people will be mocked mercilessly and encouraged to commit suicide, however.
|
|
-
-
-
ammoQ


- Joined on 04-13-2005
- Vienna.Austria.Europe.Earth
- Posts 3,291
|
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
|
|
-
-
DonkeyRyan


- Joined on 07-10-2008
- Posts 4
|
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; }
|
|
-
-
DogmaBites


- Joined on 07-15-2008
- Posts 46
|
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.
|
|
-
|
|