After I blogged about this example, there was some debate about the readability and the usefulness of anonymous methods. I replied to these critical voices (some of which do have a point) with another blog post in which I tried to clarify the situation. As it adds some extra information to the use of anonymous methods (and some other alternatives) I think it is worth reporting it, in an slightly edited version:
The post I made yesterday on anonymous methods, a new feature in Delphi 2009, stirred controversy. I agree on the readability comments, but you should also consider that using three nested anonymous methods was quite a stretch, not a common usage scenario.
Let's start from the beginning and examine only one step. I want to make an HTTP call and process the result. This has to be done in a thread, since I don't want it to be blocking. So whether you use anonymous methods or not you need to define a custom TThread class. Now suppose you want to use the same thread class (or its HTTP support code) for slightly different situations. You have two traditional options:
This brings me to another point, why not keep using method pointers. Having to allocate memory for every invocation of an event (in the case of parallel code execution) would be far from trivial in many cases in which anonymous methods just "magically" work. As a commenter noted about the code, if you hit the button many times the nHit stack-based variable gets duplicated and captured for each anonymous method invocation, so not only does it live beyond its original stack location, but you can have multiple instances at the same time.
Would this mean each and every Delphi code would benefit from this new technology? Of course not, I think it is useful only in a fraction of cases. I remain convinced that an Ajax-like call is a nice scenario and that it will take some time for the Delphi community at large to master this new language feature.
Was this article helpful?
What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.