loading...

A Beginner's guide to the Scala Programming Language

bmadusudanan profile image Madusudanan.B.N ・5 min read

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.,

Some of the other languages targeting the JVM are clojure, Ceylon lang, Kotlin, Groovy, Eta lang etc., besides java of course. But scala is unique in its own way.

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.

Most scala programmers use Intellij + Scala plugin,Eclipse as their IDEs. If you are an text editor fan there is something called ensime which supports major text editors.

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.

Functional programming

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.

I should learn scala

MOOC

Books

  • Scala lang books. This is pretty much the list that you want. All these books are very detailed and well crafted.

Blogs

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.

Yoda much to learn

Please do point out any mistakes/typos/anything that can be improved in this post.

Discussion

pic
Editor guide
Collapse
kushalvmahajan profile image
Kushal V. Mahajan

Are you suggesting to learn Java elephant first and then learn scala animal?

Collapse
bmadusudanan profile image
Madusudanan.B.N Author

I don't understand what you mean by the term "Java elephant". If you are trying to say that java is a tough/time consuming language to learn then I somewhat disagree. You don't need to learn java in its entirety, just the OOP part should be enough. It is not a necessity though, any OOP language such C#/Ruby/Python will do.

Collapse
kushalvmahajan profile image
Kushal V. Mahajan

Ok. I got carried away maybe from the line where you mentioned to tweak java libraries to scala. I get the ease of a Java person here. But wouldn't it require to learn Java in depth to understand those libraries completely and then customise to scala code? Now I get that it's possible to get to scala with just OOP but I believe you are trying to say is that to fast pace it, we need to have Java libraries which in turn means a Java background else there would be a effort from scratch to build scala libraries. Is that right?

Thread Thread
bmadusudanan profile image
Madusudanan.B.N Author

The advice that I gave is for existing java programmers.

The core of the java language is pretty small from my experience. If you want to develop complex applications then comes learning the libraries. This route is for existing java programmers and IMO a pretty good route. If you are new to the JVM and do not know java, then you can straight away get your hands dirty with scala. You might need to spend a little extra time, but the upside to that is there are certain concepts that you need not unlearn and re-learn.

Whether you should learn scala libraries in scala? Thats a pretty broad question to answer. It all depends on whether you want your code/architecture to be functional/object oriented. If you have time in hand then I would advise you to learn scala libraries directly else you can make a call on whether to learn scala libraries or use existing java libraries depending on the team you are in.

Thread Thread
kushalvmahajan profile image
Kushal V. Mahajan

Wonderful! I got the words I wanted. I come from Javascript background (MEAN/MERN). Scala is next I guess.

Thread Thread
bmadusudanan profile image
Madusudanan.B.N Author

Awesome. Don't get daunted. Learn one thing at a time. You can also start from my blog series that I have linked in the article. Not the best but I have tried my best for it to be easy on beginners.

Thread Thread
kushalvmahajan profile image
Kushal V. Mahajan

Sure.Looking forward to it.

Collapse
themoah profile image
A.D.