Emotions as State Machines

by Coraline Ada Ehmke, a software developer, open source advocate, and Greater Than Code panelist.


In her book The Art of Empathy, researcher Karla McLaren describes emotions as “neurological programs that require action.” This metaphor may resonate with us as programmers and provide a foundation for developing critical skills in identifying and positively expressing a rich range of emotions in healthy response to our interactions with other people in our lives.

In this article, we’ll be using the DSL (domain specific language) of the Ruby gem aasm (acts as state machine) to define and help us understand this model of emotions. Identifying the trigger of an emotional state is critical in determining and acting toward the desired resolution for a given feeling. There are choices we make along the way that determine what will happen, and making these choices consciously as opposed to following our first reactions can make the difference between having a healthy response or exacerbating a situation.

Anger

Anger is triggered when we feel that some sort of boundary has been violated. This might be the result of criticism of ourselves or our work, an interpersonal conflict, a disregard for our feelings, or a situation in which our sense of self-worth has been questioned.

There are two possible outcomes from anger, each determined by the method we use to address the feeling. The healthy response is to acknowledge the situation and work to restore the violated boundary, either by internally reassuring ourselves about our self-image or working to right an external wrong. The unhealthy response, lashing out, can only lead to an intensification of the emotion and potentially leave us with long-standing resentment.

Coded as a state machine, the states and transitions might look something like this:

aasm do

  state:triggered, initial: true
  state:angry
  state:acknowledged
  state:restoration
  state:lashed_out
  state:resentment

  event:trigger do
    transitions  from::triggered, to::angry
  end

  event:acknowledge do
    transitions  from::angry, to::acknowledged
  end

  event:restore do
    transitions  from::acknowledged, to::restored
  end

  event:lash_out do
    transitions  from:angry, to::lashing_out
  end

  event:internalize do
    transitions  from:lashing_out, to::resentment
  end

end

Guilt

Guilt is a response to having inadvertently caused hurt to someone else, or ourselves, through our actions or inaction. We can experience guilt as a consequence of having lashed out at someone, having said something hurtful or insensitive, or violating our internal code of conduct in some way.

The outcome of feeling guilty can either be continued (or even shared) pain, or making amends for our actions. The default reaction of doing nothing leads to a dismal outcome, while acknowledging our fault and perhaps pledging to do better in the future leaves us (and others) feeling better about the situation.

aasm do

  state:triggered, initial: true
  state:guilty
  state:acknowledged
  state:atonement
  state:inaction
  state:shared_pain

  event:trigger do
    transitions  from::triggered, to::guilty
  end

  event:acknowledge do
    transitions  from::guilty, to::acknowledged
  end

  event:externalize do
    transitions  from::acknowledged, to::atonement
  end

  event:do_nothing do
    transitions  from:guilty, to::inaction
  end

  event:internalize do
    transitions  from:inaction, to::shared_pain
  end

end

Fear

Fear arises when we are confronted with a novel or unexpected circumstance. Perhaps we are being put on the spot in a hostile interview situation, finding our job situation changing rapidly, or having to speak in front of an audience of our peers.

Regardless of the trigger, our response to this situation is critical. We have the choice of following our (probably primitive) instincts, or using our emotional intelligence to better prepare ourselves for dealing with the situation and achieving the desired outcome.

aasm do

  state:triggered, initial: true
  state:fearful
  state:acknowledged
  state:fight_flight_freeze
  state:reorientation

  event:trigger do
    transitions from::triggered, to::fearful
  end

  event:acknowledge do
    transitions from::fearful, to::acknowledged
  end

  event:react_conciously do
    transitions from::acknowledged, to::reoriented
  end

  event:externalize do
    transitions from:fearful, to::fight_flight_freeze
  end

end

Anxiety

Anxiety is a natural reaction to change. These changes may include being given a new assignment, finding ourselves in a new working environment, or being challenged to learn a new language. We may question our ability to cope with a new situation or a demand on our attention. Anxiety, therefore, can be a symptom of a lack of self-confidence.

Our reaction to anxiety can make a difficult situation better or worse. Anxiety is a signal that we must take action to avert a bad situation in which we fail to perform to expectations or miss an important deadline. Failing to take action may result in the heightening of our anxiety or even eventually lead to fear as we put ourselves in an even worse situation.

aasm do

  state:triggered, initial: true
  state:anxious
  state:organized
  state:completed
  state:procrastination
  state:fear

  event:trigger do
    transitions from::triggered, to::anxious
  end

  event:acknowledge do
    transitions from::angry, to::organized
  end

  event:act do
    transitions from::organized, to::completed
  end

  event:internalize do
    transitions from:anxious, to::procrastination
  end

  event:worry do
    transitions from:procrastination, to::anxiety
  end

  event:do_nothing do
    transitions from:procrastination, to::fear
  end

end

Confusion

Sometimes a change in a situation or a demand for action can be overwhelming, leading to the unpleasant emotion of confusion. We may be confused about an assignment, the correct way to move toward a solution, or a new piece of information that someone has shared.

Dealing with confusion in a healthy way leads to the best outcome. Allowing ourselves to give in to feeling overwhelmed will result in either feeling apathetic toward the situation (which can cause bigger problems) or becoming anxious.

