Wednesday, 5 September 2012

Great programming analogies No.1. Foggy Ping Pong

This is the first in a series of posts where I plan to share some of my favourite analogies and anecdotes from my training sessions and presentations. This one is an analogy. Foggy Ping Pong.

Earlier this year, I wrote a post in an ongoing series of mine sharing my tips for delivering a better presentation or training course. The post in question recommended keeping a course diary. One of the reasons that I gave was that you (the presenter/trainer) sometimes come up with a really good analogy off the top of your head. Provided that you remember it (hence the diary), you will use it again to great effect and perhaps refine it over time.

It was whilst writing that post that I coined the phrase "Analogies and anecdotes" which I decided there and then was to be the title of my autobiography.

It was only a matter of time (3 months) before I got around to writing some of these analogies down.

Now if you have read this far, then you must be REALLY desperate to know what "Foggy Ping Pong" could possibly relate to. Time to put you out of your misery.

This is quite a recent invention of mine and relates to State Management in web applications.

Developers of desktop applications often struggle with this issue when first moving to web development.

I often present a great course entitled Building Effective ASP.NET MVC 3.0 Web Sites using Visual Studio 2010. The chapter dealing with State Management opens with a statement.

"HTTP is stateless"

Now this is why we need analogies.

I can deliver this fact to a room of delegates and they will all look back at me and their expressions are saying, "and that means what exactly?"

So I can qualify by saying:

The transport protocol HTTP that carries the request from the browser to the web server is stateless. i.e. it simply carries the request for a particular page from the user's browser. It does not carry any other information (not quite true) such as what the user was "up to" last.

And their expressions are saying, "and that means what exactly?"

OK. How about this? What is a web server?

Well a web server receives in-bound requests from browsers for content. e.g. web pages.

OK. So a web server receives a request asking it for a copy of the web page named products.aspx.

It locates that content and returns it back to the caller. It is the HTTP protocol (I call HTTP "White Van Man", but that is another analogy ) that transports requests and responses to and fro.

By the way, the smarty pants among you will be wondering why I am referring to products.aspx which is evidently a WebForm when I began by talking about an ASPNET MVC course (which doesn't use them). Well OK. I also deliver a course entitled: Building Effective ASP.NET 4.0 Web Applications using Visual Studio 2010.

So there we were. The web server receives a request for a web page named products.aspx.

Moments later, it receives a request for productdetail.aspx.

Yes of course! That will be the user who last requested products.aspx who will have clicked on a link in that page resulting in a request for productdetail.aspx.

Oh really? And how does the server know that?

How could it possibly know that the request for productdetail.aspx came from the same source (browser) as the recent request for product.aspx?

Remember I said that HTTP (White Van Man) was stateless? It simply delivered the request.

So here is where Foggy Ping Pong comes in. Imagine you are playing Ping Pong. You are a web server, and your opponent is the user and her/his browser.

Only you can't see them because it's foggy!

Image reproduced by kind permission of my son Alex who created it for me in Photoshop. Nice one!

In fact. How do you know there is only one opponent out there?

Hey there could be a couple of the fellas on the other side, all taking it in turns to hit the ball back (make requests) and you would never know. By now, the delegates will have formed a mental picture to help them understand that the web server has no clue who it is dealing with. Or how many users in fact.

Hopefully, they are beginning to get it.

Here's the thing. Analogies rarely cover a concept entirely and often need to morph/segue into something else entirely. Once I have suggested Foggy Ping Pong, and have noted by the expressions in the room that they "get it", I point out (unless someone beats me to it) that there is a flaw in the analogy.

In this case it is because the web server is not having a single game of Ping Pong with several players. It is in fact having several games of Ping Pong with individual players at the same time.

So here is where the analogy morphs into Foggy Simultaneous Chess.

This is Bobby Fischer at the Wachusett Chess Club in Fitchburg in March 1964 in case you are interested. I didn't have the heart to ask Alex to create me another image in Photoshop. He has a lot on.

Now Foggy Simultaneous Chess isn't quite right either because in the case of chess you can see all of the boards and the pieces laid out before you. Lots of state!

But now that I have painted that picture, lets return to one player (the web server) playing countless games of Ping Pong with several individuals.

Just imagine all those little white balls flying at you out of the fog and you batting them straight back. Have you any idea how many users are out there? Hey, you could even have 5000 users online right now requesting pages from the same web server.

How are you going to tell them apart?

How can you tell which ball came from which player?

How do you know who the players are?

How do you keep track of what these players did (requested) previously?

So there you have the picture. Do you get it? Do you see the problem? That is what life is like for the web server.

Now for the remainder of the State Management chapter we would then go on to look at what features are available to web servers that can help them keep track of all the users (players), what they want, what they have had and where they are up to.

We would talk about querystrings, cookies, sessions, viewstate (for WebForms), caching and so on.

And if you want to know what they all mean, you should really go on a course!

If you are interested in learning more about web development, QA offers the following courses:

Building Effective ASP.NET 4.0 Web Applications using Visual Studio 2010

Building Effective ASP.NET MVC 3.0 Web Sites using Visual Studio 2010

Developing ASP.NET MVC 4 Web Applications

Developing Web Applications with Microsoft Visual Studio 2010

See you soon

Phil Stirpé
"I don't do average!"

No comments:

Post a Comment