Things Erlang can Teach Java Developers
After 10 years programming in Java, I felt learning a new programming language, specifically in the functional languages world, will make me fresh. After a little googling and reading some reviews I selected two to look into more deeply: Erlang and Scala. Each one interested me because of some features. Before going on to read, please note that this article is not a comparison between any X and Y language.Scala is a new and fast-growing kid in JVM-based languages world and has earned a lot of attention. It is combining both object-oriented and functional features together. It has some benefits like using previously written Java code, syntax which leads to concise code (however, as you become more experienced scala developer your code will become more concise and a bit harder to read for lower level developers), Thread-based and event-based actor model, etc. These features are all good but some combinations are not made in heaven like using old-written Java code and actor model because the design in old Java code is not matched to actor model.
In contrast, Erlang is a mature language which has been in play for more than 20 years. It has experienced a 99.9999999% up time because of its powerful actor model. This actor model is completely message-based and shares nothing between processes (against Java Thread model). Although, Erlang syntax is a little strange for imperative language programmers (C, C++, Java).
After 2 months playing with the two, I selected Erlang to dive in. Why? There were some reasons:
- I like parallel and distributed programming and Erlang unifies both for you because of powerful location transparency between processes
- Erlang is mature and Scala is under active development
- Erlang syntax and its pattern matching is fun to work with
- And finally, I wanted to experience a functional language not a hybrid one
While I started to discover Erlang (beside my daily job, coding Java), I have found that it can help me to code better in Java. I list the reasons why below. Note that these are my personal experiences:
- Erlang code is concise. You rarely see long functions in Erlang. Conciseness helps to have more readable and maintainable code. It teaches you to write small functions. Imperative language programmers tend to write long functions. Once, I remember while I was reviewing code written by one of my colleagues, I saw a 1240-line function.
- Designing applications such that they use immutable objects is hard, because in OO languages we are used to changing state (object fields) whenever we want. But working with Erlang teaches you how to deal with immutable variables because it forces you to do so (of course immutability is not a feature born in Erlang, it is a functional language feature). When you learn how to deal with immutable variables, you can design multi-threaded Java applications which use synchronized and lock less than before with better performance and will be written more easily.
- One of interesting best practices of Erlang (or better to say, functional languages) is writing side-effect-free functions. A side-effect-free function is like a mathematics function which does not affect its environment and just takes an input and gives an output. Immutability of input variable of a method push you toward writing side-effect-free functions (of course it does not force you completely to do so, you can write non-side-effect-free functions in functional languages too). When you write a side-effect-free method, debugging and reasoning about the flow of application becomes more obvious.
Learning a new language really makes you and your mind fresh. It opens new horizons for you and gives you a broader view. I recommend imperative language programmers to learn a functional language. If you are fan of parallel and distributed programming like me, you will enjoy it a lot.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)