Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question to Pierre-Yves Saumont  RSS feed

 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pierre-Yves,

Welcome to the forum and congratulations on the publication of your book.

I am new to Java, but not to programming (over 25 years experience). As such I have two quick questions:

1. Function and functional are such generic programming terms. What specifically do they mean/give to the Java developer?
2. Did functional programming exist in Java before Lamdas i.e. are Lamdas a formal response to something which was already happening or is functional programming a new concept in Java?

Cheers,

Paul.
 
Pierre-Yves Saumont
Author
Ranch Hand
Posts: 103
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Functional Programming have many different definition, maybe as many as there are functional programmers, but they usually agree about some key points. Functional programs are deterministic. A functional program will always give the same result for the same input data. This is achieved by composing programs from true functions, meaning functions which output will only depend upon their arguments. So, these functions cannot read any data from the outside world, nor can they change the outside world in any way. In other words, they have a return value and no side effect (meaning no wanted side effects, since unwanted side effects are simply bugs!). The result is that nothing can be observed beside the returned value. Pure functional programs have no effect, but they return something that can have an effect. But very often, functional programs are not so pure. They simply separate the purely functional parts from the effects. Purely functional programs can be easily tested (they do not depend on anything) and can be reasoned about (and proven correct). With not so pure functional programs, one can easily test and reason about the functional parts. This results in better abstraction (abstracting effects from computations), better reusability ant better reliability.

Functional programming existed long before Java. It may be related to the work of Alonso Church, (who incidentally died the same year Java was created). Functional languages existed long before Java (Lisp, the most famous functional language, appeared in 1958). And it was perfectly possible to write functional programs in Java before Java 8. It was just not so easy. The most important addition to Java, in the perspective of functional programming, is not lambdas (named after the "Lambda Calculus" of Alonso Church), but generics. Before lambdas, methods could be made functional. All one had to do was (and still is) to insure that methods would not depend on mutable state outside the method itself. This means that methods should not read mutable fields, nor mutate anything that is visible from outside the method. If functions needed to be manipulated (used as method arguments or return values) they had to be implemented as methods of class wrappers, generally in the form of anonymous classes. What lambda brought, beside a much cleaner syntax, is the possibility to do this without creating any class instance, which results into much better performance.

 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. Very interesting.

One thing which comes across to me is that "Functional Programming" seems to be a distinct branch of Java programming, and less of a tool to be used within a standard OO program. Talk of "Functional Programmers" adds to this perception. Is this how you see it?

Paul.
 
Campbell Ritchie
Sheriff
Posts: 55329
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You describe functional and OO programming as different paradigms of programming. Yes, they are very different. PYS points out that LISP appeared in the 1950s; that was nearly ten years before the first recognisable object language (Simula67).
 
Pierre-Yves Saumont
Author
Ranch Hand
Posts: 103
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would certainly not say that functional programming is an additional tool to be used within a standard OO program. I would rather say the opposite. Functional programming makes writing correct programs easier. But sometimes it is useful to resort to imperative techniques, be it for optimizing performance, or because some needed features are missing from the language. But I would agree that it is a distinct branch, because it implies thinking differently. It is like learning a foreign language. You start by thinking in your primary language and translating everything, which is both tedious and inefficient. With practice, you end  thinking in the new language and sometimes having difficulties to translate things to you original one. The difference with programing is that the benefits you generally gain from functional programming often makes it difficult to go back to imperative.

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!