How To View Inception Through Code

If you have not seen Inception yet, I highly recommend doing so before reading this post.


Inception Movie Poster

Inception combines the best elements of movies in the Simulism genre; such as Dark City, eXistenZ, The Thirteenth Floor and The Maxtrix Trilogy; and creates an original, thought provoking film.

Watching the film from the standpoint of a programmer, I could not help seeing the similarities between the plot devices and basic programming concepts.

The Kick

The Kick is the concept they use in the movie to return to a dream level above the existing one. For example, if you perceive that you are falling in a dream, you have a tendency to wake up.

The Kick can be portrayed in programming in several different ways:


public bool Dream()
     kick = CheckForKick();
     if(kick) return true;
     return Dream();

* CheckForKick() implementation has been omitted.

In this recursion example, each call to function Dream() will call another sub-Dream – until the CheckForKick() function returns true, which can occur however many levels deep.

Exception Bubble

public bool Dream()
                        throw new DreamException("Receive Kick");
                    catch(DreamException dx)
                        throw dx;
                catch(DreamException dx)
                    throw dx;
            catch(DreamException dx)
                // Handle Kick thrown 3 levels deep.

In this Exception example, an exception is thrown within a nested try block. Rather than handling the exception at that level, it’s bubbled up until is (hopefully) handled gracefully.


This can best be represented by the following infographic, which I reconstructed to be a stack. Here is the original source.

Inception Stack

A stack is one of the most fundamental data structures in computing. A stack operates in the following: items are added in a Last-In, First-Out (LIFO) manner. They are added via being “Pushed” onto the stack and removed via being “popped” from the Stack.

In the movie, each sub-dream is pushed onto the stack starting with reality. Our characters cannot return to a different dream level by popping the dream above off the stack.


They use Sedation in the movie to prevent the dreamers from waking up any means other than “falling” on all dream levels above the first. At the first dream level, the only way to wake up is for the machine’s time to elapse.

Sedation in the movie can be explained through the use of Threading.


public void Inception()
     Machine mach = new Machine();
     Sedation sed = new Sedation();
     Thread machineThread = new Thread(new ThreadStart(mach.Start());
     Thread sedationThread = new Thread(new ThreadStart(sed.Sedate());
     catch(Exception ex)
          // Limbo ???

* Machine implementation omitted. Sedation implementation below.

In this example, you can think of the sedation and starting the “dream machine” as being two separate threads – both induce unconsciousness, but in different ways.

The sedation occurs first as indicated by the sedationThread.Start(), followed by the machine start machineThread.Start(). The Sedation.Sedate() implementation may look like the following:

public void Sedate()
        Thread.Sleep(36000000); // 10 Hours

By calling join on the threads, the ReturnToReality() function will not get called until both the sedation wears off or the machine time elapses.


Limbo is defined in the movie has a mental state where your mind doesn’t know if you are in reality or not. The only way out is to realize that Limbo is not reality.

I already hinted at how Limbo could be represented in code in the previous example – if there is an exception (i.e. death) during the running of the threads that does not allow them to complete. Also, Limbo could also be represented by the following:

Infinite Loop or Infinite Recursion


Most of the time that we are programming and find ourselves in an infinite loop, we are not aware of it until we start to question:

  • Why is this taking so long?
  • Why is my memory decreasing?
  • Why is my CPU pegged at 100%

Fortunately in programming there are ways to identify that we are in an infinite loop state.

If you are not aware of these conditions, you will stay there until your program runs out of memory or crashes (dies) or you realize you are in a loop (meet a condition) – by calling the QuestionReality() function.


Ellen Page

The last item I will mention is the role of Architect. In the movie, Ariadne (Ellen Page) is responsible for creating the dreamscapes. This shares similarities with Software Architecture. As a software architect you responsible for knowing the systems (the main characters in movie) that you work with and to create the best solutions (e.g. Never Ending Staircase) to new problems that need to be solved (Inception). Being able to see the world around you as objects with state and behavior is a useful skill for both understanding Object Oriented Design/Programming; and as a Software Architect.

I was surprised how many people shared similar thoughts with comparing the movie with programming. If you enjoyed my post, please read their perspectives:

Tagged with: , , , , , , , , , , , , , , , , ,
Posted in Technical
12 comments on “How To View Inception Through Code
  1. Rick says:

    Nice post and at the same time pretty much sums up why Inception sucked, our dreams can’t be reduced to code, but Inception can. Nolan even proposes stabilized time in dreams, talk about reductionism.

  2. Max Loh says:

    Some of these analogies are a stretch, but the stack and thread ones make sense.

    @Rick Inception did not mean to portray machine-induced dreams as regular, everyday dreams. Machine-induced dreams are way crazier and involve multiple levels and sharing; it is not claiming that the normal dreams in its world have these special properties as well.

    That means you can’t apply any current knowledge of “normal dreams” to machine-induced dreams, because they are in Inception’s sci-fi mesh of “unknown technology.”

  3. dhirschl says:

    Max, thanks for your reply.

  4. logan says:

    well done sir

  5. Joe Darcy says:

    Another summary of Inception in code:

    “Understanding Inception using try-with-resources”

  6. Prabhakar says:

    I think similarly.
    I see everything as a recursion, where a kick is the terminating condition.

    Limbo is a special case where the recursive loop never terminates due because of the corruption of the terminating condition due to buffer overflow (lol)

    And sedation is the parameter passed to the recursive function that controls the recursion duration. Similar to your thread timeout logic. 😛

  7. kamalcom says:

    Nice comparison and nice movie :)

  8. dude says:

    Not really impressed with your logic…

    maybe a loop for checking the condition of the Kick?… and death…

    Movie had a “glitch” (in my opinion) how did they get the drug into dream? should have used some phsichy trick…

  9. There is a typo in step one of the stack, it says it uses FIFO, which is correct, but then explains that it is First-In, Last-Out (FIFO) manner. It Should be First-In, First-Out. Great Article.

  10. dhirschl says:

    Nice Catch. This has now been updated. Thanks.

  11. james says:

    I believe your explanation of the stack is flawed. Stacks are always LIFO (Last in, first out). Wikipedia has a good definition of how a stack works and how it is defined as a LIFO.

    “In a stack, the topmost item, which is added last, is taken out first. Hence a stack is a LIFO structure.”

    So in the Inception stack, we see that level 5 was “Last In” and is the first to be popped off the stack, hence LIFO. You should update the article to state that Inception follows the LIFO paradigm.

  12. dhirschl says:

    Thanks – you are correct.

5 Pings/Trackbacks for "How To View Inception Through Code"
  1. === === popular today…

    yeah! this story has entered the popular today section on…

  2. […] by admin on Saturday Jul 31, 2010 Under Zocials submitted by repeatgeek to programming [link] [5 comments] Social […]

  3. […] More programming link […]

  4. […] over at RepeatGeek wrote an article called How To View Inception Through Code, which compares the film to coding, which seem to be remarkably […]

  5. […] over at RepeatGeek wrote an article called How To View Inception Through Code, which compares the film to coding, which seem to be remarkably […]

Leave a Reply