03.08.15 Getting started with functional programming Last month, Nemanja Maksimović spoke on two meetups organized by DaFED in Novi Sad and Start-itin Belgrade, on the subject of iOS development for Android developers. Even though he focused on mapping the concepts of Objective C to familiar concepts in Java, in the Q & A part of the talk, he suggested his fellow programmers to definitely try Swift or Scala. He will try to explain why… Why I focused on Objective C in the talk, and not Swift? Primary goal of the talk was to kick-start Android and Java developers in general into iOS development easily (and other way around). Objective C has been around for a long time, as the primary language for development in Apple’s ecosystem, so there is a huge base of examples, documentation, tutorials, books that can be used. Even though it may not seem obvious, Objective C, as an object-oriented language is conceptually similar to Java. Considering some peculiarities, elements and concepts of one language are easily mapped to the other one. By trying to map Java to Swift, I would cover only a fraction of the language capabilities, and half an hour was not nearly enough to introduce major new concepts. 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, just as many of them proceeded to do structured programming in the object-oriented environments, but they will 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 lets the programmer focus on dealing with tasks which add value from the customer’s point of view. 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 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 multithreaded 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, functional concepts can be applied smoothly and without major changes into the existing projects which allow using the latest version of the JVM. Read more blog posts from Nemanja here. Autor: Nemanja Maksimovic Engineering, Space, Rockets, History, Art, Computer and Board Games...