[英] 《Java 8函數語言程式設計》作者Richard Warbourton:Java的亮點不是語言本身(圖靈訪談)

盼盼姐發表於2015-06-09

Richard Warburton是一位經驗豐富的技術專家,善於解決複雜深奧的技術問題,擁有華威大學電腦科學專業博士學位。近期他一直從事高效能運算方面的資料分析工作。他是英國倫敦Java社群的領導者,組織過面向Java 8中Lambda表示式、日期和時間的Adopt-a-JSR專案,以及Openjdk Hackdays活動。Richard還是知名的會議演講嘉賓,曾在JavaOne、DevoxxUK和JAX London等會議上演講。根據對Java社群和Java 8新特性的深刻理解,他創作了《Java 8函數語言程式設計》一書。

iTuring: How did you become a coder? And how did you become a coder specialized in Java?

I was always always interested in technology and when I was at school I fell in with "the wrong crowd" - programmers. It was great to see people who could make useful and interesting things. So I picked up a book on visual basic - the first programming language I ever learnt - read it and started programming. As I later found out visual basic leaves much to be desired in a programming language and at university I learnt Java. Despite using a number of other programming languages I seem to end up coming back to Java. Its not really the language so much as the ecosystem, tooling and libraries that are available.

iTuring: Does your experience in London Java Community help you with the idea of your book Java 8 Lambdas? How?

Yes, definitely. I initially started playing around with Java 8 prototypes before it was released by organising the LJC hackdays on the topic and sending feedback back to the expert group. I also got in touch with O'Reilly at a London event. Java 8 Lambdas would never have happened with the LJC.

iTuring: Lambdas are invisible on JVM, in your opinion, why did Java choose to support the syntactic feature by integrating it into Java 8 instead of building a plug-in?

I think this really derives from a bunch of other design decisions in Java 8. It was decided that there would be no special function types added to represent lambdas - they would just use interfaces with single abstract methods (functional interfaces). Once that's in place any library that wants to call a lambda expression just needs to call a method on an interface. There's no need to make any bytecode changes to implement that.

Its worth noting that there's quite a lot of mechanics going on to make lambdas work, which are described in some detail in an article I co-authored at http://www.infoq.com/articles/Java-8-Lambdas-A-Peek-Under-the-Hood.

iTuring: Scala, Groovy and Clojure are genetically built on functional programming, do you think Java has any unique advantages comparing to these languages?

Absolutely, but its not the language! Java is a perfectly solid language but languages like Scala are always going to add more features than Java has. Clojure will always be simpler, less syntax and less concepts. Groovy offers a mix of static and dynamic typing and a scripting focus that Java lacks. All of these languages have compelling reasons for being used over Java, but at the same time they're also missing a lot of other things.

Java has excellent mature tooling and IDE support that things like Scala and Clojure lack. To me these tools contribute to your productivity just as much as the language itself and remove a lot of the day-to-day development frustrations. I'd pick Java for many projects over Scala or Clojure for the tooling.

iTuring: Stability and simplicity are Java's biggest advantages, do you agree that functional programming of Java 8 and multiple inheritance mechanism of Java 9 will increase complexity and maintenance cost of Java?

It depends upon how you view things. In my opinion the introduction of functional programming simplifies things for many programming tasks. Collections and data processing are a very commonly performed tasks by Java developers and they're ideally suited to a functional paradigm. There's an overhead to learning these new concepts and there is an added complexity in the sense that you need to decide when to write functional and when to write imperative code. So to me its not clear-cut either way. Functional programming does make things easier, but there's more to learn.

iTuring: In Java 8, which is more important, maintaining the system or solving problems?

I'm not sure what this question is actually asking I'm afraid.

iTuring: How does the ecosystem of Java respond to functional programming of Java 8? How long would you expect before functional feature of Java 8 be used universally across the board?

I think if you're thinking about adopting Java 8 then you should go incrementally. First thinking about using core library features such as streams and collectors in conjunction with lambda expressions. I think this adoption phase is well under way and has begun to happen and I'm seeing people use these features in real code.

I think over time we'll start to see a more functional style of Java evolve - using some of the functional design patterns, more immutable classes. There's already a few libraries beginning to adopt this style. I've written a testing framework (http://richardwarburton.github.io/lambda-behave/) that gives an example of the thing kind of thing you can do.

So I think eventually there will be strong adoption of all the Java 8 functional features, but it'll be a gradual process.

iTuring: Java used to be object oriented, now it fully supports functional programming. As a coder, what can we do to facilitate the transition and make the best of functional programming?

I think there's a few steps to the transition.

Step 1 is to learn more about Java 8. Reading books, tutorial and trying out a few simple examples of functional code. Once you know the basics move to step 2.

Step 2 is to adopt Java 8 on a work project. In my opinion its good to have a small and low risk project to try things out on, but to try it in the context of solving real world business problems. Once you're familiar with Java 8 in a real world setting move to step 3.

Step 3 is to bump all your code to Java 8 and test it out without using any Java 8 features. This is because even though Java SE maintains really strong binary compatibility some third party libraries may break and you'll have to upgrade them. If you're already using the latest libraries this may not be a problem for you. Once everything is working move to step 4.

Step 4 is start writing Java 8 code across your main code-base and have fun doing so.

iTuring: In functional programming of Java, do you have some best practices that you would like to recommend? And have you identified any traps during the way?

I've never liked the idea of "best practices" - these things change over time and depend upon context. Let's talk about some things which are good things to use.

  1. Streams and Collectors. If you're writing code that involves the collections API then these are your new best friend.

  2. Immutable Data. Often its simpler and less error to return new objects rather than mutate existing ones.

  3. Think functional. Instead of thinking "how can I update this blob of state?" ask yourself "how can I transform this input into an output?"


更多精彩,加入圖靈訪談微信!

相關文章