Author: Keith Evans
Date: 21:46:04 11/26/01
Go up one level in this thread
On November 26, 2001 at 22:40:39, Russell Reagan wrote:
>So what is the advantage of using callback functions? Is it faster or more
>organized or easier to design the engine or what?
This brings to mind something that Rob Pike said in his "Notes on Programming in
C" so I have included the relevent section below - I hope that it helps to
answer the question. I think that it's difficult to appreciate how convenient
this technique can be until you take a look at some actual code.
Regards,
Keith
"Function pointers
Another result of the tyranny of Pascal is that beginners don't use
function pointers. (You can't have function valued variables in Pascal.) Using
function pointers to encode complexity has some interesting properties.
Some of the complexity is passed to the routine pointed to. The routine
must obey some standard protocol - it's one of a set of routines invoked
identically - but beyond that, what it does is its business alone. The
complexity is distributed.
There is this idea of a protocol, in that all functions used similarly
must behave similarly. This makes for easy documentation, testing, growth and
even making the program run distributed over a network - the protocol can be
encoded as remote procedure calls.
I argue that clear use of function pointers is the heart of
objectoriented programming. Given a set of operations you want to perform on
data, and a set of data types you want to respond to those operations, the
easiest way to put the program together is with a group of function pointers for
each type. This, in a nutshell, defines class and method. The OO languages
give you more of course - prettier syntax, derived types and so on - but
conceptually they provide little extra.
Combining data driven programs with function pointers leads to an
astonishingly expressive way of working, a way that, in my experience, has often
led to pleasant surprises. Even without a special OO language, you can get 90%
of the benefit for no extra work and be more in control of the result. I cannot
recommend an implementation style more highly. All the programs I have
organized this way have survived comfortably after much development - far better
than with less disciplined approaches. Maybe that's it: the discipline it
forces pays off handsomely in the long run."
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.