Sunday, March 13, 2016

On why you should try Scala or Swift

The new paradigm

Swift and Scala are new generation languages, and represent the contemporary trend of combining the elements of object-oriented and functional programming. Since concepts of object-oriented programming are supported, programmers can still carry on doing business as usual. By doing so, they will, however, fail to utilize great features offered by these languages.
Just as many of the modern languages take over the burden of memory management, freeing the developer to think and code on higher level of abstraction, modern functional tools are freeing us up from dealing with low level implementation details of many mundane tasks. This allows programmers to focus on tasks which add value to customers.

Ceding the control over details

OO makes code understandable by encapsulating moving parts.  FP makes code understandable by minimizing moving parts.
– Michael Feathers

Complexity is the source of many problems in programming. Quite often we are faced with concepts on different levels of abstraction within one unit; business logic intertwined with implementation details of displaying, storing, sending the data… This complexity is what Feathers refers to as moving parts. Problem quite often manifests itself by not being able to see the big picture from the noise of details.
Object-oriented paradigm is based on encapsulation – hiding the implementation details inside of a unit (class) and exposing the functionality through public interface on higher level of abstraction. In other words, developer lets the rest of the system interact with his/her code through interface defined in terms and on level of abstraction appropriate to the collaboration, ideally without disclosing the implementation details.
Modern functional languages, on the other hand, reduce the amount of implementation details by offering a set of tools which efficiently replace them. Primarily, these are the tools for working with collections, using the higher-order functions. In the imperative approach, which is common for structured and object oriented programming, developer would iterate through collection in some sort of a loop ( often a several nested ones), and apply some operations on its elements. Internal states are often introduced to optimize the process. This allows for faster response, but increase complexity and present a nightmare for introducing the parallelism through multi-threading.
Modern functional tools support declarative approach; they come with built-in optimization tools, which do not require for developer to introduce the additional structures and internal states. This promotes using constants wherever possible instead of variables, which reduces complexity and unforeseen side effects, especially in a multi-threaded environment.

My recommendation

Even if you don’t feel the need to change the language and habits, it is inevitable that these concepts will become a part of all mainstream languages. Apple is going to make the Swift its default language sooner or later. On the other side of the field, some functional concepts are making their way into mainstream JVM scene through Java 8. Scala, even though not yet a mainstream language, is a popular JVM language, very close in syntax and concepts to Swift, with many powerful elements. As such it is a great way to introduce yourself to functional programming paradigm.
My recommendation is that if you are already doing iOS development, dive into functional programming using Swift. As Java programmer you have 2 options: Scala and Java 8. Scala is more complex, yet more powerful and expressive language. On the other hand, Java 8 allows the functional concepts to be applied smoothly and without major changes into the existing projects which allow using the latest version of the JVM.

No comments:

Post a Comment