View of the Swiss Alps from Mount Titlis

Let’s give it a try

Marek Galik

--

There is this trend now, that many developers want to have their own blogs about programming. And yet I still have a feeling that it would be nice to have a place, where I can share my own thoughts.

But why

Most writers in the world of iOS development focus on the newest additions to Swift like property wrappers or the newest addition of async/await and other cool stuff. But Swift has some very interesting features from the very beginning. In fact, I believe that this language is one of the best widely used languages for doing functional programming and that is what I’d like to explore in this blog at least in the beginning.

Functional programming

Well… many people say, that functional programming is yet another hype, another trend, which soon will be gone. However, I’m one of those who think quite the opposite and that it’s not a temporary trend. Back in 2016, when I started working as an iOS developer I was overwhelmed by all these architectures and design patterns. Every day a different colleague came up with a different design pattern and I could not believe that there was no easier way of writing the same logic.

One day one of our colleagues came up with the idea to use a functional-reactive programming library. So we started to use it. However, the way we’ve used that library wasn’t ideal and many times we were not aware of the true benefit of functional-reactive programming. At that time it got me thinking, where does the word functional come from? And I found out that it was from functional programming. Then, why should we bother with it, and if so, then is there a way for me to understand it a bit deeper? So, I started googling and I found that there is a huge category of so-called functional languages. I didn’t know which one to choose because I knew no one who could help me with that decision. So I chose the one with the coolest name out of all- Haskell. According to some opinions, it was arguably one of the most difficult programming languages. So I bought this book about the language and it changed the way how I perceive programming. Since then I personally like Haskell notation even for describing general algorithms. To me, it was an incredible experience of discovering a completely different and much more elegant approach to writing code.

Then why Swift and not Haskell

Haskell is an incredible language (at least I believe so). However, I still wanted to be focused on mobile development. Fortunately, Swift is in many ways inspired by Haskell and it has many nice features that enable you to write functional code. In this blog, I’d like to focus specifically on these features that make Swift more suitable for functional programming than other “commercially successful” languages.

So what will be the main topic

Programming doesn’t need to be only about learning these complex design patterns and architectures. Instead, of that functional programming helped me to focus more on algorithms, data structures, and relationships between them. I truly believe that writing FP code can not only bring clarity and a different perspective to writing code, but it can also bring back fun to programming. Besides that, new frameworks like SwiftUI show us, that Apple is serious about bringing new ideas based on FP to a larger audience.

Silver bullet

No matter how much I’d try to advocate for FP on iOS, it’s still not a silver bullet. Even though SwiftUI is gaining popularity, still most of the code is currently written by using Apple Frameworks which are object-oriented and require us to do stuff like inheritance and managing local state. However, even in this kind of code, we are able to leverage the power of Swift and improve our code by using principles from functional programming. So, let’s give it a try…

--

--