September 24, 2008
Ever seen three nested anonymous method calls in Delphi? Here is a real world example, written for the book and a talk.
I've seen (and written) nested anonymous method calls in JavaScript a few times. This is the first time I write one in Delphi.
The example is not artificially convoluted, but makes sense. There is an HTTP call within a thread (started behind the scenes by the AjaxCall function), receiving as parameter the code used to process the result, which in turn calls an HTML parsing function (ExtractImages), using a second (nested) anonymous method for processing the image links. One of the links is processed by the AjaxCall, again, this time with the effect of showing the resulting data as an image, within the third nested anonymous method. Whew.
Now if you consider the syntax, with the various nested procedures in place, it certainly look very odd to any long.time Pascal developer.
posted by
marcocantu @ 0:52AM | 14 Comments
[0 Pending]
14 Comments
Anonymous, Anonymous, Anonymous
And this is to make reading code and understanding it
better?????
Comment by Jim on September 24, 02:57
Anonymous, Anonymous, Anonymous
After playing with it several times, I finally
understand the beauty. Really like the capture effect
of local variables (like nHit's usage in Marco's
code), which makes it easier to write (it is still
hard to understand by beginners, though).
Comment by Lex Y. Li
[http://lextm.cn]
on September 24, 05:30
Anonymous, Anonymous, Anonymous
What happens if
- Button3Click is called again before AjaxCall
completes? Will there be two individual
captures/threads?
- Image1...LoadFromStream throws an exception?
Comment by Lars Fosdal
[http://delphi.fosdal.com]
on September 24, 11:07
Anonymous, Anonymous, Anonymous
I agree with Jim: this is awfull. What I like about
object oriented languages is that you can - if you
have a good design and a good naming convention -
create easily readable code which hardly require
explanation at all. Your example - how 'elegant' it
may be - defies that and it turns our beloved
language into a very difficult piece of code. Sorry,
I still hate anonymous methods.
Comment by Bart Roozendaal
[http://blogs.sevensteps.com]
on September 24, 11:13
Anonymous, Anonymous, Anonymous
I certainly don't claim readability is a plus, but the
alternative coding, with the need of deriving a class
for each specialized behavior or attaching event
handler, isn't really much cleaner...
Time permitting I'll post similar code written in a
traditional way, as a comparison.
Comment by Marco Cantù
[http://www.marcocantu.com]
on September 24, 11:39
Anonymous, Anonymous, Anonymous
Hmmm.... this makes me want a compiler option to
disable anonymous methods entirely.
Comment by Eric on September 24, 17:42
Anonymous, Anonymous, Anonymous
The main problem with anonymous is readability.
Because of similar structure to block of code or to
nested procedures it difficult to see if it is
anonymous method or block of code.
I think that code should be different formated (treat
anonymous methods like parameters not functions, add
more spaces):
AjaxCall(edUrl.text,
_________procedure(aResponseContent:TStringStream)
___________begin
___________......
___________AjaxCall(aUrl,
____________________procedure(..)
______________________begin
______________________....
______________________end
____________________)
___________end
_________);
And then know you better see where is procedure and
where is anonymous method.
Comment by Macedonczyk on September 24, 17:42
Anonymous, Anonymous, Anonymous
@ Bart
If you don't like/need it, just ignore it. The
anonymous methods (closures) allow to employ a
functional programming style which became fashionable
again (for good reason).
Its inclusion imho shows that the dusty Delphi may
start again to not only chasing behind but shine.
To wrap you head around closures, you may want to read
about Ruby (blocks). Admittedly the syntax is nicer there.
Comment by Hans-Peter on September 24, 20:15
Anonymous, Anonymous, Anonymous
"If you don't like it/don't need then don't use it"
only works if you never work with other people's code.
Especially if those other people subscribe to the view
that "it's new, therefore we must learn to use it and
so we'll use it at every opportunity and worry about
whether it's the best way IN THE LONG RUN later".
As for functional programming being fashionable again
("again"? what?).
I'm firmly of the view that much of what is happening
in the area of software development is a recycling of
old ideas in order to have a new story to tell and new
ways to extract $$$s from the purchasers of tools (by
creating an illusion of necessary change).
It is telling that it is so difficult to come up with
compelling demonstrations of the practical and
productivity benefits of many of these "advances".
Sexy demo's that show off the technical possibilities
on the other hand are very easy. But strangely also
very easy to shoot down (at which point the defense is
always "but this is only an example... of course you
wouldn't do this in practice/all the time").
Comment by Jolyon Smith
[http://www.deltics.co.nz/blog]
on September 25, 03:39
Anonymous, Anonymous, Anonymous
Ouch! I had to stare at this for a while before I
grokked what it was doing.
My gut reaction to this was if one of my engineers
wrote this code I'd be tempted to send them to
Siberia for reeducation.
Is there a particular reason why this is better than
providing callbacks (reference functions or
otherwise) which implement the 'anonymous' logic in a
clear, logical and reusable way?
Raymond
Comment by Raymond Wilson on September 25, 08:26
Anonymous, Anonymous, Anonymous
What an excellent example of composition of object-
oriented and functional programming! Delphi 2009
really rocks!
I think of replacement of callback functions in my
code with anonymous methods, after transition to the
new Delphi will done. I think, it will considerably
reduce my code and will improve its perception.
Comment by Kryvich
[http://kryvich.googlepages.com/]
on September 25, 13:35
Anonymous, Anonymous, Anonymous
@Raymond: I doubt that instructors in Novosibirsk
don't know the functional programming.
So such exile can "hurt". :)
Comment by Kryvich
[http://kryvich.googlepages.com/]
on September 25, 13:50
Anonymous, Anonymous, Anonymous
Jolyon: working in a team without clear rules and
peer code review it's a good recipe for disaster.
Wilson: callbacks can't capture state, anonymous
methods can.
Comment by Luigi D. Sandon on September 25, 17:57
Anonymous, Anonymous, Anonymous
Can anyone answer to Lars Fosdal post? I am also
wondering what happens if we close application before
AjaxCall completes?
Comment by Curious guy
[]
on October 12, 22:51
Post Your Comment
Click
here for posting
your feedback to this blog.
There are currently 0 pending (unapproved) messages.