December 18, 2008

Basic Concepts: Pointers, Delegates, ByRef, ByVal and Asynchronous Programming

It came to my attention this week that asynchronous programming and delegates aren't that easy for people to understand and even if you do understand them, they're difficult to explain - okay, it came to my attention a while ago but it only occured me to write anything about it today. It's a concept, much like pointers that can take some getting your head around. There are many excellent in depth explanations of how to implement them found all over the internet but none of them seem to do a very good job of explaining the basic underlying concept in plain understandable English.

A friend of mine explained it to me a while back after I'd read 20 websites and numerous book excerpts trying to understand delegates and how they worked and what they were used for. The explanation is no more than a dozen lines of plain English that even a non programmer can understand. The legendary Jon Skeet asked me for this explanation yesterday and prompted me to wonder if so many well renowned authors find this concept difficult to explain, then maybe I should post it on my blog.

"Consider a very simple example of a manager and their assistant. The manager is very busy and asks the assistant to run off and do some tasks and tells her that upon completion of those tasks she must go to her pigeon hole where she will find further instructions awaiting.

In programming terms this is somewhat analogous to the concept of asynchronous programming. The assistant runs off and does what is asked of them and the manager goes about their merry way doing other more important manager tasks. The assistant returns upon completion of her task list and goes to her pigeon hole to find out what she needs to do next.

So in the programming world, let’s consider the manager the main processing thread of your application and the assistant as an asynchronous process. The delegate is the pigeon hole – the location to find the address of the method to run next. The instruction found in the pigeonhole is the callback method."

It occurs to me that pointers in general could be explained in much the same way. If I put a letter in your pigeon hole, you know where to find it. The pigeon hole is the pointer to the letter which is the variable and the value which is the contents of the letter. Then you get to pointers to pointers and pointers to pointers to pointers etc which I couldn't even grasp when I was beginning to learn programming no matter who tried to explain it to me. It basically becomes a treasure hunt of pigeon holes with police tape over them and a note saying "look in pigeon hole n" (where n is the number of a different pigeon hole)...finally in the last pigeon hole is the letter which is your variable, and the letter contents which is the value.

Lets forget pointers for a minute and use a similar example in a different fashion to explain the difference between passing values by reference or by value.

"The manager wants to give her assistant a message. She writes a message and takes a photocopy, giving the copy to her assistant. The assistant can now do whatever she likes with her copy, she can scribble all over it, she can tear it up and throw it in the garbage if she likes. The manager still has the original and it's untouched.

This is the same as passing a variable to a method by value. Consider in this example that the assistant's actions are the method and the photocopied note is the method's arguments.

For passing a value by reference consider this: Instead of giving the assistant a photocopy of the note, the manager writes her note on the whiteboard, the assistant makes her modifications on the whiteboard. The whiteboard itself in this scenario is the variable and what is written on the whiteboard becomes the variable which the assistant modifies by reference."

This effectively is the same as passing a pointer to the variable to the method which is in fact what passing a variable by reference means.

And so concludes a plain English example of the basic concepts of asynchronous programming, delegates, variables, pointers, by reference and by value...

I know I've simplified and skated over some of the nuances and implications of these concepts, but I hope that is of help to those that don't get it, and of help to those that find it hard to explain these concepts to others.

No comments:

Post a Comment