Sina has posted 1 posts at DZone. View Full User Profile

Things Erlang can Teach Java Developers

01.31.2011
| 12882 views |
  • submit to reddit

 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.

Final Word

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.

Published at DZone with permission of its author, Sina Bagherzadeh.

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

Comments

Eyal Golan replied on Mon, 2011/01/31 - 5:01am

Hi,

Thanks for your thoughts. I like this post :)

 

Can you recommend sites / books that helped you learn Earlang?

Did you have a 'project' in mind while learning? I ask this, because I find that it is always easier to learn a programming language when there is a goal for it (and not just the old Hello World stuff).

 

Thanks

Artur Biesiadowski replied on Mon, 2011/01/31 - 6:46am

For me, http://projecteuler.net/ is a great place to play with programming languages. Of course, some more advanced problems are getting too hard for casual programming, but even getting through first 30-50 while trying to code in 'idomatic' way for given language should give you some experience.

Damien Lepage replied on Mon, 2011/01/31 - 12:39pm

Funny, I have a similar java background and went through the same kind of exercice recently, except that I picked Clojure which allows to benefit from the java ecosystem. I would also recommend any java developer to learn a functional language, it's tough but extremely mind-opening.

In case you're interested, you can read more about my story here:
http://damienlepage.com/reformatting-my-brain-with-clojure/

Sina Bagherzadeh replied on Tue, 2011/02/01 - 1:31am

@Damien, I have read your story and it was interesting. Just as you said, we both went the same way.

Sina Bagherzadeh replied on Tue, 2011/02/01 - 1:45am

@Eyal, Hi. To start Erlang I recommend "Programming Erlang: Software for a Concurrent World" by Joe Armstrong (creator of Erlang) and to start OTP read "Erlang and Otp in Action". Beside Erlang manual pages, there is an introductory site "http://learnyousomeerlang.com" to start.

While I started learning Erlang, I did not have any project in my mind. I just tried to rewrite some of my daily work with Erlang.

@Artur, Thank you for introducing "http://projecteuler.net". It was interesting specially regarding to my major which is "Applied Mathematics".

Rj Smith replied on Wed, 2011/02/02 - 4:47pm

"Once, I remember while I was reviewing code written by one of my colleagues, I saw a 1240-line function."

 

This has nothing to do with your choice of programming language. This is just bad coding.

Sina Bagherzadeh replied on Sat, 2011/02/05 - 1:46am in response to: Rj Smith

I think you did not read it carefully. I did not say I have chosen Erlang because it is concise. I have said that "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". One of things I have learned from Erlang is code-conciseness. Read that section again.

Gar Labs replied on Tue, 2011/08/23 - 11:47am

Interesting article. Erlang is on my to-do list now. -GAR Labs

Comment viewing options

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