Why use Scala? We listed the advantages of Scala we see in our startup. See whether the benefits of Scala could help you too.
1. Focus on what matters: Expressiveness + High order functions
2. Get less bugs: Statically typed + Clean error management by design
3. Benefit from JVM ecosystem: Performant libraries + Good tooling environment
4. Attract talents
Scala is a JVM based language. It’s flexible, you can write code in both imperative or functional styles. Scala is a pragmatic language that mixes the best of functional and object-oriented programming. It basically means that you can create classes to encapsulate state and methods but you can’t mutate them.
The main power of Scala is to let the developers focus on what’s most important.
Running on the JVM, Scala is as powerful as Java but it is way clearer and more concise. It makes the code easier (and faster) to write and read!
For example, look at how we create a list of string in Java vs Scala:
High order functions are functions that abstract some control structure like a loop to update every element of an array.
They’re the functions we use most in Scala. They work by taking a function as a parameter. Used with anonymous functions, they’re perfect to focus on implementing the business logic instead of juggling with indexes to increment and stop conditions.
Look at how we can convert this list of string into a list of integers:
We can also compute the sum of it by using the high-order function fold:
By checking the type during the compilation, Scala type system reduces considerably the amount of bugs caused by type errors.
Type inference allows typing without being too verbose:
def square(x: Int) = x * x
Here the type of square (Int) is inferred from the * operator which multiplies an Int by an Int giving an Int in output.
And thanks to pattern matching, we can still write flexible code:
Here we can even decompose an object to pattern match on its attributes. We can even add pattern guards (if condition in a case), pattern match a regex or the elements of a List.
There are some useful standard types in Scala to manage errors. The most commonly used are Option and Either. An Option can either be a class Some containing a value or a None object. So we can associate a valid result to Some and an error to None. Let's write a function to safely update a String to Int.
And if we need an error message we can use the Either class that will return a Right containing the value or a Left with an error type.
Try is also a great type to manage error, especially to encapsulate every call to Java functions that can throw errors.
Option, Either or Try allow us to return a result or an error and not throw it to whatever will catch it. And thanks to a special Scala control structure, the “for-comprehension”, we can easily combine them to return the first error encountered.
dividend on the left part of dividend <- convertStringToInt(dividendStr) is the value in the Right of the convertStringToInt(dividendStr) result. If convertStringToInt(dividendStr) returns an error, it will stop immediately and return that error in a Left for safeStringDivision.
“for-comprehension” are also amazing to work with asynchronous code like HTTP calls encapsulated in a Future. We can combine them and return an error for the first error we encounter.
We can even combine Future with other error types like Either by using functional libraries like cats.
For example we use db connectors, libraries to manage timestamps, logging & monitoring clients and more!
There is a great IDE with Intellij Idea. The community also built a very efficient alternative Metals, a plugin that can transform your VS Code in Scala IDE.
Another useful tool we use is sbt-docker. It allows you to simply build a docker container embedding your application.
Such powerful language attracts skilled engineers who are curious and keen to test new tools, who have the willingness to discover new paradigms, new perspectives to see the world of programming... And this is the perfect mindset to join a startup as Engineer 1 to 10!