Anonymous Functions

I have been asked what is the advantage or disadvantage in the use of anonymous functions or inline (or onfly), used a lot in Javascript, now with 5.3, also in PHP.
An inline function, or anonymous because he did not - in fact - a name, it is a fast way to write some procedures, indenting tree sequences callback for example. Almost all languages ​​support the functionality of this writing, each to the limit with its peculiarities and restrictions. They may also have different names and syntax details, such as the so-called blocks in Objective-C.
However, restricting the cases to Javascript and PHP, their appearance is almost identical:

1
2
3
4
/ * With jQuery * /
) . click ( function ( ) { $ ('Div # button'). Click (function () {
'Clicked' ) ; alert ('Clicked');
});
1
2
3
4
/ * In WordPress for example * /
, function ( ) { add_filter ('wp_head', function () {
; echo 'Test';
});

The examples above could also be written in this way:

1
2
3
4
5
/ * With jQuery * /
function onClick () {
'Clicked' ) ; alert ('Clicked');
}
) . click ( onCLick ) ; $ ('Div # button'). Click (onclick);
1
2
3
4
5
/ * In WordPress for example * /
my_wp_head function () {
; echo 'Test';
}
, 'my_wp_head' ) ; add_filter ('wp_head', 'my_wp_head');

As is evident, the implementation of functions, anonymous or not, remains the same and the gain on the amount of code written is objectively trivial, especially on large projects (ultimately you delete the name of the function).

If we were to judge the two examples above, without asking any questions, the comparison would play on a subjective level and style, there are those who prefer to avoid the anonymous functions because they often make the code difficult to read because he loves or those who prefer the compact code and with a few function calls.

But there are some considerations, some specific to certain circumstances, which - at least to me personally - they tend to make me avoid or at least limit, the use of anonymous functions.

1. Readability
Direct experience, especially in JavaScript, the use of anonymous functions, where a good need a callback to another, and another, makes the code unclear, threatening to commit silly mistakes because we have lost sight of a closure of a brace or not there is a perception of being inside a different function compared to that of above, etc ..., etc ....

In addition, the development IDE, of course, difficult to correctly display the list of functions of a code that uses massively anonymous functions: they have a name to put in the list ...

2. Reusability
An anonymous function, which is the code that implements it, can not be used more than once! It seems obvious, but this is obviously not a good thing. On reflection it is curious that a function, which by definition has the feature of being able to be used n times, its important to lose this property in the use of anonymous function, but after all, is partly intended.

Using the example above PHP, but the concept is also valid for Javascript, here is what would happen in two cases:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/ * First filter * /
, function ( ) { add_filter ('wp_head', function () {
/ / ...
/ / Same implementation
/ / ...
});

/ * Other filter * /
, function ( ) { add_filter ('wp_footer', function () {
/ / ...
/ / Same implementation
/ / ...
});

/ * Other filter ... * /
, function ( ) { add_filter ('wp_sub_footer', function () {
/ / ...
/ / Same implementation
/ / ...
});

Obviously if you find a bug, or improves the code in one of the anonymous functions, you need to duplicate these changes in all other cases. The situation would be different with a standard code:

1
2
3
4
5
6
7
/ * Common * /
my_wp_head function () {
; echo 'Test';
}
, 'my_wp_head' ) ; add_filter ('wp_head', 'my_wp_head');
, 'my_wp_head' ) ; add_filter ('wp_footer', 'my_wp_head');
, 'my_wp_head' ) ; add_filter ('wp_sub_footer', 'my_wp_head');

In this case, paradoxically, we saved quite a bit of code written.

PLEASE NOTE: I think it is obvious that no developer would leave three or more blocks of the same code. As soon as you realize that a piece of code is repeated more than once, it is natural to turn it to anyone - in fact - in function, with name, so how much time we want to be called.

3. Identification
By definition, being anonymous, can be used onfly but not retrieved or identified, as mentioned in the previous point. This can be a hindrance in special cases such as:

1
2
3
4
5
6
7
8
/ * Common * /
my_wp_head function () {
; echo 'Test';
}
, 'my_wp_head' ) ; add_filter ('wp_head', 'my_wp_head');
...
...
, 'my_wp_head' ) ; remove_filter ('wp_head', 'my_wp_head');

Here, for example, we have a function `remove_filter ()` which claims to complete its task, the filter code (`` wp_head), but also the function name! This, with the anonymous function, it would be impossible.

This last example is perhaps the most illuminating in that, in the case of PHP and WordPress, there are actually a lot of filters that you set "on the fly" only once and that did well conform to the use of anonymous functions. I, for example, it is in these cases that often abuse. However, as has happened to me, subsequent drafts of the code can happen - often - of having to turn the anonymous function in a call by name.

In conclusion, therefore, the anonymous function are a great development tool, providing often - during the first draft of the code - definitely a speed greater than the approach 'in all functions'. As often happens, abuse is not beneficial, both in terms of functionality and code readability. Furthermore, in some contexts their use more than being discouraged becomes not feasible.

There are no comments for this post

Leave a comment

TAG XHTML PERMITS: CODE ENTRY:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL