📜 ⬆️ ⬇️

From Java to Scala: 7 reasons to learn a new language

A lot of free time only students. If you are not a student, there should be very good reasons to start learning a new programming language. This is especially true of Java programmers, because this language is the leader in its segment. Nevertheless, there are reasons for learning a new language.

The first reason: experience shows that no return is returned . I once wrote more programs in C / C ++ than in any other language. Then I learned about Java, and in front of me stood exactly the same question: “Why?” What is most interesting, I did not learn Java for some reason, it was just curiosity, and then I realized that I could not go back. In my life, I have not seen a single C ++ programmer who has learned Java, and then declared “No, I don’t like this language, C ++ is better”. Now I have some kind of deja vu: I again do not see a single Java programmer who has studied Scala and refused this language. I think this is a sufficient indicator of quality.

Reason Two: Scala is more modern . No more fashionable, but technically more perfect. Scala takes into account many of the mistakes made when designing Java. Take the equivalence operator == as an example. How often do you use it in java? You can not think, I will answer for you: you do not use it at all. Why? Because it is useless. In a language whose goal is to free a developer from working directly with memory, the pointer comparison operator does not make sense. And even worse, this operator cannot be overloaded. What did Oderski do with this operator in Scala? That's right, he equated the == operator to equals() . I think no one needs to explain how the first is more elegant than the second:

 object1 == object2 object1.equals(object2) 

By the way, I don’t think at all that there was an error with the == operator when designing Java. At the time of C ++ dominance, this was the right decision, but over time the situation has changed.
')
Reason three: declarative . Yes, yes, the very functional programming. Now a lot of swear words will be sent to my address, but I will not argue much. My argument is very simple: I have seen enough people who have not mastered functional programming and scolded him, but I have not seen one who has mastered and refused. But I’ll give one more argument: if functional programming were some kind of nonsense, it wouldn’t appear in our beloved Java (are you aware of lambdas and streams?) Someone here will argue: since it is in Java, why us scala? Answer: if you study Scala and look at the Java functionality from that bell tower, the view that opens may well make you smile. Just try it!

Reason Four: less code . Someone said that the Scala code is generally 2-3 times smaller than the Java code. I did not consider it myself, but I assure you that it seems to be true. For example, surely many are familiar with the longstanding argument about whether to use final for local variables:

 String mama = "Jane"; final String papa = "John"; 

The second line is unchangeable, which guarantees its immunity, which reduces the likelihood of bugs, blah blah blah ... But there is a problem: this second line is 28% more than the first, which means total code sprawl, which increases the likelihood of bugs, etc. And now we look here:

 var mama = "Jane" val papa = "John" 

This Scala code is identical to the previous Java code. Do I need to say something?

Here is a more impressive example. Java:

 public class Child { final String mama; final String papa; public Child(String mama, String papa) { this.mama = mama; this.papa = papa; } } 

Now the same on Scala:

 class Child(val mama: String, val papa: String) 

Everything! Not even a single brace was needed! And now imagine how such chips (and there are many more, believe me!) Reduce the code of a large project and remember that the shorter the code is, the:


Reason Five: It is very easy . Scala is an original language in the sense that it is as imperative as it is declarative. And by the way, it is even more object oriented than Java. As a Java programmer, you can start by studying the imperative part, considering Scala as “Java on steroids”, and then in the process of studying the functional part more and more use the latter. Probably no functional programming language provides such flexibility and ease. I even got the impression that Scala was written specifically to lead Java-programmers (joke).

Reason Six: New Horizons . Learning a programming language is very similar to learning a linguistic language in the sense that it allows you to discover a lot of new things (provided that the programming language is completely different, and not “yet another boring language”). Another language gives access to a different culture, a different way of thinking. I guarantee that the study of Scala, especially its functional part, will greatly change the way you write Java code. Of course for the better.

Reason Seven: This is interesting . Well, here I have nothing more to say. Curiosity is characteristic of all living organisms, possessing at least the smallest brain, what can we say about man. True, many people lose curiosity with age, but I am sure that there are no such readers among them. Therefore, my recommendation: take the time and play with Scala. I assure that you will experience a lot of pleasant sensations!

Source: https://habr.com/ru/post/269695/


All Articles