Ted Neward is the Principal at Neward & Associates, a developer services company. He consults, mentors, writes and speaks worldwide on a variety of subjects, including Java, .NET, XML services, programming languages, and virtual machine/execution engine environments. He resides in the Pacific Northwest. Ted is a DZone MVB and is not an employee of DZone and has posted 35 posts at DZone. You can read more from them at their website. View Full User Profile

On Functional Programming in Java – It's Coming...

01.22.2013
| 4369 views |
  • submit to reddit
Elliott Rusty Harold is blogging that functional programming in Java is dangerous. He's wrong, and he's way late to the party on this one--it's coming to Java whether he likes it or not.

Go read his post first, while I try to sum up the reasons he cites for saying it's dangerous:

  1. Java is not a lazy-evaluated language. Programmers in Java will screw up and create heap and stack errors as a result.

  2. See? Here's a naive implementation of Clojure code taken directly over to Java and look how it blows up.

  3. Programmers can do bad things with this idea, so therefore we should avoid it.

  4. Oh, and by the way, it's "dangerously inefficient" in Java/JVM, even though I offer no perf benchmarks or comparisons to back this statement, and I'm somehow ignoring that Clojure and Scala run on the JVM as well, apparently without problem.

That kind of about sums it up, I think.

Look, as Elliott points out, Java is not Haskell. Neither is it Lisp. It's its own language, rooted in imperative and object-oriented history, but no less able to incorporate functional features into its development than Lisp could incorporate object-oriented features. However, if you do stupid things, like trying to re-create an infinite (implicitly lazily-evaluated) list in Clojure by creating an actualized list that stretches to infinity... you're going to blow the JVM up. Duh. Not even the supercomputer on the USS Enterprise five hundred years from now will be able to construct that list.

Porting code from one language to another is not a trivial exercise. If you attempt to port line-for-line and expression-for-expression, you can expect that your ported code will not be idiomatically correct. (I know this already, having done the exercise myself.) The root of the problem in his ported code is twofold. One, he (rather foolishly and in elegant strawman fashion) badly simulates what an infinite list would look like in Java--a commenter does the better job by showing how an Iterator can be made to perform the same thing that Haskell actually does under the hood by producing the next value on demand, rather than trying to create a list of Integers stretching to infinity. For someone who professes to have some Haskell experience and love, it surprises me that Elliott makes this kind of mistake, which leads me to conclude that he's trying to create the strawman. Two, he assumes that anyone who programs in Java functionally will have to create all of their functional tools by hand, and frankly, using Guava or FJ here would make this code sample a LOT easier to swallow. The fact that he ignores both of these in his stawman again sort of reinforces the idea that he's deliberately crippling his strawman to make his point.

His underlying point, though, seems to be simple: "I work with bad programmers, who don't seem to understand how to write code functionally in Java without screwing it up." Dude. Sucks to be you. "Bad programmers will move heaven and earth to do the wrong thing." --Glenn Vanderburg.

What really sucks for him is that these features are coming in Java 8, including lambda expressions and library support including a Stream interface that will allow for exactly this kind of code to be written without pain. Those programmers Elliott is working with are going to be even more on fire to use their functional approaches (and all the associated goodness of doing so, including composability and what-not) in their Java code. What might make Elliott more happy is that at least they won't have written it; it'll all be written by guys much smarter than any of them.

Published at DZone with permission of Ted Neward, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Mario Fusco replied on Wed, 2013/01/23 - 2:49am

Jilles Van Gurp replied on Wed, 2013/01/23 - 9:19am

I would use the word 'different' rather than the more provocative 'dangerous'. I've been switching between Java, Ruby, Python, and Javascript for the past year or so. I've been a Java developer since the late nineties but before that I actually learned programming using a Haskell predecessor called Gofer in university. There are a lot of different languages and paradigms that one can use to solve the same problems. I think people tend to treat them as proverbial hammers that are suitable for smashing any nail in sight. I think Java people got a little too carried away using object orientation by creating all these really elaborate frameworks for solving simple problems. Swing is a good example and I think JEE pretty much sets the standard of how to make something very simple and straightforward very hard.

Functional programming is great. Having closures in a language is also great. They are not the same thing (though they are related). Java is basically just adding closures to the language. Closures are common in many other languages and generally liked by the people that use those languages. Closures are useful for supporting a lot of popular idioms that are somewhat tedious to do in Java today. Making that problem go away is a good thing. I mean idioms like being able to map a function to elements of a list; being able to reduce a list; being able to do callbacks (without inner classes), etc. Most of these idioms are good things to have and are things that are quite easy to explain to people.

Java has actually had something resembling a closure for some time: anonymous inner classes You can sort of fake a lot of the before mentioned idioms using them. It's not great but quite common. Closures are going to be very helpful in getting rid of a lot of that tediousness.

Functional programming takes things to the next level. Pure functional programming can be very alien to most developers who haven't seen this before. Not having assignments and loops for example is a bit of a mind fuck for example; especially once you start realising you don't need them.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.