Friday, April 29, 2022

Lazy Java code makes applications elegant and sophisticated

In lazy development, you focus on the “what” and functional libraries handle the “how.”

“The functional style of programming is very charming,” says Venkat Subramaniam. “The code begins to read like the problem statement. We can relate to what the code is doing, and we can quickly understand it.” Not only that, Subramaniam explains, but as implemented in Java and beyond, functional-style code is lazy—and that laziness makes for efficient operations because the runtime isn’t doing unnecessary work.

Oracle Java Exam Prep, Oracle Java, Java Exam Prep, Java Career, Java Jobs, Java Skills, Oracle Java Certification

Subramaniam, president of Agile Developer and an instructional professor at the University of Houston, believes that laziness is the secret to success, both in life and in programming. Pretend that your boss tells you on January 10 that a certain hour-long task must be done before April 15. A go-getter might do that task by January 11.

Aiming to complete the work superquickly is not the best approach, insists Subramaniam. Instead, be lazy. Don’t complete that task until April 14. Why? Because the results of the boss’s task aren’t needed yet, and the requirements may change before the deadline, or the task might be canceled altogether. Or you (or your boss) might even leave the company on April 13.

So, don’t complete the job until you need to do so, because maybe you won’t need to do so after all.

This same mindset should apply to your programming, says Subramaniam: “Efficiency often means not doing unnecessary work.”

Subramaniam often explores how functional-style programming is implemented in the latest versions of Java, and why he’s so enthusiastic about using this style for applications that process lots of data—and where it’s important to create code that is easy to read, easy to modify, and easy to test.

First, a quick comparison of functional-style programming and imperative-style programming.

Functional versus imperative programming

The old mainstream of imperative programming, which has been a part of the Java language from day one, relies upon developers to explicitly code not only what they want the program to do but also how to do it.

Consider software that has a huge amount of data to process. A developer might create a loop that examines each piece of data and, if appropriate, takes specific action on that data with each iteration of the loop. It’s up to the developer to create that loop and manage it—in addition to coding the business logic to be performed on the data.

This classic imperative model, argues Subramaniam, results in what he calls “accidental complexity,” as each line of code might perform multiple functions, which makes it hard to understand, modify, test, and verify. Plus, the developer must do a lot of work to set up and manage the data and iterations. “You get bogged down with the details,” he says. “This not only introduces complexity but makes code hard to change.”

By contrast, when using a functional style of programming, developers can focus almost entirely on what is to be done, while ignoring the how. Why? Because the how is handled by the underlying library of functions, which are defined separately and applied to the data as required.

Subramaniam says that functional-style programming provides highly expressive code, where each line of code does only one thing: “The code becomes easier to work with and easier to write.”

He adds that in functional-style programming, “The code becomes the business logic.”

No, this isn’t entirely new

If you’re familiar with programming languages such as Clojure, Python, Erlang, or Haskell, you know that functional-style programming isn’t new. It’s not even close to new: The granddaddy of functional-style programming languages, LISP, first appeared in 1958.

Oracle Java Exam Prep, Oracle Java, Java Exam Prep, Java Career, Java Jobs, Java Skills, Oracle Java Certification
What’s new is that functional-style programming features were added to the Java programming language with the release of Java 8, which introduced lambda expressions, streams, and other features that enable developers to use functional-style programming where appropriate.

For example, streams are pipelines of data that can be transformed and mapped using functions. Conceptually, streams are easier to understand and manipulate than lists, which are a common data structure used in imperative-style programming.

Of course, Java 8 and later versions can still be programmed imperatively, giving developers tremendous flexibility. However, as Subramaniam says, functional-style programming is going mainstream, and that’s where the biggest benefits will be realized—especially when you get lazy.

Lazy programming in Java

Subramaniam explains that the Java language specifications for functional-style programming include laziness—and that’s a benefit at runtime.

Why? Functional-style programming, using streams, is designed to make a single pass through the data, performing all the necessary actions on each element in the stream before moving to the next element. If certain actions aren’t necessary to perform because the results of that action won’t be used, the Java implementation knows not to perform those actions on that element.

By contrast, in some other functional-style programming languages, there might be several iterations through the data. In some cases, actions will be performed on elements in the stream, even when it’s unnecessary or the results won’t be used. This can drastically hurt performance on very large datasets, he says.

In modern Java, says Subramaniam, “A collection of functions is applied on each element only as necessary. Instead of a function being applied on the entire collection, intermediate operations are fused together and run only when necessary.”

He adds, “In other languages, you can’t use lambdas and streaming for big data projects if the language doesn’t support lazy evaluation.” Those lazy streams are what got Subramaniam hooked on functional programming in Java: “Laziness is the ultimate sophistication.”

Source: oracle.com

Related Posts

0 comments:

Post a Comment