On 22.12.16 16.58,
foru...@gmail.com wrote:
> The method DetermineAutoPilotMode is called at interval. FlightMode and DeviceID are determined at run time and is static through flight. SpaceShipMode and AutoPilotMode are dynamic - i.e change during the flight trajectory. I've scanned a handful of articles on how to restructure a design predicated on switch statements but not sure how the solutions apply to this case. I know switch statements are not necessarily bad but any recommendations on how to restructure this (design patterns ....) would be appreciated.
AFAICS you try to model several dependent state machines.
From the performance point of view there is nothing wrong with switch.
But doing this in Spaghetti code is error prone. If you miss to
synchronize one of the mode variables on an unusual mode transition the
system could end up in an inconsistent state, turning into undefined
behavior at least at application level. And if you miss to handle one
combination in one of the sub paths the implementation could be
incomplete, again UB.
First of all you should split the different levels of operation into
different functions. This makes the code more readable.
Furthermore calling a function /Determine/AutoPilotMode that depends on
AutopilotMode is at least misleading. Actually the implementation is
unstable, because it toggles between Decorrelation and Terminal.
DeviceID sound like something that should not be hard coded at all. But
it is not clear from your example what the intention behind DeviceID is.
At the next step it could be useful to separate the different state
machines. I.e. Some code controls the SpaceShipMode and provides actions
for state transitions. Another code manages the AutopilotMode including
transitions as well. E.g. it is probably valid to combine AutoPilot Off
with Launch. If you do so, then there is no more need to /determine/ the
AutopilotMode at all. It is just there.
Maybe the autopilot handler does not exist at all if it is turned off.
In this case the mode Off is superfluous.
Just a few suggestions,
Marcel