By warpedjavaguy
via warpedjavaguy.wordpress.com
Published: Jun 25 2008 / 12:40
Avoiding redundant code is a basic programming instinct. Java closures make it easier to eliminate redundant code and avoid it altogether! All other benefits are derived benefits inherited from this fundamental benefit.
Comments
killerweb replied ago:
Makes lots of sense but preaching to the informed. Anytime explaining something like Closures, you really need to give REAL world examples. Devil is in the details and just saying it makes DRY easier doesn't help, you need to show it. Reusable code isn't enough of a reason, since that could be done other ways. Just a suggestion.
Dhananjay Nene replied ago:
I voted this down since the argument is both weak and incomplete.The essence of closures is not avoiding redundancy (closures distinguish themselves by "closing over" a runtime context) and if the primary focus is to avoid redundancy, closures is not likely to be the primary tool (there are just many of them eg. function, class etc.)
warpedjavaguy replied ago:
Hi Dhananjay,
I am not saying that avoiding redundancy is the primary essence or focus of closures, but rather that it is the primary benefit that closures bring to the Java language. They eliminate boilerplate and facilitate much greater reuse in our code. If we already have the tools for achieving this, then why are we still writing boilerplate code and why is redundant code still a problem?
Dhananjay Nene replied ago:
Hi,
There are two primary aspects of closures (a) The ability to close around and (b) The ability to treat code blocks as function objects. The latter capability can easily deliver almost bordering on the same reduction in redundancy that you would like to see with closures. To meet the redundancy reduction objective one only needs to introduce what are variously called as function objects / functors / lambdas - one does not need closures. A substantial part of the redundancy you seek to bring down can also be had by simply restructuring your code just a little differently without even using function objects.
I am very certain that the reason we still continue to write boilerplate code has to do with peopleware and not software and most certainly not the lack of closures. Better mentoring and training is the answer there .. not closures.
warpedjavaguy replied ago:
I'm sorry, but no amount of training or mentoring will ever eliminate boilerplate and redundancy from closure-less Java code.
Voters For This Link (11)
Voters Against This Link (5)