CodeSOD: Pretty Little State Machine

State machines are a powerful way to organize code. They are, after all, one of the fundamental models of computation. That's pretty good. A well designed state machine can make a complicated problem clear, and easy to understand. Chris, on the other hand, found this one. static { sM.put(tk(NONE, NONE, invite), sp(PENDING, INVITED)); // t1 sM.put(tk(REJECTED, REJECTED, invite), sp(PENDING, INVITED)); // t2 sM.put(tk(PENDING, IGNORED, invite), sp(PENDING, INVITED)); // t3 sM.put(tk(PENDING, INVITED, cancel), sp(NONE, NONE)); // t4 sM.put(tk(PENDING, IGNORED, cancel), sp(NONE, NONE)); // t5 sM.put(tk(PENDING, BLOCKED, cancel), sp(NONE, BLOCKED)); // t6 sM.put(tk(INVITED, PENDING, accept), sp(ACCEPTED, ACCEPTED)); // t7 sM.put(tk(INVITED, PENDING, reject), sp(REJECTED, REJECTED)); // t8 sM.put(tk(INVITED, PENDING, ignore), sp(IGNORED, PENDING)); // t9 sM.put(tk(INVITED, PENDING, block), sp(BLOCKED, PENDING)); // t10 sM.put(tk(ACCEPTED, ACCEPTED, remove), sp(NONE, NONE)); // t11 sM.put(tk(REJECTED, REJECTED, remove), sp(NONE, NONE)); // t12 sM.put(tk(IGNORED, PENDING, remove), sp(NONE, NONE)); // t13 sM.put(tk(PENDING, IGNORED, remove), sp(NONE, NONE)); // t14 sM.put(tk(BLOCKED, PENDING, remove), sp(NONE, NONE)); // t15 sM.put(tk(PENDING, BLOCKED, remove), sp(NONE, BLOCKED)); // t16 sM.put(tk(NONE, BLOCKED, invite), sp(PENDING, BLOCKED)); // t17 sM.put(tk(IGNORED, PENDING, invite), sp(PENDING, INVITED)); // t19 sM.put(tk(INVITED, PENDING, invite), sp(ACCEPTED, ACCEPTED)); // t20 sM.put(tk(NONE, NONE, remove), sp(NONE, NONE)); // t21 sM.put(tk(NONE, BLOCKED, remove), sp(NONE, BLOCKED)); // t22 sM.put(tk(BLOCKED, NONE, remove), sp(NONE, NONE)); // t23 } Honestly, I only know this is a state machine because Chris told me. I could hazard a guess base on the variable name sM. The comments certainly don't help. Numbering lines isn't exactly what I want comments for. I don't know what tk or sp are actually doing. So yes, this is an unreadable blob that I don't understand, which is always bad. But do you know what elevates this one step above that? If you note the third parameter to the tk function- invite, cancel, accept, etc? Those are constants. So are INVITED, PENDING, ACCEPTED. While I am not fond of using the structure of a variable name to denote its role, "caps means const" is a very well accepted standard. A standard that they're using sometimes, but not all the time, and just looking at this makes me grind my teeth. [Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!

Sep 8, 2025 - 13:00
 0  2
CodeSOD: Pretty Little State Machine

State machines are a powerful way to organize code. They are, after all, one of the fundamental models of computation. That's pretty good. A well designed state machine can make a complicated problem clear, and easy to understand.

Chris, on the other hand, found this one.

  static {
    sM.put(tk(NONE, NONE, invite), sp(PENDING, INVITED)); // t1
    sM.put(tk(REJECTED, REJECTED, invite), sp(PENDING, INVITED)); // t2
    sM.put(tk(PENDING, IGNORED, invite), sp(PENDING, INVITED)); // t3
    sM.put(tk(PENDING, INVITED, cancel), sp(NONE, NONE)); // t4
    sM.put(tk(PENDING, IGNORED, cancel), sp(NONE, NONE)); // t5
    sM.put(tk(PENDING, BLOCKED, cancel), sp(NONE, BLOCKED)); // t6
    sM.put(tk(INVITED, PENDING, accept), sp(ACCEPTED, ACCEPTED)); // t7
    sM.put(tk(INVITED, PENDING, reject), sp(REJECTED, REJECTED)); // t8
    sM.put(tk(INVITED, PENDING, ignore), sp(IGNORED, PENDING)); // t9
    sM.put(tk(INVITED, PENDING, block), sp(BLOCKED, PENDING)); // t10
    sM.put(tk(ACCEPTED, ACCEPTED, remove), sp(NONE, NONE)); // t11
    sM.put(tk(REJECTED, REJECTED, remove), sp(NONE, NONE)); // t12
    sM.put(tk(IGNORED, PENDING, remove), sp(NONE, NONE)); // t13
    sM.put(tk(PENDING, IGNORED, remove), sp(NONE, NONE)); // t14
    sM.put(tk(BLOCKED, PENDING, remove), sp(NONE, NONE)); // t15
    sM.put(tk(PENDING, BLOCKED, remove), sp(NONE, BLOCKED)); // t16
    sM.put(tk(NONE, BLOCKED, invite), sp(PENDING, BLOCKED)); // t17
    sM.put(tk(IGNORED, PENDING, invite), sp(PENDING, INVITED)); // t19
    sM.put(tk(INVITED, PENDING, invite), sp(ACCEPTED, ACCEPTED)); // t20
    sM.put(tk(NONE, NONE, remove), sp(NONE, NONE)); // t21
    sM.put(tk(NONE, BLOCKED, remove), sp(NONE, BLOCKED)); // t22
    sM.put(tk(BLOCKED, NONE, remove), sp(NONE, NONE)); // t23
  }

Honestly, I only know this is a state machine because Chris told me. I could hazard a guess base on the variable name sM. The comments certainly don't help. Numbering lines isn't exactly what I want comments for. I don't know what tk or sp are actually doing.

So yes, this is an unreadable blob that I don't understand, which is always bad. But do you know what elevates this one step above that? If you note the third parameter to the tk function- invite, cancel, accept, etc? Those are constants. So are INVITED, PENDING, ACCEPTED.

While I am not fond of using the structure of a variable name to denote its role, "caps means const" is a very well accepted standard. A standard that they're using sometimes, but not all the time, and just looking at this makes me grind my teeth.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow