Monday, May 27, 2013

Scala Highlights

I went to a Scala meetup which was about introducing features of Scala to Java developers. The speaker was Dr. Venkat Subramaniam and his talk was very informative, easy to follow, and engaging. He showed examples of how to do common tasks in Java and then showed how it is better with Scala.

It was recorded so I'll link to the video when it gets posted. Some of my notes:

Scala is hybrid functional - you have the option of functional programming or Java like imperative programming. Functional programming is beautiful as we want to focus on telling the computer what to do instead of exactly how to do it.

Scala is more statically typed than Java - the Scala compiler does type inference so you don't need to be explicit about specifying the type (although there are circumstances when the compiler cannot infer the type). This inference is all compile time.

The concept of ceremony - ceremony is basically all the extra code/things you need to write in order to do what you want. This includes writing getters and setters and boiler plate code. Java has high ceremony -- imagine trying to explain "hello world" in Java to a newbie. Explaining each of the parts of "public static void main(String args[]) ) would take quite a while. Scala automatically creates classes, main methods, getters/setters so you can write less code. Whenever you need to use the IDE to generate code, it is usually a language smell.

From the Java book "Effective Java", everything should be explicitly declared final if the reference is never going to change. It is very easy to find all the places where you put final but very hard to find all the places where you forgot to put final. This is addressed in Scala with the val keyword so that one has to think about immutability when declaring. Also, method parameters are always immutable in Scala. Because Effective Java was such a huge part in improving the quality of Java code, there are many examples of Scala taking concepts from that book and implementing them directly as part of the language.

Scala IDE/Eclipse has a cool REPL called Scala worksheet that helps quickly evaluate and test code.