Debating the AJAX Demo

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:

  1. Inherit a class for each usage scenario and use the template pattern: the thread Execute method will call a virtual function each specific class can override. Nice, but in case the specific code is limited, creating many similar classes, mostly used only for a single object in a specific situation is far from a nice architecture.
  2. Delphi classic alternative to inheritance is to use events. In fact, you don't inherit from TButton to override the Click method, but assign an external procedure to the event, using method pointers. Each customization is in a separate method you assign.

Method pointers and anonymous methods are not that different. In one case you can write the procedure in-place, but that is an option. For readability, you can write the method's code as a series of separate functions, each assigned to an anonymous method pointer to be called later on. Will this be more readable? Possibly, even if (from JavaScript experience) I think it is mostly a matter of getting used to one style or another. In other words, syntax aside, the concept of an anonymous method is not far from that of a method pointer, but the fact they introduce a new lifetime model for variables can help.

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?

0 0
Project Management Made Easy

Project Management Made Easy

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.

Get My Free Ebook


Post a comment