JavaScript Anonymous Functions – Blessing or a Curse?

Anonymous FunctionsThe growing popularity of AJAX and jQuery has significantly increased the use of anonymous functions in client-side JavaScript code.  Although anonymous functions provide a very convenient way to decrease the amount of code necessary in an app, their prevalence and necessity may portend to structural deficiencies in either the language or development tools.

Anonymous functions are functions declared without a name.  Below is an example of a standard JavaScript function:

function DoSomething() { /* Function code goes here */ }

If the developer wants to use that function, they will need to declare it, and then reference it later.  For example, if they want the function to run every second (1000 milliseconds), they could write:

window.setTimeout(DoSomething, 1000);

An anonymous function, on the other hand, is declared in-line with the code.  Instead of taking two lines of code and specifically naming the function, the developer could alternatively write:

window.setTimeout(function() { /* Function code goes here */ }, 1000);

Although at face value it seems shorter and simpler, using anonymous functions causes three significant issues:

First, debugging becomes more challenging, since an explicit stack trace is not available for errors.  Instead of analyzing the stack trace, developers need to resort to more traditional methods, such as debug messages or a custom logging module.

Second, complex or lengthy code in anonymous functions can significantly complicate the readability and maintainability of the software.  While anonymous functions do make one-line functions easier to read, longer code has the opposite effect.  The ease of their declaration also often increases redundant code.

Finally, anonymous functions are less efficient than traditional functions.  Although the performance hit is negligible for simple operations, more advanced code can significantly decrease app speed.

The Solution

In order to decrease use of anonymous functions, two aspects of web development need to be improved.

First, most web development IDEs feel like they have been built in the 1980’s.  Developers are provided with little more than a basic text editor for developing apps.  With a better IDE that implements convenient code completion and auto-function generation, most programmers would quickly start explicitly defining the anonymous functions.

Most importantly however, the DOM needs to be revised.  Due to the high interactivity of web apps, there needs to be a better way to code asynchronous calls and time delays.  The API’s need more extensive built-in capability for common interactions, or support for data-binding and automatic updates.  Microsoft has been taking a step in that direction with WinRT, however the solution is far from complete.

While the proliferation of anonymous functions is, in general, a good indication that developers are pushing the boundaries and creating an exciting, fervent environment for technological growth, it is also vital for the language architects to review and implement solutions that will structure development and allow even greater flexibility and modularity in the future.

Written by Andrew Palczewski

About the Author
Andrew Palczewski is CEO of apHarmony, a Chicago software development company. He holds a Master's degree in Computer Engineering from the University of Illinois at Urbana-Champaign and has over ten years' experience in managing development of software projects.

RSS Twitter LinkedIn Facebook Email

Leave a Reply

Your email address will not be published. Required fields are marked *