I have been dabbling with scala for some time now and most of the questions that I get from my friends/colleagues/random people is that
- Why scala is hard?
- How to learn scala in an easy way?
- Is learning scala worth it?
In this article I present the learning path that I took, the mistakes I made along the way and what I learnt.
I will focus mostly on a path for java developers since they are the majority, but other devs can also learn a lot from this article.
Little bit of background
The history of scala is summed up pretty well in the scala lang wikipedia article. Main motivation behind creating scala was to create a multi paradigm programming language which is functional,object oriented which runs on the JVM. We all know that many languages now target the JVM for its performance,widespread usage etc.,
Scala as the first programming language
This mistake is often because of buzzword first design mindset. Scala is definitely not a beginner friendly language and it does not try to be. Not that it can't be, but it does not make sense to cram in so many things into a single language. The result will be a horrible mess of everything.
Well that's not encouraging right, but don't be disheartened. Contrary to the popular myths out there, scala can be learnt with moderate effort. If you are a beginner with no programming experience then there are several MOOCs which can teach you basic programming. One of the best that I know of is CS50. You can then learn little bit of python and then progress to java.
In theory any OOP language would do, such as Python/Ruby/C#.
Learn the OOP side first
Now you are all set to take on scala. The next big mistake people would normally do is to learn the functional side of it and get overwhelmed by it. Scala is not to be blamed. We should blame what we have been taught in our undergraduate courses. Almost all of the college undergraduate curriculum do not teach foundations of functional programming. As a result our style get fixed to an imperative/OOP paradigm which is very hard to get across.
It is very much possible to do object oriented/imperative style programming in scala as well. This multi-paradigm style makes it perfect to learn functional programming without losing too much of your hair. I would highly recommend to learn the object oriented side of scala and convert small coding snippets/algorithms from java to scala.
Since scala runs on the jvm, it can very much use java libraries with very minimal changes. Try consuming java libraries in it. The interesting part is that resulting code in scala will already be more concise/shorter due to the programming syntax of scala.
Do this step before trying core scala libraries, as they embrace functional programming more. The developers of scala didn't develop equivalent imperative/oop library equivalents of what is there in java such as spring/hibernate/gson etc., since you can use their java counterparts directly in scala. Language interoperability is a huge benefit and we must take full advantage of it rather than using core scala libraries just for the buzzword or being cool.
Getting familiar with the ecosystem
Many people underestimate this step. It is very important that you should be comfortable with the tools/ecosystem because that can frustrate you even more. Since scala works on top the jvm, the major difference from java would be the compiler obviously and then a different IDE perhaps.
We can use the traditional maven build tool for projects, but it is recommended to use the scala build tool or sbt for short. Sbt offers a more simpler way to deal with dependencies and you can learn with very minimal effort to get started.
Scastie is a handy tool to share code snippets. Kind of similar to jsfiddle.
Approaching functional programming
Since scala combines OOP and FP at a fundamental level, we can resort to lazily include FP style in our codebase. From my personal experience I would take the following route.
Java -> Scala OOP (gradual migration- since they can inter-operate)
Scala OOP -> Scala FP (can be gradual/complete refactor based on the project requirements)
IMO this what makes scala shine since at each stage of development/refactoring, it can be done part by part without breaking the regular development cycle if done properly.
Why learn scala?
So why learn a language which is hard to learn and takes so much of your time? Many people generally tend to learn new technologies/languages for career growth/getting a break into the industry and for this scala is perfectly fine. But apart from this functional programming can genuinely make you a better programmer even if you don't use it in your day to day work. It makes you think in ways you have never thought before and the decision to use it for practical purposes lies with you since you can program in OOP style as well.
It is also preferred in,
- Machine learning (Apache spark mllib)
- Big data (Apache spark, Apache kafka)
- Front end development
- It also has a native runtime now, so you are not restricted to the JVM and can take advantage of native programming libraries. Scala native is in early stages btw.
I have written an article on why scala will be a language that you should consider. Do give it a read.
Materials to learn scala
Unlike many other languages, scala does not have a standard learning path that fits experienced functional programmers/novices alike. I have categorized materials based on their format.
- The functional programming specialization in scala. I wouldn't call this beginner friendly but very comprehensive nonetheless coming from the founders of scala themselves
- Scala lang books. This is pretty much the list that you want. All these books are very detailed and well crafted.
- The Neophyte's guide to scala. This is easily one of my favourites on many core functional programming topics.
- Haoyi's blog. Creator of scala js.
- Blogs from scala lang authors themselves
- Scala tutorial series. I am writing a blog series aimed at learning scala in the exact same way I described above. But I am no perfect writer, YMMV.
Difficulty in learning something is highly subjective. Some folks find math easy and some don't. The best way is to decide for yourself keeping in mind what people have already gone through.
Please do point out any mistakes/typos/anything that can be improved in this post.