View Single Post
Suggestion - Get rid of the complex nesting.
Posts: n/a
Lightbulb Suggestion - Get rid of the complex nesting. - 11-09-2006, 10:29 PM

I hadn't looked at that problem before reading your posting - but it's a poster child for the "stepwise refinement" approach the text emphasizes. Don't get locked up in a batch of nested loop/while statements - at least at first. I believe the problem itself gives you a hint when it says "you may want to use several methods."

I just put together some quick code where the most complicated nesting is one if statement in a method that is called "while the world is running" and which basically controls the motion of the fan based on two world level variables - powerOn and Speed.

I made sure this worked first by manually setting the variables and running the world - then did the rest of the control with a set of statements similar to those in Fig. 5-2-3. Won't claim this approach worked perfectly the first time - but the problem was separated down enough that it was easy to see the problems and fix them.

Note: One thing to watch out with this problem is to be sure you have the blades rotating at a slow enough speed, even on high, that the display driver can handle it - otherwise funny things can happen. (I'd suggest no more than 3 rps maximum. The actual maximum is dependent on your specific hardware but very few drivers even physically refresh the screen faster than 10-12 frames per sec.)
Reply With Quote