My first blog is now published. Do read it and let me know about it in the comments section❤️
Benefits of Lambda Programming
Well, you might have heard about this word a lot but if you haven’t let me just give you a quick summary. Lambda expressions (or lambda functions) are essentially blocks of code that can be assigned to variables, passed as an argument, or returned from a function call, in languages that support high-order functions. They have been part of programming languages for quite some time. Some examples include Smalltalk, Lisp, Ruby, Scala, Python, and, more recently, Java and JavaScript. Here are some examples:
- Python:
square = lambda x: x**2
square(6) //-> 36
- Ruby:
square = lambda { |x| x * x }
square.(6) #-> 36
- JavaScript (fat-arrow functions):
var square = x => x * x;
square(6) //-> 36
Now coming back to where we were. So, from the above explanation you would have gotten an idea of what lambda expressions or lambda programming is and as you have read the title we are here to discuss some of the benefits of lambda programming which are:
- Conciseness
- Reduction in code bloat
- Readability
- Elimination of shadow variables
- Encouragement of functional programming
- Code reuse
- Enhanced iterative syntax
- Simplified variable scope
- Less boilerplate code
- JAR file size reductions
- Parallel processing opportunities
To get a full understanding of the benefits of lambda expressions in Java and how they provide conciseness and readability, just look at how functional interfaces, that is, an interface that only defines a single method, were implemented prior to lambda expressions. There were basically two approaches that could be used. One approach was to define a separate class that implements the interface, while the second was to use an anonymous inner class.
Single class approach to interfaces
Using a separate class to implement the Comparator interface example from a prior lambda functions article would looks as follows:

The benefits to this approach is the ability to share the new class among multiple code bases, so reuse is possible. The disadvantages to the single class approach? For starters, there is code bloat, as a whole new class is required to implement a single method. Furthermore, the logic is separated from the point in which it is needed, which will make troubleshooting and code maintenance more difficult in the future. To address the shortcomings of this approach, Java 1.1 introduced the concept of anonymous inner classes.
Benefits and drawbacks to anonymous inner classes
An anonymous inner class allows developers to define and implement a functional interface at the point in the code where it is needed. In the prior example, we needed to create a separate class named MyComparator. With anonymous inner classes, you don’t define a separate class, you simply implement the methods requires, thus the name anonymous. Here’s how the Comparator example looks when implemented using an anonymous inner class.

Anonymous inner classes are popular, and much preferred over creating separate implementing classes. They also allow for the implementing code to be written at the spot in which the code is used, which makes troubleshooting and long-term maintenance easier.
There are drawbacks to anonymous inner classes though. One major drawback is that the code does not look pretty. If anything, it looks bloated. Furthermore, the ability to reuse this code is muted, as there’s not way to invoke an anonymous inner class from outside of the code in which it is defined. And a more innocuous issue with anonymous inner classes is the peculiar variable scope they introduce directly within a class. Variables defined outside of an inner class do not employ the traditional rules pertaining to block scope within a Java class. As a result, it is easy to fall prey to a bug-inducing anti-pattern known as variable shadowing. This can create bugs that are very difficult to troubleshoot.
Introduce Yourself (Example Post)
This is an example post, originally published as part of Blogging University. Enroll in one of our ten programs, and start your blog right.
You’re going to publish a post today. Don’t worry about how your blog looks. Don’t worry if you haven’t given it a name yet, or you’re feeling overwhelmed. Just click the “New Post” button, and tell us why you’re here.
Why do this?
- Because it gives new readers context. What are you about? Why should they read your blog?
- Because it will help you focus you own ideas about your blog and what you’d like to do with it.
The post can be short or long, a personal intro to your life or a bloggy mission statement, a manifesto for the future or a simple outline of your the types of things you hope to publish.
To help you get started, here are a few questions:
- Why are you blogging publicly, rather than keeping a personal journal?
- What topics do you think you’ll write about?
- Who would you love to connect with via your blog?
- If you blog successfully throughout the next year, what would you hope to have accomplished?
You’re not locked into any of this; one of the wonderful things about blogs is how they constantly evolve as we learn, grow, and interact with one another — but it’s good to know where and why you started, and articulating your goals may just give you a few other post ideas.
Can’t think how to get started? Just write the first thing that pops into your head. Anne Lamott, author of a book on writing we love, says that you need to give yourself permission to write a “crappy first draft”. Anne makes a great point — just start writing, and worry about editing it later.
When you’re ready to publish, give your post three to five tags that describe your blog’s focus — writing, photography, fiction, parenting, food, cars, movies, sports, whatever. These tags will help others who care about your topics find you in the Reader. Make sure one of the tags is “zerotohero,” so other new bloggers can find you, too.