From PHP to Scala: my experience!

From PHP to Scala: my experience!

The journey that allowed me to discover this amazing language

ยท

8 min read

The vision is recommended for an audience of programmers only!

I started my career as a developer immediately after graduating from high school in 2017. It seems to me that it's passed a really long time since these days when the only concern was to beat my other classmates to get food at the school's vending machines. Let's resume a little bit of the road that led me to write this article.

The beginning

Me jumping into the new adventure

I worked for some months in a big Italian company but after 5 months I decided to move to another way smaller company (more or less 10 employees) because it was more stimulating.
I've spent 3 years in this consultant company as a Full Stack developer and I've really had the chance to work on a lot of different projects (E-learning, automatic invoice systems, E-commerce, Booking websites, Food delivery apps, etc.). I'm really satisfied that I made that decision years ago.

My experience with PHP

In this small company where I worked for 3 years, we were using PHP (7.4 version) with a really unknown framework called Yii2. This was a pain point since the documentation wasn't very good and the searches on Google didn't produce a lot of good results. Frameworks like Laravel or Symphony were more used and so they had a lot more community and resources (and also very good documentation).
For the first 2 months, I studied on Udemy a little bit of vanilla PHP with amazing lessons by Hidran Arias, CSS and JS. After this learning period, I've jumped on working on projects and I've touched and deployed "real code" in production. I still remember my first release which broke everything in production.

The years passed and I've broken the production environment less and less, plus I've gained some valuable knowledge on PHP. I've never felt "safe" to use a language where there was no strict typing or no compiler, a lot of my bugs wouldn't have happened with tools like the compiler and typing. But I was still too junior to see the real value of these amazing tools. During my journery with PHP I've also created some open-source projects like this:

  • Paypal demo --> A small implementation of the v1 and v2 APIs of Paypal with Laravel (I wanted to use Laravel because it was way better than Yii2).

  • Yii2ExportableModel --> I know, I should have written some README to show how it works, but it was an internal tool that we've used in the company so I didn't think at the time of writing it. This allowed declaring a Model which was "exportable" with an Excel file.

  • Electronic invoice senders --> I've also contributed to creating a very generic solution to implement a "Sender" which sends to the government the invoices. This package allowed us to have a very generic codebase and then we implemented these abstractions based on the provider which was used to send invoices (Aruba, Acube, etc...).

However, the desire for a new challenge led me to embrace Scala in 2022. Embarking on this new path, I dove into a world of functional programming

My experience with Scala

In April 2022 I started my new adventure in my current company. Everything was different from the previous company:

  • I was moving away from a consultant company to a product-based company

  • I was moving from a small local company to an international environment

  • A more structured organization and way of work

  • A completely different stack (and paradigm)!!

