Journey of Learning Scala
I started learning scala this year, and it has been quite a journey so far. There were times when I felt I was finally getting a grasp of it all, followed by something total unknown the next day :( . I felt learning scala was not just learning the language, but also knowing the practices to get the best out of it. So, I jumped into cold water and started with contributing to scala services in AutoScout24.
Language semantics #
In my early PRs, I can see lots of if's and else's, because I didn't know about pattern matching. Another thing I did was I created variables in every state, instead of mapping it and letting the state flow.
To find the sum of all the even numbers of a list, I would have:
val listOfItems = List(1, 2, 3, 4, 5)
val evens = listOfItems.filter(v => v%2 == 0)
var sum = 0
evens.foreach(v => sum+=v)
But now I would write
listOfItems.filter(_ % 2 == 0).sum
It's okay to break it down and have variables in every step. As I read more scala code, I saw these cool tricks and tried to use them ( I have also been overusing some techniques :P). The Intellij IDE helps a lot in refactoring codes (e.g. finding out duplicated codes, extracting methods, simplifying anonymous functions to name a few).
Another good lesson is if it’s hard to write test for your feature, it probably needs refactoring. I've been guilty of writing huuuge functions serving several responsibilities. Then when I started writing tests, I was overwhelmed with the test cases I could think of to test the function. Breaking down the function by responsibilities helped me write better tests, and also gave me confidence in the code.
Lots of practice and trying out new things was crucial for me. So, I have been using the scala worksheet to try out and play with the language. You can also use scastie to run your snippets.
I have slowly conquered my fear of chaining functions. If the code compiles, it’s almost definitely good.^_^ There is still a long way to go, and I am lucky to have excellent scala developers in my team, who support and help me grow in my pace.
This article showed you just the tip of the iceberg of my journey. I have to admit that my journey is going a lot smoother, because of guidance I have from my team. If you are also interested to work in such a team, get in touch.
Software Engineer at AutoScout24