Aug 23 2006

Closures are dangerously powerful

Christopher Diggins on closures and anonymous functions:

… closures have a very clear downside: they increase code coupling. Passing a single closure can extend the lifetime of massive numbers of objects, leading to a huge performance hit.

Closures are a very powerful feature, but dangerously so. They are arguably too easily abused. A language designer has the responsibility to force a programmer to be explicit about dangerous things.

I love closures, and it’s one important corner stone of many elegant solutions. For example Python decorators will not be as useful without closures supported in the language. However I also agree with the danger of abusing closures — it is indeed something that programmers can easily shot themselves on the foot. Javascript memory leak on many web-based applications for example, is caused by closures keeping the browser-created objects (i.e. DOM nodes) for too long, sometimes due to bad cross referencing.

No Comments

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>