aasm do

  state:triggered, initial: true
  state:confused
  state:clear_headed
  state:enlightened
  state:overwhelmed
  state:apathetic
  state:anxious

  event:trigger do
    transitions from::triggered, to::confused
  end

  event:acknowledge do
    transitions from::confused, to::clear_headed
  end

  event:clarify do
    transitions from::clear_headed, to::enlightened
  end

  event:ignore do
    transitions from:confused, to::overwhelmed
  end

  event:do_nothing do
    transitions from:overwhelmed, to::apathetic
  end

  event:do_nothing do
    transitions from:overwhelmed, to::anxious
  end

end

Jealousy

Jealousy occurs when an important connection to something we care about is challenged. We may, for example, consider ourselves strongly connected with being the most creative person when it comes to solving a problem in a given domain, and feel triggered when someone else gets credit for doing something like this. Jealousy is usually interpreted as a negative or unhealthy emotion, but it can have the positive outcome of challenging us to better ourselves and thus improve our connections.

If we allow ourselves to react thoughtlessly to jealousy, we may end up feeling betrayed by the success of our peers, come to question our self-identity, or even rise to anger. But by identifying jealousy as a positive challenge, we can identify the affected connection and work to heal, restore, or even strengthen it.

aasm do

  state:triggered, initial: true
  state:anxious
  state:organized
  state:completed
  state:procrastination
  state:fear

  event:trigger do
    transitions from::triggered, to::anxious
  end

  event:acknowledge do
    transitions from::angry, to::organized
  end

  event:act do
    transitions from::organized, to::completed
  end

  event:internalize do
    transitions from:anxious, to::procrastination
  end

  event:worry do
    transitions from:procrastination, to::anxiety
  end

  event:do_nothing do
    transitions from:procrastination, to::fear
  end

end

Envy

Envy is closely related to jealousy and one may often be mistaken for the other. The important difference is that jealousy is triggered when a connection we have to an idea or outcome is challenged by someone else asserting a similar connection, while envy is the desire to have the same connection to something that someone else does.

Envy, when processed properly, can lead us to recognize a gap between our desire and our ability, and can improve our sense of self by accomplishing the feat of bridging this gap. If allowed to fester, it can lead to heightened insecurity and a damaged sense of self.

aasm do

  state:triggered, initial: true
  state:envious
  state:motivated
  state:improved_sense_of_self
  state:insecure
  state:damaged_sense_of_self

  event:trigger do
    transitions from::triggered, to::envious
  end

  event:acknowledge do
    transitions from::confused, to::motivated
  end

  event:grow do
    transitions from::motivated, to::improved_sense_of_self
  end

  event:ignore do
    transitions from:envious, to::insecure
  end

  event:do_nothing do
    transitions from:insecure, to::damaged_sense_of_self
  end

end

Sadness

Sadness results from an unmet expectation, such as when we thought that something we worked on would be accepted, or that a person whom we admired would respond to our reaching out to engage with them. Sadness is situational and requires careful attention to offset our tendency to dwell.

Dealing with sadness in a positive way means letting go of our expectations and re-grounding ourselves in the reality of a situation. Dwelling on our sadness, or holding onto unrealistic or unmet expectations, can lead to more sadness or even trigger a depressive state.

aasm do

  state:triggered, initial: true
  state:sad
  state:released
  state:grounded
  state:holding_on
  state:depressed

  event:trigger do
    transitions from::triggered, to::sad
  end

  event:process do
    transitions from::sad, to::released
  end

  event:move_on do
    transitions from::released, to::grounded
  end

  event:internalize do
    transitions from:sad, to::holding_on
  end

  event:do_nothing do
    transitions from:holding_on, to::depressed
  end

  event:reinforce do
    transitions from:holding_on, to::sad
  end

end

Happiness

We all have a desire to be happy. Happiness is the result of a novel occurrence, whether it’s being recognized for our work or finishing an assignment ahead of a deadline.

Our reaction to the event that triggered our feelings of happiness is situational, sometimes calling for an outward expression of our feeling (a shout of joy, laughter, or just a big goofy smile) and other times leading instead to a feeling of internal delight.

Happiness can also take a negative form when combined with envy or jealousy, however, in the form of “schadenfreude”, or pleasure at someone else’s failure. How we react in this situation can be an indicator of our own sense of self-worth.

aasm do

  state:triggered, initial: true
  state:happy
  state:expressive
  state:invigorated
  state:suppressed
  state:delighted

  event:trigger do
    transitions from::triggered, to::happy
  end

  event:externalize do
    transitions from::happy, to::expressive
  end

  event:share do
    transitions from::expressive, to::invigorated
  end

  event:internalize do
    transitions from:happy, to::suppressed
  end

  event:do_nothing do
    transitions from:suppressed, to::delighted
  end

end

Other Emotions

The vocabulary of human emotions is almost unimaginably rich. This article has addressed the more common emotions but the principles laid out apply to the entire universe of feelings. In all cases, it’s critical to identify the trigger for an emotion and make a reasoned response based on our desired outcome. Allowing the primitive brain to react for us can make a good situation bad, or a bad situation even worse. Applying our emotional intelligence ensures that every emotion has the potential to teach us something about ourselves or other people in our lives.

Leave a Reply

Your email address will not be published. Required fields are marked *