⬆️ ⬇️

Scala as enhanced java or java ++

Java exists for quite a long time, a huge amount of software is written on it and it is a leader in its field. However, Java is not without flaws. When designing a language, an important criterion was simplicity and ease of learning a language, and this simplicity forces you to write a huge amount of excess code to compensate for the lack of flexibility of the language: design patterns, XML configs, AOP, hetera setters, try catch finaly syntax, etc. It is also obvious that for all tasks a pure OOP approach is optimal, some tasks are much more efficient to perform in a functional style.



There are many different languages ​​- Groovy, Python, Ruby, JRuby and many more that can potentially replace java. So why exactly Scala?





Now a list of the main chips in Scala, which are not in Java:



Each of these chips is a minor improvement, without which one could well do, but together they form a very powerful tool. But how does this affect the programming process?



At times more powerful tools for library developers. The effectiveness of development in any language is largely determined by the availability of ready-made libraries. What is the use of language, if you have to develop it from scratch? The ability to develop your DSL, implicit parameters and conversions, higher-order functions, a powerful type system allows you to write more flexible and powerful libraries that are more convenient to use. But even if the library you need is not written on Scala, you can simply use the library from Java.

')

High-level work with collections. Due to the presence of closures for the majority of manipulations with collections (filter, make a selection, check all the elements for a condition, divide into groups, etc.) there are separate methods. This allows 95% of the use of ready-made library methods, while in Java you need to write the same operations every time.



Conciseness of dynamic programming. Thanks to implicit, implicit type inference and rich syntactic sugar, your code starts to look almost as succinctly as if you were writing in a dynamic language like Groovy or Ruby. In addition, each variable has its own type and the compiler will tell you if you will do something wrong.



Fewer non-information noise. This is exactly the moment where the overall picture is made up of trifles. In Scala, a lot of moments where it allows you to write a few characters less. The output is approximately 2-3 times less code than in Java without losing readability!



Support for many design patterns at the syntax level. If you want a singleton, it changes the word “class” to “object”, you want a factory — use a constructor with default values ​​and named parameters, you want a command — you simply pass a closure, etc.



More understandable and structured code of business logic. Not everything can be described at a high level, there is logic with a large number of if / else checks, conditions, and the allocation of additional auxiliary methods. Thanks to pattern-matching, monads, higher-order functions, and nested methods, this logic is much more readable. When in Java you need to do a lot of if / else / switch checks in Scala, you can use the pattern matching, when in Java you need to embed the interface and add a class to the configuration, in Scala you just need to pass a closure, when in Java you go to null checks and error handling in Scala monads are used when Utils classes, factories, interfaces with global variables are created in Java, and the companion object is created in Scala.



Conclusion



I do not claim that Java is an absolute evil. On the contrary, it is a wonderful language, it was a huge step forward for its time, but as time goes on and things change, new technologies appear, the old ones become obsolete. And we, programmers, need to move forward and learn new, more advanced tools. And Scala is great for this role, it is devoid of most of the drawbacks of Java and has a lot of new features with which the programmer can better express their thoughts and significantly increase their productivity.

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



All Articles