Smart Trains 2

ST 14 Keeping Edge Cases at the Center

In an earlier post, we looked at the stopping mechanism of the Train state machine. In particular, the substate AutoMotorRest stops the train gradually by sending the event MOTOR_RUN_REQ with the target speed 0 and default deceleration.

Our fictional program manager comes again and says “It’s nice to have one magnet trigger a gradual deceleration when approaching a station, but it’d be nicer to have a two-stage deceleration using two magnets to improve accuracy. So, here are the modified requirements:

  1. The first magnet triggers the same ARRIVING event causing the train to decelerate gently to a slow coasting speed.
  2. The train runs at the coasting speed as it approaches the end of a station.
  3. The second magnet triggers a new ARRIVED event causing the train to decelerate to a complete stop.

The above steps are pretty straight-forward. However, if we implement them with a sequential mindset, it’d be easy to miss some cases commonly known as “corner cases” or “edge cases”. What if the train hasn’t reached the coasting speed when the second magnet is detected? What if the second magnet is not detected for a long time?

As usual we update our statecharts first when requirements are changed. Since the steps above clearly map to some state behaviors (the needs to wait), we add three substates in AutoMotorRest corresponding to each of the steps, namely AutoMotorSlowing, AutoMotorCoasting and AutoMotorStopping. See the refinement from the first to the second diagrams below.

Drawing the states out forces us to think about all possible cases at design time. If the ARRIVED event is received anywhere other than the AutoMotorCoasting substate, the train breaks rapidly with a higher deceleration (first what-if above). If the ARRIVED event is not received before timeout, it triggers fault handling via the MISSED_STOP event (second what-if above).

With statecharts, corner cases and edge cases aren’t really at the corner or edge anymore. They are right at the center of our design. The third diagram shows the new overall design of the Train state machine.