How Using State Diagrams Can Make You a Better Web Coder
In Articles by Raj DashOne tool that should be in your web coding arsenal is the state diagram. A state diagram shows the various "states" (reactions) that your application (e.g., web site) can be in, as well as what action or input (from the user) is necessary to get to a specific state. A state diagram helps to make concrete in your mind all of the possible user/application interactions. This increases the chances that you'll at least consider coding for all possibilities simply because you now know them all - which reduces the chances that you have buggy code or worse, forgot to handle specific functionality.
Why Use a State Diagram?
A state diagram consists of two components: circles to represent states (application reactions/ output) and arrows to represent transitions (user actions/ input). State diagrams are very handy for complex and not so complex computer applications of any sort. However, when it comes to website development, state diagrams are not always of use:- If you have a static website where the navigation guarantees that every single page links to every other page, then a state diagram is redundant. (In the branch of mathematics called Graph Theory, this situation is known as a "fully connected graph". A graph is a set of edges and nodes - i.e., transitions and states.)
- If you have a dynamic website running on a CMS (Content Management System) - which includes blog platforms - then so much of the state transitions are already coded into your site. So once again, a state diagram is not of much use.
- Handling special user input, where what they select decides the next state. (E.g., forms or menus that have drop-down or dynamic lists.)
- AJAX-y sites where even after a significant user action, the URL does not change. (Content does, URL doesn't.)
How Do You Create State Diagrams?
The surprising thing is that state diagrams are not all that complicated to produce. Yet in my experience, they are not used all that often by most programmers, despite the benefits (deeper understanding of user interactions; cohesion of coding effort). I swear by them - or did when I was coding every day in various jobs. It's recommended that you do produce state diagrams on paper first, then transfer them to a digital copy if and only if that's necessary. (There's something about the tactility of sketching input/display relationships on paper that makes them more concrete in your mind, making it easier to accomodate all possible interactions and thus reduce bugs in your applications.) A state diagram consists of:- Labeled arrow lines to show user input/action (transition).
- Labeled circles to show the resulting display of content (application state).
- Start state with a double-border circle.
- End states (but not when the application is web-based. See below for an explanation.)
You can see a simple example directly below - which will be expanded on later in this article:

Here are the steps for creating state diagrams (with a leaning towards website-based applications):
- Make a list of all possible inputs by the application user, from every single state.
- Draw the Start state - a double-border circle labeled with "S". With a web site, the start state is the home page and its default display.
- Draw circles for possible unique state or sub-state. For static sites, each web page is a separate state. If for your web app can have different sub-states for the same URL, then you need to draw separate state circles.
- For each possible action from the start state, draw labeled arrows (transitions) to the next possible state's circle.
- Repeat this from each state until you have a full state diagram for the application.
- Don't forget about circular transitions. E.g., from one state back to itself (possibly because of re-clicking the same link twice).
- Repetitive transitions from a cluster of related states can be represented with some short form, as will be discussed below.
- State diagrams for non-Web applications almost always have an "End" state. However, the Web is said to be "stateless" because in a web browser, every single state is an end state. You can take an action and leave, or take another action and then leave, etc. So for web applications, there's no need to draw the End state.
How Do State Diagrams Apply to Website-based Applications?
For a static website that uses no AJAX-y features (i.e., any feature where the URL does not change), a state diagram is fairly easy to produce but generally unnecessary. For example, a static website in which every page is connected to every other page results in a state diagram that is sometimes referred to as a "fully-connected" graph. Such state diagrams are usually pointless because there's no special handling to visualize. Every state is connected to every other state) Where state diagrams are most handy is for dynamic sites - especiall those with AJAX-y features (such as drop menus, sliders, accordions, galleries, etc.). In the latter case, the URL in the browser might not change but the page content does so partially. It's harder to visualize all possible states and transitions (actions) because a "page" can have multiples sub-states. A state diagram (or set of increasingly detailed diagrams) comes in very handy in this case - especially if there are a team of coders (and sometimes designers) to work with.An Example of State Diagram Use
In an upcoming tutorial I'm going to show you how to code the jQuery for two effects I'm using in my AboutMe template. The live page has a few CSS glitches that need to be ironed out first. I'd also like to add some more jQuery-based features if there's enough time. These additional features will be the subject of our example.

In the future, this template will turn into a free WordPress theme aimed at freelancers that want to showcase their work (gigs) experience. For now, I want to show how state diagrams can help you understand the necessary cause/reactions (aka input/transitions) for the "Current Gigs" gallery seen above. Understanding the necessary transitions helps you to code more confidently, and it's all coding language-independent. So you can decide on code library/language after creating the state diagram.
Desired Application Features
If you look at the "Current Gigs" gallery at the center left of the image above, or on the live page, you'll see that this is essentially the same concept as an image gallery. Click a link and details of that "gig" will appear. However, when I built the live page, there were no jQuery tutorials on how to throw text into the mix, for each "frame" of the showcase. I had to come up with my own code.
To do that, I had to first understand all possible user interactions. A state diagram is ideal for this. Let's up the ante, though. What I really wanted is a showcase area that shows both Current Gigs and Past Gigs. It's sort of like a visual C.V. (Curriculum Vitae, aka "resume"), showing a gallery of work experience. Each gig's frame contains a screen snap of the associated site's home page, along with some text giving details of the work I did/ am doing there. For now, the page only has "Current Gigs", but should have "Past Gigs" as well. Here is a list of the functional requirements for what the showcase area should have:
- Tabbed jQuery interface, but "invisible". Instead of using regular tabs, use mini-banners similar to the "Current Gigs" graphic.
- When a "tab" is clicked (Current Gigs, Past Gigs), the appropriate gig list is shown, along with the frame (details) of the first item.
- The default showcase is "Current Gigs".
- When someone clicks "Past Gigs", then the past gigs list must show, along with the details frame of the first item in that list.
- Gigs lists. Each "tab" will provide a list of gigs positioned to the left using a Blueprint CSS grid.
- The gig list items will be text links.
- Each showcase will have entirely different links (past and present work). So a "job experience" can only appear in one showcase at a time.
- When an item in a gig list is clicked, the gig's details "frame" will appear. Each frame will show a screen snap (previously saved) and the associated job description. The Blueprint CSS grid framework will be used to position the showcase elements. (This isn't necessary, but that's what I'm aiming for.)
So to reiterate, the goal is to have a tabbed interface where the tabs are labelled "Current Gigs" and "Past Gigs". This allows for more tabs later, limited only by the width of each label and the width of the showcase space (590 pixels). But I want the tabs to be "invisible", as mentioned. Instead of the typical tabs in a tab interface, I want to use mini-banners. If you look at the live test page, there's a mini-banner labeled "Current Gigs", and another labeled "Past Gigs". Those will be the tabs. Here is a closeup screen snap of what the final showcase area might look like, with default settings.

Creating the State Diagram
To create the state diagram, we first have to enumerate every possible unique state, input and action:- Start state: Upon load of the home page:
- Hide (non-display) all gig frames using CSS.
- Present "Current Gigs" showcase as default.
- Within the default showcase, present the frame for the 1st item in gig list as the default. So whenever someone clicks the Current Gigs "tab", the showcase will reset itself.
- Possible generic actions from the Start state:
- Click on "tab". Reaction/ transition: render the showcase corresponding to the tab clicked.
- Click on a gig list item. Reaction/ transition: render the corresponding gig item frame.
- Possible generic actions from other states: exactly the same. We're fortunate in this case, because this makes the state diagram so much simpler.
Note: At this point, we're only concerned with what's happening in the C.V. showcase. In the state diagram, we don't care about actions that affect other parts of the web page. We're only showing actions/ reactions that affect the C.V. showcase. Here is a sample state diagram for the above functionality.

A few notes about this diagram:
- For the purposes of this discussion, it's not all that important what each label actually is. Here, each one represents a website that I am either writing for now or used to write for.
- It's not as complicated as it looks. Just focus on one transition at a time and it'll be clear what's going on. [Here, the action labels are the same as the state labels. That's not always the case.] It's usually a lot clearer when you draw the diagram yourself, adding new state circles and transition arrows one at a time.
- Transitions, aka user actions, are represented by labelled arrows. (Normally the labels would be full text, not the short forms used here.)
- States, aka reactions, are represented by circles. The start state is always marked with a double circle and an "S".
- Short forms are used for both types of labels, to keep the diagram less cluttered.
- The states and sub-states are color-coded based on whether they are primary or secondary in nature. For example, blue represents primary states (and transitions). You can go from Start state to any blue state with a single click on the appropriate link. You cannot go to a purple (secondary) state from Start without first passing through a primary state.
- Because there is a lot of repetitive transition (i.e., from any gig item to another), groups of transitions are shown with one of the thick outlined arrows (blue or purple fill). For example, while you're viewing the FF (FreelanceFolder) gig details, you can click on any of the gig items listed for the CG (Current Gigs) showcase, including FF itself. Or you can click on the CG "tab" and reset the showcase. You cannot go from a "current" gig frame to a "past" gig frame, nor vice versa.
- The short, outlined blue arrow includes transitions back to CG's default state. The short, outlined purple arrow does not include transitions back to PG's default state. (That's because those transitions are already shown explicitly. They were not for CG because the diagram would be way too cluttered.)
Final Thoughts
Overall, you should now have a clearer mental picture of how the showcase section functions. I'm not going to get into how to move from a state diagram to actual code. That should become evident once you understand all the user interactions. State diagrams - and your understanding of them should help you to write more cohesive code, reducing the chances of a buggy application. Your coding technique does not change.Comments
Leave a CommentAdd a Comment












Shane
June 8th, 2008
An interesting post, and a brave choice - not because the quality isn’t there, because it is, but because it isn’t a ’sexy’ tutorial.
However, state diagrams and other process elements are crucial to non-trivial web projects, and worth learning more about.
Thanks for posting, and let’s hope we have more like this one.
D. Carreira
June 9th, 2008
Another great tutorial!
Thank you for sharing this,
David Carreira
yens
June 9th, 2008
What program are you using for the diagram?
Taylor Satula
June 9th, 2008
Wow this is a really good tut, We Need More Tuts. I Love Em’
Rijalul Fikri
June 9th, 2008
still confuse though
Ben Griffiths
June 9th, 2008
Very interesting, and original article, thank
Mikael
June 9th, 2008
I will backup Shane on this one. Not too sexy but necessary.
Lamin Barrow
June 9th, 2008
This concept is what is referred to as UML (Unified Modeling Language) in software engineering. In my own opinion the right place for this article is to be at nettuts since it is almost useless for graphic design professionals.
Lamin Barrow
June 9th, 2008
Oh sorry please disregard my above comment. I thought i was at psdtuts… LOL
Joefrey Mahusay
June 9th, 2008
Wow nice article although I am a little bit confused. Thanks for sharing.
Shane
June 9th, 2008
lol@Lamin
trs21219
June 9th, 2008
haha nice ladmin. im thinking your like me and have psdtuts, nettuts, and freelanceswitch open all the time
Stefan
June 9th, 2008
Thanks Raj!
Rich
June 9th, 2008
OMG System Analysis and Design Class………. wow state diagrams =P
Ahmad Alfy
June 9th, 2008
@Lamin
OMG ROTFLOL!!!!
Zach
June 9th, 2008
YES! I have been looking for something like this to help me with my development process. Freaking awesome, hot diggity. First Kucinich presents Articles of Impeachment against Bush, and now this. What a day!
Zach
June 9th, 2008
Out of curiosity, what program was used to make the ‘attention-getter’ picture’s state diagram?
Robin
June 9th, 2008
“Out of curiosity, what program was used to make the ‘attention-getter’ picture’s state diagram?”
Definitely not something from Microsoft
Most likely Illustrator.
Nico
June 9th, 2008
Very interesting. Thanks!
Zach
June 9th, 2008
@Robin
I thought perhaps there was an actual program that let you create these diagrams and displayed them in the way pictured.
Braden Keith
June 9th, 2008
I find this uterly useless for graphic designers… I think it would better be suited at nettuts.com rather than here.
Oh wait, did someone already pull this? My b. haha
Lamin Barrow
June 10th, 2008
@Robin
You can use Microsoft office Visio to make those diagrams or even the new office word 2007 or any UML design tool. It is certainly possible to create those in Adobe Illustrator but that will call for more complications than necessary.
Andrew Pryde
June 10th, 2008
Im not sure about this one but it is nice to see another tutorial we don’t get enough here at nettuts like they do over at PSDTUTS
Andrew
Braden Keith
June 10th, 2008
@Andrew
I feel the same way. But you got to keep in mind they’re just starting out and it’s not like they can just pull their photoshop writers to write a nettuts. I hope to start seeing one on a daily basis like psdtuts.
Raj Dash
June 10th, 2008
Thanks, everyone, for the comments.
As mentioned by a few commenters, this is definitely not a sexy post nor topic. But I did teach state diagrams under a “Finite Math for Computer Scientists” Lab in college, and most students said they understood coding better as a result. I know that it helps me.
Lamin: State Diagrams have nothing to do with UML. At least they didn’t at one time. They’re part of Graph Theory and pre-date UML, to the best of my knowledge. I know you’re just having some fun, but state diagrams ARE “useful” to graphic designers who code websites. Even just to understand that such and such a state requires, say, a different graphic banner than another state. Anyone who’s part of a team building dynamic websites should at least understand a basic state diagram. Now that said, you CAN probably produce a state diagram using UML tools. I mean, it’s just circles and arcs
Yens, Robin: The diagrams are done in SmartDraw. It cost nearly US$299, but it’s worth every cent. I made a little animation out of building the state diagram. It might appear here in the near future, if Collis approves. I used to use Visio even before Micrsoft bought the company. (But because of that, the product went from something like $150 to being buried in an Office “package” which I didn’t care to use.)
Zach: Hmm. State Diagrams or impeachment articles against Bush. Wow
Maybe we need a proper state diagram to understand how to implement the impeachment. Start state: now. End state: anything else. Inbetween states: unknown.
Danny
June 10th, 2008
Never heard of state diagrams, sounds interesting though!
Jacob Gube
June 10th, 2008
@Lamin Brown: “Oh sorry please disregard my above comment. I thought i was at psdtuts… LOL” — that made my day! Both wonderful sites and great that you frequent both!
ali
June 11th, 2008
very good tutorial
Ben
June 11th, 2008
First, state models predate UML by decades and is one of the fundamental approaches used by hardware designers. Many diagrams in UML were pre-existing but needed a formal, singular definitive definition for the software industry. Similarly, becoming a pattern did not invent it, but better communicated the approach in literature programmers would read.
Second, while I like this approach you have abused the state model. Its very complex, more of a mesh, since you had to invent your own transition icons to describe it. The state pattern cannot scale well, meaning that good usages are fairly simple (e.g. network protocol) and leverages composition to grow. Instead of describing the entire page’s interactions, if you broke it into self-contained widgets (component model), each individually backed by a state model, it would work far better. Instead of using states to describe page flow, a simple workflow model (e.g. Spring WebFlow) are cleaner. State models are quite poor at describing transitions, which is better done through petri nets (colored petri nets make excellent back-end workflow engines).
Overall, excellent article since I doubt many front-end developers have thought about modeling this way.
Min Sik
June 12th, 2008
There is no doubt that state diagram is a powerful concept; especially, if you deal with scientific/engineering problems. However - not trying to be a pain here - the example presented in this tutorial is rather an overkill. We do not gain much compared to maintaining a simple site map. I am not arguing that the state diagram consists more information (e.g. possible transition, etc.); however, given the small set of possible inputs/transitions, it is rather a waste to spend time maintaining this type of diagram.
Thank you for spending time writing this tutorial (esp. the nice looking diagrams). However, I hope the readers do not simply try to spend too much time making something cool without gaining much of benefits (well, unless if it is a hobby).
Raj Dash
June 12th, 2008
Ben: I had to debate about whether to use that extra “icon” of a thick arrow, but there’s a cutoff point where the graph would be overly complicated and would just be confusing. Typically, a state diagram would in fact be broken down at several scale levels, if warranted. However, I didn’t want to get too complicated. The essence is that if you’re working by yourself, you’re allowed to cheat a little. If you’re working in a group, you can look into the state diagram model further and do it up right.
Min Sik: Very good point. Again, there’s a cutoff point. If I use too complicated an example, then I have too complicated a diagram, which might lose some readers. The idea of the tutorial is to show the basics. I did cheat with the thick arrows, but there’s no Finite Math professor staring over my shoulder “tsk tsking” me. And those who follow what I’m talking about with this simple example can now scale up to a more complex example.
The general intent is to suggest that if you have a mindset of understanding the coding projects you’re working on, then it’s a great benefit to most people to have a schematic of interactions. For static websites, you really don’t need a state diagram - something I said repeatedly above. For the type of tutorials you’ll see at Nettuts, you might not always need them either, but if you’re doing “dynamic site” deve, it really helps. (What’s more, I was “setting up” for more complex tutorials in the future.)
Raj Dash
June 12th, 2008
By the way, to emphasize what Min Sik said, I almost never draw state diagrams on a computer - almost always on paper only. For simple projects, it takes far too long to go beyond paper/
Taylor Satula
June 13th, 2008
This is really complicated…
I Feel Retawded

VertigoSFX
June 16th, 2008
That is an awesome tutorial. When I first came to it, I read the comments before the actual article (go figure) and there were a lot saying it was confusing. I actually found it to be the opposite. I’m more of a designer myself, but I delve into code because I know when I get out into the real world (i’m just about to start college) i’ll need to know some of this stuff to “excel” above the rest.
This here is great because I’m starting to research doing more advanced dynamic websites and I think it would really help out….because you can easily map out a website site map to see the structure of the site but not many think about mapping out the structure of different states of a page as well.
Gotta love nettuts.com and psdtuts.com
Code It Red
June 17th, 2008
Never really heard of “state diagrams” before, but now that I have I will certainly have to go in and check them out.
JustChris
June 19th, 2008
Now this is the kind of stuff worth learning in a Discrete math/Data Structures class
I normally only create diagrams for database architecture, and flowcharts for site navigation, but the more I see this the more state diagrams make sense. The flow is more intuitive, the presentation is more compact.
Taylor Satula
June 29th, 2008
Looking back this really wasnt a good idea this article. P.S. James your never gonna beat me
Windows Themes
September 5th, 2008
Hmmm. What software do I need to create a similar diagram ? :D. Thanks