Chapter 4. Conclusions
Object-Oriented vs. Functional Languages
In this report, we’ve covered a lot of ways in which ideas from functional programming relate to existing object-oriented design principles. These idioms aren’t as different as a lot of people make them out to be. Definitely functional programming emphasizes the power of reuse and composition of behavior through higher-order functions. And there’s no doubt that immutable data structures improve the safety of our code. But these features can be supported in an object-oriented context as well, the common theme being the benefits that be achieved are universal to both approaches. We’re always seeking to write code that is safer and offers more opportunity for composing together behavior in a flexible manner.
Functional programming is about a thought process. It’s not necessarily the case that you need a new language in order to program in a functional style. Some language features often help though. The introduction of lambda expressions in Java 8 makes it a language more suited to functional programming. While object-oriented programming traditionally has been about encapsulating data and behavior together, it is now adding more support for behavior on its own thanks to ideas from functional programming.
Other languages such as Scala or Haskell take functional ideas further. Scala offers a mix of both functional and object-oriented programming facilities, whilst Haskell focuses purely on functional programming. It’s well worth exploring these languages and seeing what set of language features you find useful in your problem domain. However, there’s no need to necessarily move to Scala or Haskell thinking that they’re the only way to program in a functional style. However, they certainly offer some features that Java lacks, and it’s sometimes worth using different programming languages.
I appreciate that this maybe is a controversial opinion to those who have spent their careers advocating functional programming, but software development isn’t about idealism or religious evangelism: it’s about producing reliable software that works for our clients and business. A functional style of programming can help us achieve this end, but it is not the end in and of itself.
Programming Language Evolution
One of the interesting trends over time in programming languages is the gradual shift between languages that are more object-oriented and more functional. If we jump in our Delorean and go back in time to the 1980s, a lot of interesting changes were going on. Older procedural programming lanugages were being phased out and there was a growth in the popularity of both object-oriented and functional programming languages.
Interestingly enough, a lot of the early advocates of both functional and object-oriented languages combined features of the other. If you ask any object-oriented purist what her ideal programming language is, she’ll tell you it’s Smalltalk. Smalltalk 80 had lambda expressions, and Smalltalk’s collections library was inherently functional in nature, and equivalent operations to map, reduce, and filter existed (albeit under different names).
A lot of purist functional programmers from the period would tell you that Common LISP is the ideal functional language. Interestingly enough, it had a system for object orientation called CLOS (Common LISP Object System). So back in the 1980s, there was reasonable recognition that neither paradigm was the only true way to enlightenment.
During the 1990s, programming changed. Object-oriented programming became established as a dominant programming approach for business users. Languages such as Java and C++ grew in popularity. In the late 1990s and early 2000s, Java became a hugely popular language. In 2001, the JavaOne conference had 28,000 attendees. That’s the size of a rock concert!
At the time of writing, the trend has changed again. Popular programming languages are moving away from being specifically object-oriented or functional. You will no doubt get the old holdout such as Haskell or Clojure, but by and large, languages are going hybrid. Both C++ and Java 8 have added lambda expressions and started to retrofit useful elements of functional programming to their existing object-oriented capabilities. Not only that but the underlying ideas which have been adopted in terms of generics in Java and templating in C++ originated in functional programming languages.
Newer languages are multiparadigm from the get-go. F# is a great example of a language which has a functional style but also maintains parity with C# in terms of its object-oriented features. Languages such as Ruby, Python, and Groovy can be written in both a functional and object-oriented style, all having functional features in their collections API. There are a number of newer languages on the JVM that have developed over the last decade or so, and they predominantly have a mixture of functional and object-oriented features. Scala, Ceylon, and Kotlin all come to mind in this regard.
The future is hybrid: pick the best features and ideas from both functional and object-oriented approaches in order to solve the problem at hand.