• Registered (unregistered)

    My frist guess would be that Chris was trying to track down the reason the application was stuck in a mysterious state t18.

  • Michael R (unregistered)

    sM.put(tk(INVITED, ACCEPTED, reject), sp(FRIST, SECNOD)); // t18

  • (nodebb)

    The comments certainly don't help. Numbering lines isn't exactly what I want comments for.

    No, but they are probably references to entries in the document describing the transitions in the state machine. ("t15" for "transition 15", most likely.)

    The parameters to tk() are probably "old_state, new_state, event". I wouldn't like to speculate about sp(), though.

  • (nodebb)

    My vote is that sp() is more properly named the SideEffectPropagator()

  • (nodebb) in reply to Steve_The_Cynic

    Yeah, agree. Especially when it comes to state machines, describing transitions in a short way is often incredible difficult. Even if you want to be a vague as possible, you end up with either very long descriptions or some that describe the transition at all and are just a few hints to what is actually going on. So it's often best to index the transitions and document them fully in wherever the use cases/business cases are documented to get actually a full picture (hopefully with links to the relevant stories implementing it).

  • (nodebb)

    Reading code that should have been done with a state machine can be a lot worse. IMO.

  • (nodebb)

    The comments certainly don't help.

    Sure they do. I especially appreciate the comment which explains why "t18" is missing. [chef's kiss]

  • (nodebb)

    MaxiTB explained why state t18 is "missing". The transition labels index into a document which likely had to go through a long approval process. t18 was no longer necessary, and no one wanted to go through the approval process again just to renumber the transitions.

  • Jonathan (unregistered)

    Frist - State machines are always ugly to describe.

    Secnod - They are using case to differentiate between state names and transition arrow labels. I don't think it's awful.

    Thrid - Today's article title is the best I've ever seen in years and years of reading DailyWTF. It got an audible laugh and whoever wrote it (Remy, I assume) gets a boatload of kudos.

  • (nodebb) in reply to sibtrag

    Cool story. Still not commented. Next person to maintain that code is gonna wonder "what happened to 18?" and why should he have to dig through piles of documentation trying to find the explanation of why it was removed? Just add a single line "t18 no longer active, see xxxxx".

  • (author) in reply to Jonathan

    Usually I'm the punny title person, but no- this time the title came from Chris and I knew I couldn't top it.

  • MachineThatStates (unregistered) in reply to Steve_The_Cynic

    I'm pretty sure sp is the output state, the transitions appear to make much more sense that way than under your interpretation (e.g. invite in tk seems to imply INVITED in sp, same for reject -> REJECTED, cancel -> NONE etc.). Why there appear to be pairs of states on each side is a mystery

  • Klimax (unregistered) in reply to MachineThatStates

    Because it contains a state and previous action. PENDING (invitation), INVITED (to make invitation pending)

    So flow is as such: Initial state is NONE, NONE and "invite" -> PENDING, INVITED (or remove that is NOP?) users can cancel (for issuer), accept, ignore, block Say they ignore it INVITED, PENDING, ignore -> IGNORED, PENDING (past action: ignore, current status: PENDING) We get possible actions for this pair: invite (reissue?), cancel(issuer), remove (?), invite (duplicate?) Cancel and remove have same transition and NONE,NONE has both invite and remove transitions.

    So there are effectively three TRWTFs: duplication, no specific order in tk and clearly same namespace for state and previous action.

  • mihi (unregistered)

    My wild guess is that the states represent relationship between entities (think people on LinkedIn) and that a state transition can also act on and modify the "inverse" state. I.e. If I don't know Chris, neither he knows me, but I invite him, my relation to Chris moves to state PENDING while Chris relation to me becomes INVITED. And so on.

  • (nodebb)

    Having written similar code, I can see why it'd be done that way: Whatever interprets that is running through an array of binary data that encodes state transitions, conditions, and god knows what else. Using macros to set up the data in this way is a means of making it human-readable rather than just having a huge block of hex constants. It's using the C++? compiler as a state-machine compiler. So I wouldn't class this as a WTF at all.

  • Duke of New York (unregistered)

    Kind of a like a cloud / I was PENDING way up in the sky / And I was feeling ACCEPTED like you wouldn't believe / And sometimes I BLOCKED the feelings myself / And I REJECTED that I was ever coming down / Just then little dot INVITED my eye. / There was just about NONE to see. / But I IGNORED it way too long. / It was pulling me down.

  • (nodebb)

    I'm very surprised to learn the lowercase one are constants, which is a WTF indeed. Given the code, I would have expected them to be functions (function pointers, functors, delegates, etc.).

  • (nodebb) in reply to MachineThatStates

    It seems to be modelling some sort of call flow, and the states are the state of each endpoint of the call. So from NONE NONE I can "invite" and get to PENDING INVITED - I'm "pending" and the remote is "invited" i.e. "the phone is ringing". But the states must be reversed at the other end - I can only get out of PENDING INVITED by "cancel" which takes me back to NONE NONE. Most of the meaningful states seem to be reached from INVITED PENDING, but there's no way into that state. Which makes this a rather odd state machine as you've got two different entities that can move states, with different available actions. Possibly it's modelling a "telephone exchange" which is trying to connect one endpoint to another rather than being on an endpoint.

  • Klimax (unregistered) in reply to SteelCamel2

    See my now visible reply. There are 4 ways out of that state. Unfortunately, you have to look for both pairs and reverse pairs...

  • (nodebb)

    Is it even a standard if it's not standard?

  • Solomon Ucko (unregistered)

    This doesn't seem that bad to me. I guess replacing this with a few "if" statements could clarify the reasoning behind this, but it's probably documented somewhere else given that the transitions are all numbered. IDK what "tk" and "sp" might stand for, but I think this is for some kind of social media app, and the 5 values are two users' previous friendship/connection states, the action taken by one of the users, and the two users' new states.

  • Craig (unregistered)

    The one that makes me go hmm is t21. Why was there a need to specify that executing "remove" on "none, none" would go to "none, none"? The rest all seem pretty straightforward.

Leave a comment on “Pretty Little State Machine”

Log In or post as a guest

Replying to comment #684112:

« Return to Article