Scala: What's in It for Me?
After a few years developing in Java, I've just started to work in a project that uses Scala. It's my first experience with a functional programming language and, while I know that I still write some Jala (Scala in Java-style), it's growing on me. Here are some thoughts about my experience with it.
Scala has a lot of neat features: pattern matching, case classes, higher-order functions, type aliases, etc., but the thing I found more fascinating is how it makes it simple to write code that has little or no state at all. Code becomes much easier to understand and deep class hierarchies usually become unnecessary.
Also, Scala has much less boilerplate than Java, making it easier to focus on the problem you want to solve. I never thought much about that, but when I now write some Java I do feel it.
All languages have their own issues and Scala is no different. While developing in it you can find minor inconveniences here and there like slow build times, libraries and tools that change a bit too often or have no documentation, IDE support that is not as good as Java's, etc.
I guess that those will improve over time, but by now one has to get used to it.
Scala is a powerful and flexible language, and though this usually a good thing, there are some features that can add quite a lot of complexity and 'magic' to your codebase, making it difficult to maintain.
In particular, I think that implicit parameters and operators are often abused in third party libraries and make the code difficult to understand. The first adds logic that is not visible in the client source code (hey, it might be implicit but is not always obvious!) and the second is often used to create made up operators that are specific to the library. There are even unicode operators!
When coding with Scala, specially if you come from an imperative background, it's good to check for the following constructs in your code:
- vars and mutable collections: one of the cornerstones of Scala (and FP in general) is the use of immutable data. If you find yourself using vars or mutable collections there's a good chance that your algorithm/function could be refactored to not use them.
- Unit: functions that return Unit (the equivalent of Java's
void) indicate that they have some kind of side effect, otherwise they would be useless and should be removed! Always try to return the result of a function instead of modifying some internal state.
- Exceptions: Scala doesn't have checked exceptions and it makes it so easy to return a value or an error (using Either or Scalaz Validation) that you should probably never throw or rise an exception in your code.
I'm not saying that the previous constructs should never be used, there are certainly cases where they are useful or even required, but most of the times they are a sign that we're coding in imperative style.
Where to Start
I've found Functional Programming Principles in Scala a good course to start learning Scala and Programming in Scala, also by Martin Odersky, a more thorough resource to learn it in depth.
Nevertheless, I think Functional Programming in Scala does a better job at showing how to program in a functional way, instead of just teaching Scala's syntax and features.