4 things to make your Java 8 code more dynamic
August 13, 2015 Editor 0
#Java 8 has a few new features which should help you write more dynamic code. Of course one of the big features was the addition of a lambda syntax. But what about some of the other features that were added? Here are a couple of things that I tell people to do in order to make their code more dynamic and more functional.
Use lambda’s to abstract your functions
Although this is really self-explanatory, it’s important to remind people to do it. Some of you might be saying, “Hey, I’ve used lambdas in my code!” That’s fantastic, but are you using them as much as you could be? Look at your code — I want you to think, “What if I could pass in this part of the function?”
I always tell people to start looking for duplication of their code. Look for functions doing mostly the same things, see if you can pass in a function to handle the part of the code that is different. While you are factoring out some of these duplicate functions, you should, if its possible to re-use the inner functions that you’re creating. Although it might be awesome to have lots of little lambdas everywhere, if you’re rewriting lambdas, you should consider making it a method and use the method reference notation instead.
Let’s say that there are two functions
So how could we reduce this into a single method? Let’s use the new functional interface.
Which allows us to call:
Or we can use the method reference:
Overall, this should allow you to create much more dynamic and reusable code and reduce your code base. There is no exact formula here, you’re going to experiment with creating functions and abstracting the functionality out as necessary. The more you do it the better you’ll get.
Use Optional instead of NULL
There is some contention here about Optionals. Some people feel that you shouldn’t be using them in lieu of NULL. But let’s think about the reasoning of an Optional versus a NULL. NULL means the absence of a value, generally indicating that a value wasn’t found or available. Returning an Optional gives you the ability to return a value or absence thereof safely. But what does that mean “safely?”
Let’s think about a normal function return such as a Customer object. The function can return two different things, either a Customer object or a NULL. It returns two things because operating on the Customer object is not the same as operating on the NULL. You might say, “Wait, what does that mean?” NULL, in this case, just means there is no Customer object. Again, you may say, “But you can’t call anything on that NULL, right?” And since you don’t know which is there (NULL or the Customer), you must check to see if that Customer is NULL before you can do anything on the Customer object.
Returning an Optional allows us to always have an object coming back. This means that your function return will always be of the same type (an Optional). The Optional has two really awesome properties, first you cannot just start using the object, you must get the object. More specifically you can actually do a test on the object to see if it
isPresent()similar to a NULL check. For example, let’s say that you have a method of
getCustomerById(Long id)which returns an
Optional<Customer>. Let’s see an example of updating a customer record.
We can use the
ifPresent()method and actually make this even cleaner though.
You can also use the
orElse()methods such as
orElseThrow()to protect against an empty
What if you wanted to create the object if it didn’t exist?
The second really awesome property is that, although people can attempt to
get()an empty Optional, the data type itself forces people to acknowledge that they are doing something without proper error checking. And in the cases above, can actually handle those cases when the Optional is empty as part of the datatype itself.
Stop using for loops and start using streams
Streams are one of the most powerful components in Java 8 besides lambdas. Without Streams, Java would’ve had no list comprehensions using lambdas. If you’re using lambdas but not using Streams, you’re really missing out. In many functional languages, list operations are native to the list structures. In Java, they are considered Streams which means that you’ll need to convert a list to a Stream before you can do list operations.
“Ok Josh, how do I use streams?” you may say. Easy, just call
stream()on your list object. “Ha, ha, no. I mean, how can I make use of streams?” you’d reply. Well, think about times that you
filter()out parts of a list or
map()an object into another type. Let’s assume that we need a function that will return all of the enabled customers id’s? Well, with
stream(), we can actually do a
filter()for only enabled customers on the stream followed by a
map()to convert the Customer object to customer ids. We will then
collect()these back into a list with the
Collectors.toList()static method. It’s pretty simple actually.
You can actually make it even easier by using method references.
This type of method chaining with transformations allows us to concisely express our ideas of how the functionality is working. In this instance, we are filtering out only customers that are enabled and converting Customers into their respective customer ids, then collecting them into a list. Now imagine that you wrote a function that you could then dynamically pick which filter to use — this allows us to simply recreate these functions.
Use the Nashorn scripting engine
Where to go from here
The best advice I can give you is to continue to find more places you can abstract functionality out into lambdas. Continue to get rid of your NULL checks by converting them to Optionals. Convert your for loops into streams. More importantly though, help others to understand how to make their code dynamic. The more you can get others to understand and write dynamic code, the more your code base will flourish.
Remember, anytime is a good time to refactor your code — just make sure that your refactor is providing benefit. There will be sometimes that it doesn’t, and that’s ok. Use that to learn why it didn’t work out. Don’t get frustrated, try again and again. Remember when you started programming and wrote broken applications, and got frustrated? You got past all that, right?
Editor’s note: This post is part of our ongoing exploration into learning how to solve programming problems.
Public domain splash image via Pixabay.
- 3 simple reasons why you need to learn Scala
- What every Java developer needs to know about Java 9
- Intensification of smallholder livestock production through utilization of crop residues for livestock feed in Tanzania
- [Infographic] Africa Mobile connections predicted to explode to almost a billion by 2017
- Some carbon-capture roadmap movement, but still a long way to go
- Gates tells G20 innovation is the key to development
Subscribe to our stories
- SL Crowd Green Solutions September 21, 2020
- Digital transformation in the banking sector: surveys exploration and analytics August 3, 2020
- Why Let Others Disrupt You? Take the Smart Self-Disruption Journey! August 3, 2020
- 5 Tips for Crowdfunding During the Pandemic August 3, 2020
- innovation + africa; +639 new citations August 3, 2020