Thursday, April 14, 2011

Passing Parameters by Reference

So, I was struggling with how to managed a cascading fall – fall of a stick as far as it can fall. I was going to use the return value of true to indicate that more falling was possible.

This turned out to contradict other parts of the design (see the previous post and all the hair on the floor – you know, the hair I pulled out when I was trying to figure out how I had broken the system.)

Then I though, for the cascade, I would have to create a different method that would return the final position of the fall. That would be used in the next call to the fall method. I would lose true/false as a return value, but I could just compare the original position with the new position. If they were equal, then that's the same as false.

But as I sat down to work on this post, I thought, what if I passed the location value by reference? If I did, then the calling method would always have access to the current value of the stick (provided that is reset in the method) but still have the same return value to signify success or failure.

So, I'm going to go give it a try.


I started by just inserting the ref keyword into the fall method signature. This gave me a world of errors. I know how the ref keyword works – it implements a pointer. I've never done this in C#, though, and it turns out that you need to put the ref keyword in the method calls as well. This makes sense, of course. The compiler is using ref as the asterisk for defining a pointer. (or maybe the ampersand – pointers are still a little hard for me.)

And that time, it compiled.

But does it work? I will run tests.

Yes, it does work. It passes both the original tests and the fall method tests.

So, next, I need to re-write the fall method to change the location variable to be the output location of the fall method – AND I NEED POWERSHELL CODE THAT TESTS THAT.

(Considering the frequency with which I forget to use semicolon at the end of a C/C# line of code, you'd think I'd dislike them more.)

This is the code that I changed:


doMove(position[0], position[1]);

location = position[1]; // set location to the new position

return fall(ref location);

Location is now passed to the iteration of the fall method. This change needs to be made in several places, along with this change:


if (!isFilled(position[2], board))

{

location = position[2]; //// set location to the new position

return doMove(position[1], position[2]);

}


Running this code gives me three FAILs:

FAIL: Stick from position 6 did not fall to expected position 12.

FAIL: Stick from position 8 did not fall to expected position 14.

FAIL: Stick from position 8 did not fall to expected position 14.

The fact that this is just three FAILs suggests that I have a minor mistake in the code. I'll take a look:


The only fall cases that this applies to are the position[0], position[1], position[2] chains.

I added a call to the display function to see where things are ending up:


o o o o o o

o o o o o o

|

o o o o o o

o o o o o o

o o o o o o

o o o o o o

FAIL: Stick from position 6 did not fall to expected position 12.

o o o o o o

o o o o o o

| |

o o o o o o

o o o o o o

o o o o o o

o o o o o o

This can't happen. The final position here is 18. That's not a valid move position for this node. That's node 7, not node 6.


OK, back to the code.


This is the change that I made to get it to pass the tests:


else if (position[3] == location)

{

if (!isFilled(position[2], board))

{

location = position[2]; //// set location to the new position

return doMove(position[3], position[2]);

}

To this:


else if (position[3] == location)

{

if (!isFilled(position[2], board))

{

doMove(position[3], position[2]);

location = position[2]; //// set location to the new position

return true;

}


I'm not sure I understand why this failed. The doMove method must have some other side side-effect, but the only candidate I can find is:


board.getStick(origin).Location = destination;


OK, well, if I can trust the tests, things are working.

More tomorrow.

Oh, I need to write tests to make sure that the reference is returning the expected value. All I know is that the methods are returning the expected true/false value.

No comments:

Post a Comment