The anxiety was so great, I started by first of all learning about Scala. In the first month, I just studied the official docs, some courses on Coursera, and last but not least the amazing Rock the JVM courses, both the free ones on Youtube and the paid ones on the website. After the first month, I started coding by creating from scratch a new microservice (my first one!!) to gain more confidence with the language. I still have nightmares about me trying to understand the map and flatMap (What the hell is functional programming?? Monads??, in PHP we don't have this kind of thing ๐Ÿ˜†)

Now after more than 1 year, I can say with confidence that I reeeeaaaally love Scala and its ecosystem. Let's see together some points which I want to highlight from my "migration" to Scala. This was my initial impression of Scala:

Training ๐Ÿ’ช

At first, I started by understanding the new world in which I was. What is a map? and what about flatMap? What the hell are Covariance, Invariance and Contravariance??? I've spent hours reading the official docs and understanding these basic concepts. So I've wanted to share some great resources which helped me learn all these concepts:

  • Rock the JVM (Youtube + paid courses on the website)

  • Scala book

  • Tour of Scala

  • Coursera

  • Official docs (genius :D)

My 2 cents

After more than 1 year I can say that I really like Scala for a lot of reasons. Let's see them together!

The typing system ๐Ÿ’ก

With a typing system like this, I've really struggled to create bugs. I've really understood how a simple type can boost confidence in your code. I'm really amazed by the strong typing that this language offer since it was one of the things which I wanted the most on PHP (from 8.0 it started to use type but I was using 7.4). Typing itself in my opinion if it's done in a good way can be also documentation! if you type correctly things other developers will immediately understand what needs to be passed on that specific method. Obviously, if you type as String something that should've been Enum it's not the optimal solution. I've experienced how good typing can change the dev experience!!

The compiler ๐Ÿค–

One of my favorites. I've always underestimated the power of a compiler since PHP doesn't have it. Do you even imagine how many bugs I could have avoided with a tool like this in PHP? well, a lot ๐Ÿ˜‚. The true power of this tool came up into my mind when I had to do BIG BIG refactoring. In PHP this refactor would have led to an enormous headache since I was removing more than 8k lines of code. In PHP errors are thrown at runtime so it would be very very difficult to do a refactor like this. In Scala the job was way simpler, I just deleted (more or less) the things that I needed to remove and then ran sbt compile , the compiler would point me out to every place where the code gets broken. I just needed to fix them in the correct way and ran sbt compile again, if the Integration tests pass, the job is done!!
When this refactoring was completed, I really was amazed by the power of a tool like this. I swear, I will never go back to a language without a compiler (JavaScript cough...cough...).

Pattern matching: Love at first sight ๐Ÿซถ

Well... this functionality is absolutely number one of my favorite things in Scala. It can be trivial but when I discovered this functionality I was really amazed. Pattern matching is undoubtedly one of the most powerful and elegant features in Scala, making code more readable, maintainable, and expressive. Its ability to match a variety of data structures, and integrate seamlessly with case classes, and support guards provides developers with a robust toolset for tackling complex programming challenges. Below is a little snippet to show how much this tool is powerful and convenient, in PHP this would be a nightmare ๐Ÿ˜ฑ๐Ÿ˜ฑ!!

This example shows how much is flexible pattern matching. In the manageGreetable the method you can see it in action:

  • The first case is evaluated only if the user doesn't have a middle name (it's None)

  • The second one is evaluated only if the user has EXACTLY "Middle name" as the middle name (what an imagination I have)

  • The third one is evaluated when the user has ANY kind of middle name, it doesn't matter what is the value, the important thing is that it's None.

  • The last allows us to call the generic sayHi to welcome a not logged user

I hope I've shown you how much powerful it is!!

The Option monad โ“

Probably another trivial feature of the language, but I really love the Option monad. It allows me to express the presence or not of something without relying on something ugly as the damn NULL type which I really hate. After using Option I can't ever see anymore the nullable types, I swear. It's an amazing monad that works perfectly with the previously discussed pattern matching and it gives the code more robustness. My bold opinion is that I don't want to see any more null in a codebase ๐Ÿ˜‚

The syntax ๐Ÿ‡ธ ๐Ÿ‡จ ๐Ÿ‡ฆ ๐Ÿ‡ฑ ๐Ÿ‡ฆ

Infix notation is really really convenient!
It's a feature that allows you to have code written as if it was a sentence and it makes sooo easy to understand the code when you're reading it.

Let's take the example below: this is a test that checks if the field is not empty. But I'm sure you've already understood that since it seems that you're reading a sentence from a book.

What is Infix Notation ? you can use infix notation when calling methods that take a single parameter, by placing the method name between the object and the parameter, without using dots or parentheses.

Conclusions

In conclusion, my journey from PHP and Yii2 to Scala has been transformative. As a programmer, I've embraced Scala's functional paradigm, its powerful tools, and expressive syntax. These factors have elevated my coding experience, providing a solid foundation for tackling complex challenges with confidence. While my early days in high school memories might feel distant, the growth and evolution I've undergone in my programming career have been both remarkable and fulfilling.
Even if I appreciate Scala, my journey has just begun, I still have a lot of things to learn to use this powerful tool at its best!

Did you find this article valuable?

Support Kristian lentino's blog by becoming a sponsor. Any amount is appreciated!

ย